ubuntu18上使用nginx+uwsgi部署django1.11.8
版本請對號入座
- 如果你在調試過程中,設置失敗,建議先行卸載nginx再重裝
可能在調試中亂改了源文件的代碼,所以建議重裝再進行調試
# 卸載
sudo apt-get --purge remove nginx
# 安裝
sudo apt-get update
sudo apt-get install nginx
#測試nginx配置是否正確
sudo nginx -t
# 重啓nginx,每次改完nginx參數
sudo service nginx restart
- 安裝uwsgi
#對於 Python3.x 版本:
sudo apt-get install python3-dev
sudo apt-get install python3-pip
sudo pip3 install uwsgi
- uwsgi.ini參數的設置
# 這個是項目的絕對路徑圖,根據對應的項目路徑修改
/home/yice/PycharmProjects/YC_Ops
# 任意路徑的都可以創建 uwsgi.ini 文件
sudo vim uwsgi.ini
#----------------------------uwsgi.ini內容起始線--------------------------------------
# 以下是 uwsgi.ini的文件內容
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# 和nginx的文件的聯繫端口
socket=:8005
# the base directory (full path) 項目的局對路徑
chdir = /home/yice/PycharmProjects/YC_Ops
# Django's wsgi file
# 注意這裏是要設置 項目名稱.wsgi
module = YC_Ops.wsgi
# the virtualenv (full path) 這裏是虛擬環境的設置
# home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes 最大進程數
processes = 10
# the socket (use the full path to be safe 絕對路徑的.sock !!注意這裏是和nginx關聯的關鍵
socket = /home/yice/PycharmProjects/YC_Ops.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
#-----------------------uwsgi.ini內容分割線-------------------------------------
- nginx參數的設置
!!!注意,由於版本的不一樣,nginx的設置有所區別,導致啓動不成功!
!!!這裏的版本是ubuntu的nginx,我們要修改的文件只有 /etc/nginx/sites-enabled/default
!!!只需要改 /etc/nginx/sites-enabled/default,如已改過其他文件,建議重裝nginx
以下是default的文件內容設置,對比下面,新增源文件中不存在的部分即可
21 server {
22 listen 80 default_server;
23 listen [::]:80 default_server;
24
25 # SSL configuration
26 #
27 # listen 443 ssl default_server;
28 # listen [::]:443 ssl default_server;
29 #
30 # Note: You should disable gzip for SSL traffic.
31 # See: https://bugs.debian.org/773332
32 #
33 # Read up on ssl_ciphers to ensure a secure configuration.
34 # See: https://bugs.debian.org/765782
35 #
36 # Self signed certs generated by the ssl-cert package
37 # Don't use them in a production server!
38 #
39 # include snippets/snakeoil.conf;
40
41 root /var/www/html;
42
43 # Add index.php to the list if you are using PHP
44 index index.html index.htm index.nginx-debian.html;
45
46 server_name localhost; # 如果綁定了域名,server_name 域名/服務器IP,本地調試則用localhost
47
48 location / {
49 # First attempt to serve request as file, then
50 # as directory, then fall back to displaying a 404.
51 include uwsgi_params;
52 uwsgi_pass 127.0.0.1:8005; # 這行端口對應uwsgi的端口
53 uwsgi_read_timeout 30; # 設置超時時間,設置太短,會導致異步阻塞報錯
54 # try_files $uri $uri/ =404; 把該行註釋掉,否則訪問會出現404
55 }
56
57 location /static {
58
59 expires 30d;
60 autoindex on;
61 add_header Cache-Control private;
62 alias /home/yice/PycharmProjects/YC_Ops/static/; # 對應設置靜態文件的讀取路徑,否則項目無法讀取靜態文件
63 }
64
修改以上的nginx的default的文件後記得要重啓nginx, sudo /etc/init.d/nginx restart
-
啓動項目
去到剛剛設置的uwsgi.ini路徑
sudo --ini uwsgi.ini -
大功告成!