使用nginx和uwsgi將django項目部署到云服務器

一. 簡介

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.

A Web Server Gateway Interface - WSGI - does this job. WSGI is a Python standard.

二. 組件概覽

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

nginx充當the web server,響應處理靜態(tài)資源的請求,并將動態(tài)資源的請求通過socket以uwsgi協(xié)議轉發(fā)給uWSG處理,uWSG則是運行django項目的web服務。

三. 具體步驟

逐步搭建,方便排查問題。以下步驟若未特殊聲明,則均在云服務器上執(zhí)行。

1. virtualenv

方法一:
安裝virtualenv

sudo apt-get install virtualenv

創(chuàng)建和激活virtualenv

virtualenv env --no-site-packages --python=python3
source env/bin/activate

--no-site-packages 表示不使用系統(tǒng)已經安裝的第三方庫
--python指定項目用到的python版本

方法二:
使用python3內置venv

$ python3 -m venv envpy3
2. 測試uwsgi

將uwsgi安裝到virtualenv中。激活virtualenv再執(zhí)行

pip install uwsgi

新建文件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

uwsgi --http :8000 --wsgi-file test.py

訪問127.0.0.1:8000查看是否成功,若成功說明以下組件連接成功

the web client <-> uWSGI <-> Python
3. 測試django項目

在本機django的項目根目錄執(zhí)行以下命令,生成項目依賴清單requirements.txt

pip freeze > requirements.txt

用scp命令將django項目傳到云服務器

 scp [option] <localfile> <username>@<host>:<remote-file-location>
示例:
scp -r /home/user/hello_django user@x.x.x.x:/home/user/

激活virtualenv,切換到項目根目錄下安裝依賴

pip install -r requirements.txt

運行django

python manage.py runserver 0.0.0.0:8000

如果運行成功,再測試使用uwsgi運行django

uwsgi --http :8000 --module hello_django.wsgi

--module 指定django模塊
如果成功,說明以下組件連接成功

the web client <-> uWSGI <-> Django
4. 測試nginx

安裝

sudo apt-get install nginx

啟動

sudo systemctl start nginx

nginx默認監(jiān)聽80端口,所以可以訪問127.0.0.1:80測試是否成功。
nginx安裝之后,會默認創(chuàng)建www-data用戶和組,并以www-data身份運行nginx(可以在/etc/nginx/nginx.conf首行查看或修改)。為了避免文件訪問權限問題,修改django項目的用戶組

sudo chown -R :www-data hello_django
5. 部署靜態(tài)資源

在django項目的settings.py文件添加

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然后執(zhí)行

python manage.py collectstatic

即可將項目的靜態(tài)資源統(tǒng)一收集到項目根目錄下的static文件夾中。

在/etc/nginx/sites-available/目錄下,新建hello_django.conf并寫入以下內容,將static路徑替換成上面的項目的static目錄路徑

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste
   
    # 開啟gzip
    gzip on;

    # 啟用gzip壓縮的最小文件,小于設置值的文件將不會壓縮
    gzip_min_length 1k;

    # gzip 壓縮級別,1-10,數(shù)字越大壓縮的越好,也越占用CPU時間,后面會有詳細說明
    gzip_comp_level 2;

    # 進行壓縮的文件類型。javascript有多種形式。其中的值可以在 mime.types 文件中找到。
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

    # 是否在http header中添加Vary: Accept-Encoding,建議開啟
    gzip_vary on;

    # 禁用IE 6 gzip
    gzip_disable "MSIE [1-6]\.";

    # Django media
#    location /media  {
#        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
#    expires 30d;
#    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
        # enable cache 
        expires 30d; 
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

并創(chuàng)建軟鏈接到/etc/nignx/sites-enabled目錄

sudo ln -s /etc/nginx/sites-available/hello_django.conf /etc/nginx/sites-enabled/

重加載nginx

nginx -s reload

然后訪問127.0.0.1:8000/static/xxx/xxx.png,若成功說明靜態(tài)資源部署成功。
若訪問失敗可查看nginx錯誤日志(/var/log/nginx/error.log),Permission denied問題請將static目錄加入到www-data用戶組。

6. 連接nginx和uwsgi

以--socket方式運行django項目,并將端口改成nginx中配置的8001端口

uwsgi --socket :8001 --module hello_django.wsgi 

然后訪問127.0.0.1:8000查看測試nginx的動態(tài)轉發(fā)是否成功。
若成功,則所有組件的連接都成功了

the web client <-> the web server <-> the socket <-> uWSGI <-> Python
7. 使用Unix sockets替換uwsgi的端口

nignx和uwsgi間通過socket端口連接雖然簡單,但是不是最佳方式,應改用Unix sockets。
修改hello_django.conf

server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)

指定使用Unix sockets時創(chuàng)建的sock文件,如

server unix:///home/www-data/hello_django/hello_django.sock; # for a file socket

然后改以unit socket方式運行django

uwsgi --socket /home/www-data/hello_django/hello_django.sock --module hello_django.wsgi --chmod-socket=666

--chmod-socket是為了避免hello_django.sock文件的權限問題
然后訪問查看是否成功。

8. 以.ini文件方式運行uwsgi

創(chuàng)建文件 hello_django_uwsgi.ini

[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.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
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

然后指定文件啟動uwsgi

uwsgi --ini hello_django_uwsgi.ini # the --ini option is used to specify a file
9. 將uwsgi配置成service并設置開機自啟

退出virtualenv

deactivate

將uwsgi安裝到系統(tǒng)環(huán)境

sudo pip3 install uwsgi

然后用系統(tǒng)環(huán)境的uwsgi測試

uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

在/etc/systemd/system目錄,新建文件hello_django.service

[Unit]
Description=hello django uwsgi web service
After=syslog.target

[Service]
Type=simple
ExecStart= /usr/local/bin/uwsgi --ini /path/to/your/hello_django.ini
Restart=always
StandardError=syslog

[Install]
WantedBy=multi-user.target

配置完成后,reload一下systemd,即成功添加了hello_django的service

$ sudo systemctl daemon-reload

啟動

systemclt start hello_django

設置開機自啟

systemctl enable hello_django

systemd還可以監(jiān)控服務進程,代替supervisor第三方庫。

10. 其他

以Emperor模式啟動uwsgi,該模式下,會監(jiān)視uwsgi的配置文件,當配置文件被修改后,會自動重啟uwsgi下對應的web服務。

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

啟動

sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

在hello_django.service的配置文件中修改啟動的shell命令

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log

參考文章
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容