1、首先使用yum命令安裝nginx
yum install nginx
2、pip install uwsgi 安裝uwsgi
這裏要說明一下,nginx負責調用靜態資源,如css等,而uwsgi負責動態服務器的調用,如果不安裝nginx,直接使用uwsgi是可以訪問網站的,但是無法加載css樣式
3、安裝好之後可以新建一個test文件進行測試,如下圖,網址是https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
4、如果出現HelloWorld字樣說明成功
5、對nginx進行配置,進入nginx.conf文件中,如下圖所示
注意,由於使用的是centos系統,因此如果爲了方便,就直接在nginx.conf中改了。這裏也可以進入conf.d文件中新建.conf文件,因爲nginx.conf文件中有這樣一句話
這裏就直接修改nginx.conf了,修改server{}中的內容,如圖所示
文字版:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /static {
alias /home/res_sys/collected_static;
}
location /media {
alias /home/res_sys/collected_static;
}
location / {
uwsgi_pass 127.0.0.1:8000;
include /etc/nginx/uwsgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
注意:其中的 location /static 表示存放靜態文件的地方,location /media 表示存放上傳文件的地方,正常情況下不是一個文件夾,我這裏由於項目歷史原因,無奈。location / {}中,第二行前面的路徑可以改,但最後必須有uwsgi_params
6、在項目最上級的同級目錄中新建一個文件夾,我這裏是res_sys_uwsgi,用於存放與uwsgi相關的配置
這裏的res_sys是我的項目
在其中新建.ini文件,文字隨意,配置如下
文字版:
[uwsgi]
chdir = /home/res_sys
module = res_sys.wsgi:application
master = True
process = 4
harakiri=60
max-requests = 5000
socket = 127.0.0.1:8000
uid = 1000
gid = 2000
pidfile = /home/res_sys_uwsgi/master.pid
daemonize = /home/res_sys_uwsgi/res_sys.log
vacuum = True
7、使用
uwsgi --ini /home/res_sys_uwgi/res_sys.ini
命令啓動uwsgi,同時重啓nginx
就可以了
最後附上b站教程鏈接
https://www.bilibili.com/video/av31456425?from=search&seid=8076264223498171412