[Python自學] Nginx+uWSGI啟動django項目


一、uWSGI命令運行Django項目

1.Django配置

要讓所有IP都能訪問,需要配置ALLOWED_HOST:

# settings

ALLOWED_HOSTS = ["*"]

2.使用uWSGI啟動django應用(踩坑)

在/root/admin_demo目錄(django項目為admin_demo,放在/root下)下執行命令:

(venv_fb) [root@centos-venv-fb admin_demo]# uwsgi --http :8001 --static-map /static=/root/admin_demo/static  --module admin_demo.wsgi 

--http表示使用http協議。

:8001表示監聽端口。

--module表示django項目中的wsgi.py模塊。

--static-map表示靜態資源請求,映射到實際的目錄。否則無法返回靜態資源。(坑)

3.uWSGI熱加載

在uwsgi命令中使用--py-autoreload=1參數來開啟熱加載:

(venv_fb) [root@centos-venv-fb admin_demo]# uwsgi --http :8001 --static-map /static=/root/admin_demo/static  --module admin_demo.wsgi --py-autoreload=1

當開啟了熱加載后,我們如果實時修改了后端python代碼,uwsgi會自動幫我們加載,服務不會停。

二、uWSGI配置文件運行項目

1.創建一個uwsgi.ini配置文件

touch /etc/uwsgi.ini

寫入如下配置信息:

[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/admin_demo # 指定django項目的絕對路徑
# Django's wsgi file
module = admin_demo.wsgi  # wsgi.py文件,這里寫相對路徑,chdir為相對路徑
# the virtualenv (full path)
home = /root/.virtualenvs/venv_fb # python虛擬環境home目錄(絕對路徑),使用cdvirtualenv來查看
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4  # 指定uwsgi啟動的進程數,視機器配置來定
# the socket (use the full path to be safe
# socket = 0.0.0.0:8000  # uwsgi啟動一個socket連接,當使用nginx+uwsgi的時候,使用socket參數
http = 0.0.0.0:8000 # uwsgi啟動一個socket連接,當不使用nginx,直接使用uwsgi時,使用該參數
# ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true

2.使用uwsgi.ini配置文件啟動

將uwsgi.ini配置文件拷貝到django項目目錄下:

(venv_fb) [root@centos-venv-fb etc]# cp /etc/uwsgi.ini /root/admin_demo/

啟動項目:

(venv_fb) [root@centos-venv-fb admin_demo]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.18 (64bit) on [Thu Jan 30 21:45:19 2020] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 30 January 2020 10:24:09
os: Linux-3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019
nodename: centos-venv-fb
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /root/admin_demo
detected binary path: /root/.virtualenvs/venv_fb/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /root/admin_demo
your processes number limit is 14988
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 0.0.0.0:8000 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:44810 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.7.0 (default, Jun 28 2018, 13:15:42)  [GCC 7.2.0]
Set PythonHome to /root/.virtualenvs/venv_fb
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x209b820
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 364520 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
/root/admin_demo/static/
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x209b820 pid: 3382 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3382)
spawned uWSGI worker 1 (pid: 3383, cores: 1) spawned uWSGI worker 2 (pid: 3384, cores: 1) spawned uWSGI worker 3 (pid: 3385, cores: 1) spawned uWSGI worker 4 (pid: 3386, cores: 1)
spawned uWSGI http 1 (pid: 3387)

解決uwsgi找不到靜態文件的問題,在uwsgi.ini配置文件中加上:

static-map = /static=/root/admin_demo/static

讓uwsgi在后台運行,在uwsgi.ini配置文件中加上:

daemonize = yes

再來運行:

(venv_fb) [root@centos-venv-fb admin_demo]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => /root/admin_demo/static

查看進程信息:

