ubantu16.04上部署nginx+uwsgi+django,supervisor啓動

首先要確保本身的django項目能啓動,python manage.py runserver。html

首先安裝uwsgi,在終端輸入python

sudo pip install uwsgi

uwsgi用pip安裝是最好的。nginx

pip源用豆瓣源,豆瓣源很好很騷。python的一些國外第三方用豆瓣源下載會很舒服。django

接下來簡單配置uwsgi+django,建立一個ini文件,test1.ini,bash

[uwsgi]
http-socket    = :8080
plugin    = python
wsgi-file = /home/sora/PycharmProjects/shangshuSevice/project/wsgi.py
process   = 3

OK,在該文件的目錄下運行。uwsgi test1.ini。127.0.0.1:8080看看。socket

下面是nginx部分,在終端輸入學習

sudo apt-get install nginx

安裝完後,要nginx+uwsgi+django了,測試

前面的test1.ini沒用了,從新建立一個ini文件,test2.ini,rest

[uwsgi]
chdir=/home/sora/PycharmProjects/shangshuSevice
module=project.wsgi
socket=127.0.0.1:9090
processes = 4
pidfile=/tmp/zp-master.pid
master=True
vacuum=True

uwsgi test2.ini運行,127.0.0.1:9090查看,和以前測試的結果不一樣,不急code

前面的是http-socket,這裏是socket,差了一個http,

接下來是配置nginx,在終端輸入

cd /etc/nginx/site-enabled/
sudo nano nginx.conf

編輯該conf文件,

server {
        listen   8000;
        server_name localhost;

        charset utf-8;

	client_max_body_size 75M;

        access_log  /home/sora/project_log/shangshuSevice/access.log;
	error_log /home/sora/project_log/shangshuSevice/error.log;

        location / {
         include        uwsgi_params;
         uwsgi_pass     127.0.0.1:9090;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #

        location /static { 
            alias  /home/sora/PycharmProjects/shangshuSevice/static/;
        }

       # location /media/ {
       #     alias  /home/work/src/sites/testdjango1/testdjango/public/media/;
       # }
    }

access_log和error_log的文件路徑本身決定,include中uwsgi_params指向的文件是/etc/nginx下的,uwsgi_pass 指向的uwsgi配置的ini文件中的接口地址。
static和media的路徑是指向django項目的static和media文件夾。

喲系,接下來重啓nginx,

service nginx restart

檢驗結果,localhost:8000,項目能運行的就沒問題啦。

接下來是supervisor,這邊工頭和我說用apt-get安裝的話會省事不少,後續步驟會節省。然而他和我說以前我已經安裝完配置好了。。。。這是很騷的,我用的pip安裝的

sudo pip install supervisor

安裝後,在終端

cd /etc/
echo_supervisor_conf > supervisor.conf

若是沒法執行第二個命令,能夠執行

echo_supervisor_conf

複製顯示出來的全部內容,而後建立一個新的文件supervisor.conf,粘貼進去,ok。

制定supervisor的啓動配置文件

supervisord -c /etc/supervisord.conf

編輯sudo nano supervisor.conf,添加

[program:sss]
command = uwsgi /home/sora/PycharmProjects/shangshuSevice/ss_uwsgi.ini
stopsignal=QUIT
autostart=true
autorestart=true
stdout_logfile=/home/sora/project_log/shangshuSevice/supervisor_stdout.log
stderr_logfile=/home/sora/project_log/shangshuSevice/supervisor_stderr.log
redirect_stderr=true

這裏program是你啓動的項目簡稱,log的路徑本身決定,第二行的command是你uwsgi啓動ini的命令。

sudo supervisorctl update

這裏說下,supervisor在運行期間,若是在supervisor.conf文件中修改了內容,那麼就須要update下。

sudo supervisorctl status

項目在RUNNING

sudo supervisorctl stop sss

sss:stopped,在看下狀態status,STOPPED,localhost:8000看看,失敗就對了。在啓動

sudo supervisorctl start sss

查看status,RUNNING。到這裏就基本ok了。

開機啓動supervisor來啓動項目的話,在下就弄個超級簡單腳本,在/etc/init.d文件夾下建立文件

#!/bin/bash
supervisord -c /etc/supervisord.conf
exit 0

而後

update-rc.d servicetest defaults

重啓試試。

nginx,uwsgi,supervisor我也是剛開始接觸學習,不對的地方摸見怪。

相關文章
相關標籤/搜索