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我也是刚开始接触学习,不对的地方摸见怪。

相关文章
相关标签/搜索