Centos7+Django+Uwsgi+Nginx+pyenv+virtualenv实现生产环境部署

本文目标

实现不同python版本(python2.7、python3.5、python3.6共存)、不同运行环境隔离(项目1、项目2隔离)

相关环境

系统:Centos 7.3
运行环境:Python2、3
框架:Django
Django+Uwsgi+Nginx:实现Web访问
pyenv:实现多个Python版本隔离
virtualenv:实现多个Django项目运行环境隔离

安装步骤

pyenv+virtualenv

supervisor

pip install supervisor
echo_supervisord_conf > /etc/supervisord.conf

supervisor开机启动

# vim /usr/lib/systemd/system/supervisord.service
[Unit] 
Description=Supervisor daemon

[Service] 
Type=forking 
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf 
ExecStop=/usr/bin/supervisorctl shutdown 
ExecReload=/usr/bin/supervisorctl reload 
KillMode=process 
Restart=on-failure 
RestartSec=42s

[Install] 
WantedBy=multi-user.target

设置开机启动

systemctl enable supervisord
systemctl is-enabled supervisord

nginx

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
wget https://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make
make install

开机启动

# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

修改权限

$ sudo chmod +x /usr/lib/systemd/system/nginx.service

设置

systemctl enable nginx
systemctl is-enabled nginx

nginx相关命令

$ sudo systemctl start nginx.service
$ sudo systemctl reload nginx.service
$ sudo systemctl restart nginx.service
$ sudo systemctl stop nginx.service
$ journalctl -f -u nginx.service # nginx日志

环境变量设置

$ echo 'PATH=$PATH:/usr/local/nginx/sbin/' >> /etc/profile
$ source /etc/profile

www用户创建

groupadd www
useradd -g www -m www

uwsgi

yum install python-devel.x86_64 # uwsgi相关,否则安装uwsgi出错
pyenv activate project_01 # 进入
pip install uwsgi

运行

/data/.pyenv/versions/3.6.6/envs/project_01/bin/uwsgi --uid www --gid www --ini /data/django_project/project_01/uwsgi.ini

uwsgi.ini

[uwsgi]
buffer-size = 32768
chdir = /data/walle/releases_weixin_mini_program/weixin_mini_program/20180815-154053
env=CURRENT_DIR=/data/django_project/project_01
enable-threads=true
chmod-socket = 664
master = true
max-requests = 6000
module = project_01.wsgi
post-buffering = 65536
processes = 2
socket = 127.0.0.1:7092
threads = 2
vacuum = true

nginx配置文件:

server {
    listen 80;
    server_name a.com;
    error_log /usr/local/nginx/logs/error.log;
    access_log /usr/local/nginx/logs/access.log;

    location / {
        uwsgi_pass 127.0.0.1:7092;
        include /usr/local/nginx/conf/uwsgi_params;
    }

    location /static/ {
        root /data/django_project/project_01;
    }

    location /upload/ {
        root /data/django_project/project_01;
    }
}

nginx日志目录:/usr/local/nginx/logs/

supervisor配置文件:

[program:project_01]
command=/data/.pyenv/versions/project_01/bin/uwsgi  --uid www --gid www --ini /data/django_project/project_01/uwsgi.ini
directory=/data/django_project/project_01
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
environment=CURRENT_DIR="/data/django_project/project_01"
;logto = /tmp/project_01.log

环境变量也可以写在uwsgi项目配置文件中:

env=CURRENT_DIR="/data/django_project/project_01"

问题

服务器开机启动nginxsystemctl start nginx.service,过一会儿进程会崩掉,未知原因

暂时解决:

[program:nginx]
command=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注