Django + nginx + uwsgi 部署(linux)


首先確保你的系統(tǒng)有安裝django,uwsgi,nginx,具體如何安裝網(wǎng)上的教程一大把,這里我就寫幾個自己遇到的問題


* 首先

在測試uwsgi是否工作是我們都會寫一個xxx.py文件,如下

def application(env, start_responce):
    start_responce("200 OK", [('Content-Type', 'text/html')])
    return [b"Hello World"] # python3
    # return ["Hello World"] # python2

運行: uwsgi --http :8000 --wsgi-file xxx.py 后,登錄http:127.0.0.1:8000


* 再者

就是uwsgi.ini 和 nginx.conf 的配置了
下面我貼上自己的配置文件:

uwsgi.ini(下面xxx為你的django項目名, you是你自己的/home/you/)
[uwsgi]
# Django-related settings
# socket = :8000
socket = /home/you/xxx/xxx.sock
chmod-socket = 666
uid = you
gid = you
# the base directory (full path)
chdir = /home/zbl/xxx
# Django s wsgi file
module = xxx.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# clear environment on exit
vacuum = true

xxx_nginx.conf(下面xxx為你的django項目名, you是你自己的/home/you/)
# xxx_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///home/you/xxx/xxx.sock; # for a file socket
    # server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name www.example.com; # 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 /path/to/your/mysite/media;  # your Django project's media files - amend as required
    # }

    location /static {
        alias /home/you/xxx/static; # your Django project's static files - amend as required
    }

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

最后

  • 就是要注意上面所有路徑的權(quán)限,尤其是工程目錄權(quán)限
nginx error.log 日志錯誤信息    .sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: www.xxxxx.com

vim /etc/nginx/nginx.conf   -> user root

-rw-rw-r--  1 you you  664 12月 20 15:21 uwsgi_params
-rw-r--r--  1 you you 1.1K 12月 20 16:14 you_nginx.conf
  • 127.0.0.1 www.example.com 加入/etc/hosts
  • 權(quán)限運行 ln -s /you/xxx_nginx.conf /etc/nginx/sites-enabled/nginx
  • cp /etc/nginx/uwsgi_params /home/you/xxx
  • 重啟命令 /etc/init.d/nginx restart
  • uwsgi 命令 uwsgi uwsgi.ini 之后訪問 http:127.0.0.1應該出現(xiàn)的就是你項目的首頁
  • uwsgi 安裝: ***sudo apt-get install libpcre3 libpcre3-dev ***
    sudo pip install uwsgi
  • nginx 安裝(網(wǎng)上教程需要安裝許多依賴): ***sudo apt-get install nginx *** 通過訪問 http://localhost:80 測試

自己遇到的一些問題,寫的不好,還望指教
By: single430

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容