(venv_fb) [root@centos-venv-fb admin_demo]# ps -ef | grep uwsgi
root       3405      1  0 21:50 ?        00:00:00 uwsgi --ini uwsgi.ini
root       3406   3405  0 21:50 ?        00:00:00 uwsgi --ini uwsgi.ini
root       3407   3405  0 21:50 ?        00:00:00 uwsgi --ini uwsgi.ini
root       3408   3405  0 21:50 ?        00:00:00 uwsgi --ini uwsgi.ini
root       3409   3405  0 21:50 ?        00:00:00 uwsgi --ini uwsgi.ini
root       3410   3405  0 21:50 ?        00:00:00 uwsgi --ini uwsgi.ini
root       3412   2561  0 21:51 pts/3    00:00:00 grep --color=auto uwsgi

三、Nginx+uWSGI

在前面的一二節中,我們已經可以使用uWSGI來啟動Django項目。

下面我們配置Nginx作為反向代理服務器。

1.准備工作

1)安裝nginx服務器

參考:[Linux系統] (8)Nginx

2)確認uWSGI服務監聽端口

192.168.1.191:8000

3)去除uWSGI的一些配置

由於靜態資源請求,我們可以直接讓Nginx來處理,所以可以將uWSGI中的靜態資源目錄映射去除。

# 去除
static-map = /static=/root/admin_demo/static

如果使用supervisor來管理uwsgi進程,則將daemonize配置項也去除:

# 去除
daemonize = yes

4)修改uwsgi的重要配置

[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/admin_demo
# Django's wsgi file
module = admin_demo.wsgi
# the virtualenv (full path)
home = /root/.virtualenvs/venv_fb
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# the socket (use the full path to be safe
socket = 0.0.0.0:8000 #http = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum = true
#static-map = /static=/root/admin_demo/static
#daemonize=yes

非常重要:當我們要使用nginx+uwsgi時,需要使用socket配置項。而單獨使用uwsgi時,則使用http配置項。

2.配置Nginx(踩坑)

1)配置nginx

Nginx安裝在uwsgi同服務器。IP地址都為192.168.1.191。

Nginx安裝目錄為/opt/nginx。

vi /opt/nginx/conf/nginx.conf

修改其中部分配置:

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    #access_log  "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G"  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen 80;  # nginx監聽端口,即對外暴露端口
        server_name 192.168.1.191;  # 本機IP

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #access_log  "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G"  main;

        location / {  # 當我們訪問192.168.1.191:80時,請求會轉給uwsgi監聽的8000端口
            include /opt/nginx/conf/uwsgi_params;  # 包含uwsgi_params中的配置(固定寫)
            uwsgi_pass 192.168.1.191:8000;  # uwsgi服務器綁定地址和端口
        }


        location /static/ {
            alias /root/admin_demo/static/;  # 注意,這里使用nginx直接提供靜態資源響應(uwsgi只做動態資源響應)
        }
    }  # server end
}  http end

3)解決靜態資源訪問403錯誤

 

我們訪問頁面時,動態資源能夠正常拿到,但靜態資源,例如css、js、圖片等資源都無法請求。

 

這是因為在Nginx的配置文件中,雖然我們配置了/static的訪問路徑:

location /static/ {
    alias /root/admin_demo/static/;
}

但是Nginx啟動時使用的用戶叫nobody,可以查看進程看到:

[root@centos-venv-fb admin_demo]# ps -ef | grep nginx
root       8255      1  0 08:08 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
nobody  8256   8255  0 08:08 ?        00:00:00 nginx: worker process
root       8258   7718  0 08:08 pts/2    00:00:00 grep --color=auto nginx

而nobody對靜態資源文件沒有訪問權限。所以,我們在nginx的配置文件中將user修改為root即可:

#user  nobody;
user root;

關於默認的nobody用戶,可以參考:http://alisa365.com/linux/104.html 中的說明。

重新載入或重新啟動Nginx后,可以正常訪問靜態資源:

systemctl restart nginx
systemctl reload nginx

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM