Django3.0 + nginx + uwsgi 部署


CentOS7.6 下部署Django3.0應用,使用nginx+uwsgi部署:

1. uwsgi部署

pip install uwsgi

在項目的根目錄中,新建文件夾 conf, 然后進入conf文件夾,並新建文件 uwsgi.ini, 內容如下:

# mysite_uwsgi.ini file
    [uwsgi]

    # Django-related settings
    # the base directory (full path)
    chdir=/root/EduOnline
    # Django's wsgi file
    module=EduOnline.wsgi
    # the virtualenv (full path)

    # process-related settings
    # master
    master=True
    # maximum number of worker processes
    processes=5
    # the socket (use the full path to be safe
    socket=0.0.0.0:8001
    # http=0.0.0.0:8001
    # ... with appropriate permissions - may be needed
    # chmod-socket    = 664
    # clear environment on exit
    vacuum=true
    virtualenv =/root/.virtualenvs/eduonline

    logto=/tmp/mylog.log

  

首先進入文件的根目錄要使用命令 uwsgi --http :8000 --module EduOnline.wsgi,然后通過瀏覽器訪問:127.0.0.1:8000 是否能訪問首頁,如果能訪問首頁,則說明uwsgi能訪問成功,然后通過上面文件的配置,將socket一行注釋掉,將http一行釋放掉,然后保存並退出,再執行以下命令:

uwsgi -i uwsgi.ini

然后再通過瀏覽器訪問,127.0.0.1:8000,如果能訪問到首頁,則說明配置文件成功,那么這時如果要用nginx來訪問,則需要把socket一行釋放掉,http一行注釋掉即可,然后保存並執行uwsgi -i uwsgi.ini, 然后查看端口使用處於監聽狀態:netstat -ntulp |grep 8000, 如果有,則表示這個文件配置成功了

 

2. 配置nginx

安裝nginx步驟

a.  Add Nginx Repository

sudo yum install epel-release

b.  Install Nginx

sudo yum install nginx

c. Start Nginx

sudo systemctl start nginx

d. 設置防火牆

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

最后訪問你的ip地址:
http://server_domain_name_or_IP/

進入conf.d文件夾

cd /etc/nginx/conf.d

然后創建文件uc_ningx.conf, 代碼如下:

# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 0.0.0.0: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 127.0.0.1 0.0.0.0 47.104.226.120; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
    alias /root/EduOnline/media;  # 指向django的media目錄
}

location /static {
    alias /root/EduOnline/static; # 指向django的static目錄
}

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

  保存並退出,然后重啟nginx服務:

 service nginx restart

然后輸入網址:http://127.0.0.1:8000 如果能訪問到首頁,則表示nginx配置成功

如果訪問出現 css和js樣式不對,以及報403 Fobbien 的錯誤,則表示nginx user權限不夠,需要設置nginx.conf的頭部user: root

 


免責聲明!

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



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