腾讯云Centos7+Uwsgi+Nginx+pyenv+virtualenv 的生产环境进行Django2.0部署

一、Python环境相关

1、pyenv + virtualenv可以查看本站 的文章

1、pyenv python版本管理模块
可以直接文章的第二部分 === 》》》》 二、python版本管理模块

2、supervisor

进入虚拟环境,我们的Python环境直接使用虚拟环境,不知道的可以看看第一步的操作

supervisor安装

[root@VM_0_6_centos ~]# pyenv activate envtest3
(envtest3) [root@VM_0_6_centos ~]#
(envtest3) [root@VM_0_6_centos ~]# pip install supervisor
(envtest3) [root@VM_0_6_centos ~]# echo_supervisord_conf > /etc/supervisord.conf

supervisor开机启动

# 新建文件
(envtest3) [root@VM_0_6_centos ~]# 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

设置开机启动

(envtest3) [root@VM_0_6_centos ~]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
(envtest3) [root@VM_0_6_centos ~]# systemctl is-enabled supervisord
enabled

二、MySQL安装以及数据迁移

1、mysql安装

记一次腾讯云标准型SA1 Centos7.2安装MySQL的坑

2、数据迁移

使用Navicat远程连接服务器上的MySQL,然后选中服务器连接中的库名,右键->数据传输,左边选择本地连接和库名,右边选择远程连接和库名开始传输即可

三、本地Django项目迁移

三、安装nginx

1、安装nginx

(envtest3) [root@VM_0_6_centos ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
(envtest3) [root@VM_0_6_centos ~]# wget https://nginx.org/download/nginx-1.14.0.tar.gz
(envtest3) [root@VM_0_6_centos ~]# tar -zxvf nginx-1.14.0.tar.gz
(envtest3) [root@VM_0_6_centos ~]# cd nginx-1.14.0
(envtest3) [root@VM_0_6_centos ~]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
(envtest3) [root@VM_0_6_centos ~]# make
(envtest3) [root@VM_0_6_centos ~]# make install

2、nginx配置文件

nginx默认使用/etc/nginx/nginx.conf文件
nginx.conf内容

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    # 后端Django变量
    upstream django {
        # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
        server 127.0.0.1:8000; # for a web port socket (we'll use this first)
    }

    server {
        listen       80;
        listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        # 原本是用来设置前端目录的,貌似用不上了
        location / {
            root /root/www/vue/dist;
            index index.html;
            try_files $uri $uri/ /index.html;
        }

        # 后端接口转发
        location /api {
            uwsgi_pass  django;
            uwsgi_connect_timeout   3000;
            uwsgi_send_timeout      3000;
            uwsgi_read_timeout      3000;
            uwsgi_param   Host                 $host;
            uwsgi_param   X-Real-IP            $remote_addr;
            uwsgi_param   X-Forwarded-For      $proxy_add_x_forwarded_for;
            uwsgi_param   X-Forwarded-Proto    $http_x_forwarded_proto;
            # uwsgi_params在/etc/nginx/目录下配置文件
            include /etc/nginx/uwsgi_params;
        }
        #后端admin路径
        location ^~ /admin/ {
            uwsgi_pass  django;
            include /etc/nginx/uwsgi_params;
         }

        # Django后端静态文件 需要设置,用来跟vue区分不同的静态文件目录
        #STATIC_URL = '/teststatic/' #主要
        #STATIC_ROOT = os.path.join(BASE_DIR, "teststatic").replace('\\','/')
        # vue的静态目录也需要设置,config目录下index.js中的
        # (生产包)build:{assetsSubDirectory:'vue-static'} 
        #就可以配置location /vue-static {alias /root/www/vue/static;}讲请求转发到vue的静态文件目录
         location /static {
            alias /root/www/leimizhu/static;
         }
        # Django后端媒体静态文件
        location /media {
            uwsgi_pass  django;
            alias /root/www/leimizhu/resources/media;
         }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

3、设置开机启动配置

# 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 /etc/nginx/nginx.conf
# 配置文件
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3、修改权限

(envtest3) [root@VM_0_6_centos nginx-1.14.0]# sudo chmod +x /usr/lib/systemd/system/nginx.service

4、nginx开机启动

(envtest3) [root@VM_0_6_centos nginx-1.14.0]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
(envtest3) [root@VM_0_6_centos nginx-1.14.0]# systemctl is-enabled nginx
enabled

nginx设置完可以直接通过ip访问服务器了,会出现nginx的测试页面

5、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日志
killall -9 nginx  #关闭所有的nginx
##error_log /var/log/nginx/error.log;  配置文件中配置的nginx日志文件
##access_log  /var/log/nginx/access.log  main; 配置文件中配置的nginx日志文件

6、环境变量设置

(envtest3) [root@VM_0_6_centos nginx-1.14.0]# echo 'PATH=$PATH:/usr/local/nginx/sbin/' >> /etc/profile
(envtest3) [root@VM_0_6_centos nginx-1.14.0]# source /etc/profile

7、www用户创建

(envtest3) [root@VM_0_6_centos nginx-1.14.0]# groupadd www
(envtest3) [root@VM_0_6_centos nginx-1.14.0]# useradd -g www -m www

四、uwsgi

[root@VM_0_6_centos ~]# yum install python-devel.x86_64 # uwsgi相关,否则安装uwsgi出错
[root@VM_0_6_centos ~]# pyenv activate envtest3
(envtest3) [root@VM_0_6_centos ~]# pip install uwsgi

创建config目录用来存放配置,并创建文件uwsgi.ini

(envtest3) [root@VM_0_6_centos config]# pwd
/root/config
(envtest3) [root@VM_0_6_centos config]# touch uwsgi.ini
(envtest3) [root@VM_0_6_centos config]# ll
total 0
-rw-r--r-- 1 root root 0 May 13 21:44 uwsgi.ini

初始化uwsgi

(envtest3) [root@VM_0_6_centos config]# /root/.pyenv/versions/envtest3/bin/uwsgi --uid www --gid www --ini /root/config/uwsgi.ini
*** WARNING: Can't find section "uwsgi" in INI configuration file /root/config/uwsgi.ini ***
*** Starting uWSGI 2.0.18 (64bit) on [Mon May 13 21:46:40 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 13 May 2019 13:40:03
os: Linux-3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017
nodename: VM_0_6_centos
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /root/config
detected binary path: /root/.pyenv/versions/3.6.5/envs/envtest3/bin/uwsgi
setgid() to 1000
setuid() to 1000
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3893
your memory page size is 4096 bytes
detected max file descriptor number: 100001
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
The -s/--socket option is missing and stdin is not a socket.

设置uwsgi.ini内容

[uwsgi]
socket = 127.0.0.1:8000
stats = 127.0.0.1:9293
workers = 4
# 项目根目录
chdir = /root/www/leimizhu
touch-reload = /root/www/leimizhu
py-auto-reload = 1
# 在项目跟目录和项目同名的文件夹里面的一个文件
module= leimizhu.wsgi
pidfile = /var/run/inner_manager.pid
daemonize = /var/log/uwsgi/uwsgi8000.log
#pyenv虚拟环境
virtualenv = /root/.pyenv/versions/3.6.5/envs/envtest3
# If you plan to receive big requests with lots of headers you can increase this value up to 64k (65535).
buffer-size=65535
# 日志文件,需要提前手动创建
logto=/tmp/mylog.log

uwsgi启动

(envtest3) [root@VM_0_6_centos ~]# uwsgi --ini /root/config/uwsgi.ini
## /var/log/uwsgi/uwsgi8000.log    uwsgi日志查看
打赏 赞(0)

为您推荐