xshell连接Linux、ngix部署
Linux端安裝sshd服務(openssh-server)
查看防火墻:ufw(Linux默認安裝了)
再就是客戶端了。。
?
平時在測試環境下的項目不能承載高并發,需要部署到web server上。
web server:
? ? apache(早期)
? ? ngix(更能承載高并發、輕量級,底層是I/O多路復用epoll)
如何在生產上部署Django?
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。
?uwsgi介紹
uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。
要注意 WSGI / uwsgi / uWSGI 這三個概念的區分。
- WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx,uWSGI等服務器)與web應用(如用Flask框架寫的程序)通信的一種規范。
- uwsgi是一種線路協議而不是通信協議,在此常用于在uWSGI服務器與其他網絡服務器的數據通信。
- 而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
- uwsgi協議是一個uWSGI服務器自有的協議,它用于定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣東西。
?uwsgi性能非常高
uWSGI的主要特點如下
- 超快的性能
- 低內存占用(實測為apache2的mod_wsgi的一半左右)
- 多app管理(終于不用冥思苦想下個app用哪個端口比較好了-.-)
- 詳盡的日志功能(可以用來分析app性能和瓶頸)
- 高度可定制(內存大小限制,服務一定次數后重啟等)
總而言之uwgi是個部署用的好東東,正如uWSGI作者所吹噓的:
If you are searching for a simple wsgi-only server, uWSGI is not for you, but if you are building a real (production-ready) app that need to be rock-solid, fast and easy to distribute/optimize for various load-average, you will pathetically and morbidly fall in love (we hope) with uWSGI.
Uwsgi 安裝使用
1 # Install the latest stable release: 2 pip install uwsgi 3 # ... or if you want to install the latest LTS (long term support) release, 4 pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz安裝uwsgi
基本測試
Create a file called?test.py:
# test.py
def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"] # python3#return ["Hello World"] # python2
運行
uwsgi --http :8000 --wsgi-file test.py 用uwsgi啟動Django
uwsgi --http :8000 --module midware.wsgi
可以把參數寫到配置文件uwsgi.ini
[uwsgi]
http = :9000
# the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /home/ubuntu/midware
# Django's wsgi file
wsgi-file = midware/wsgi.py
# maximum number of worker processes
processes = 4
# thread numbers startched in each worker process
threads = 2
# monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum = true 用uwsgi啟動Django
ubuntu@ubuntu:~/midware$ uwsgi uwsgi.ini
?用uwsgitop監控
安裝uwsgitop
sudo pip3 install uwsgitop 進行監控
uwsgitop :9191 #9191是配置文件中的監控進行127.0.0.1:9191 安裝Nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start # start nginx
sudo /etc/init.d/nginx restart # restart nginx
為你的項目生成Nginx配置文件
You will need the?uwsgi_params?file, which is available in the?nginx?directory of the uWSGI distribution, or from?https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
Copy it into your project directory. In a moment we will tell?nginx?to refer to it.
Now create a file called mysite_nginx.conf, and put this in it:
# uwsgi_params #把這個文件從/etc/nginx/拷貝到項目midware下。
[uwsgi]
http = :9000
# the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /home/ubuntu/midware
# Django's wsgi file
wsgi-file = midware/wsgi.py #每個Django項目都會有一個名為wsgi.py的文件。
# maximum number of worker processes
processes = 4
# thread numbers startched in each worker process
threads = 2
# monitor uwsgi status
stats = 127.0.0.1:9191 #uwsgitop監控的就是它
# clear environment on exit
vacuum = true
文件mysite_nginx.conf # 放到/etc/nginx/sites-enabled下,或者放到項目midware下,然后鏈接到/etc/nginx/sites-enabled(建立短鏈接ln -s mysite_nginx.conf /etc/nginx/sites-enabled/)# mysite_nginx.conf# the upstream component nginx needs to connect to
upstream django {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 127.0.0.1:8001; # for a web port socket (we'll use this first) #uwsgi.ini中的the local unix socket(socket = 127.0.0.1:8001)
}# configuration of the server
server {# the port your site will be served onlisten 8000; #用戶訪問的端口# the domain name it will serve forserver_name .example.com; # substitute your machine's IP address or FQDN #服務器名稱charset utf-8;# max upload sizeclient_max_body_size 75M; # adjust to taste #用戶請求最大為75M# Django medialocation /media {alias /path/to/your/mysite/media; # your Django project's media files - amend as required}location /static {alias /home/ubuntu/midware/static; # your Django project's static files - amend as required #靜態文件路徑}# Finally, send all non-media requests to the Django server.location / {uwsgi_pass django;include /home/ubuntu/midware/uwsgi_params; # the uwsgi_params file you installed #把這個文件放到項目目錄,或者/etc/nginx/sites-enabled/uwsgi_params}
} 啟動uwsgi、重啟Nginx
...
Deploying static files
Before running?nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding:
# 文件settings.py
DEBUG = False #生成環境中不能用DEBUG模式
STATIC_ROOT = os.path.join(BASE_DIR, "all_tatic_files")
?#把所有靜態文件放到一個文件夾下,注意不要與"static"重名,否則會被覆蓋。 ?
?最后執行
python manage.py collectstatic #合并靜態文件
不要忘了,修改mysite_nginx.conf中的靜態文件配置 啟動uwsgi、重啟Nginx
?
轉載于:https://www.cnblogs.com/yangxiaoling/p/6831530.html
總結
以上是生活随笔為你收集整理的xshell连接Linux、ngix部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第五人格在什么场景下要用什么求生者
- 下一篇: 创维电视多少钱啊?