1.安裝Nginx
sudo apt-get install nginx #安裝
在Nginx的安裝目錄下找到default配置文件進行設置:
server {
listen 9000 default_server;
listen [::]:9000 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9006;
}
location /static {
root / ;
}
}
參數 | 說明 |
---|---|
listen 9000 | 監聽的端口(未來訪問的端口,可以自己修改) |
root | 網頁路徑 |
server_name | 服務名 |
location / | 管理uwsgi的參數 |
location /static | 靜態文件的目錄,例如自己網頁的 js image html |
其中 uwsgi_pass 127.0.0.1:9006; 參數是未來與uwsgi通信的地址與端口,必須與後面項目總uwsgi的配置相同
啓動Nginx:
/etc/init.d/nginx start #啓動
/etc/init.d/nginx stop #關閉
/etc/init.d/nginx restart #重啓
2.安裝uwsgi
pip install uwsgi
安裝之後新建一個test.py文件進行測試
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
運行test.py文件
uwsgi --http-socket :9000 --plugin python --wsgi-file test.py
控制檯 ctrl+c可退出運行
運行成功,看到hello world表示uwsgi安裝成功
3.接下來開始部署自己的django項目
首先
修改自己項目中的wsgi.py文件,添加兩行代碼
import sys
sys.path.append("/var/www/django_app_server/app_server/")
其中sys.path.append爲你自己項目在當前系統中路進,請自行修改
然後
在自己項目manage.py同級目錄上添加uwsgi配置文件,本例取名爲app_server_uwsgi.ini,可以自行取名
可以使用linux創建文件的命令
touch app_server_uwsgi.ini
具體配置
# app_server_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = 127.0.0.1:9006
# the base directory (full path)
chdir = /var/www/django_app_server/app_server
# Django s wsgi file
module = app_server.wsgi
# the virtualenv (full path)
home = /root/SmartCommunity/SmartCommunity-ENV/vir1
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
plugins=python
參數 | 說明 |
---|---|
socket | 與nginx通信的地址和端口 |
chdir | 自己項目所在的系統路徑 |
module | wsgi文件所在的相對於項目的路徑 |
home | 運行django項目的虛擬環境路徑 |
processes | 進程數 |
其中 socket 要與nginx中配置的uwsgi_pass參數相同,nginx需要通過該地址將一些動態請求交給uwsgi去處理。
4.運行
在自己django項目的app_server_uwsgi.ini配置文件下運行uwsgi
uwsgi --ini app_server_uwsgi.ini
運行nginx
sudo service nginx start
現在就可以訪問到我們部署上去的項目了