AberSheeran
Aber Sheeran
I know nothing except the fact of my ignorance.

Uwsgi部署Django

起笔自
所属文集: 杂记
共计 3259 个字符
落笔于

下载Nginx,uwsgi, Mysql

我用的是CentOS7和Python3.5,所以如下(安装新版Python参照我另一篇CentOS7安装Python3

yum install -y nginx
yum install -y mariadb mariadb-devel mariadb-server
pip3 install uwsgi

编写uwsgi,nginx, mysql配置

按照官方文档的说法,如下配置,保存在项目根目录下的uwsgi.ini中,其中%d的含义为“包含配置文件的目录的绝对路径”(结尾是/),%n是“不带扩展名的文件名”。

[uwsgi]
socket = %d%n/django.sock
chmod-socket = 666
processes = %k
pidfile = %d%n/master.pid
touch-logreopen = %dsplitlog.py
daemonize = %d%n/run.log
chdir = /website/proxy/
wsgi-file = proxy/wsgi.py
virtualenv = venv
# clear environment on exit
# like pid or unix socket
vacuum = true

然后在/etc/nginx/conf.d/proxy.conf中这样写

server {
    listen 80 default_server;
    server_name example.com;

    client_max_body_size 200M;

    location / {
        include uwsgi_params;
        uwsgi_pass unix://app/django.sock;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-Port $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static/ {
        alias /app/static/;
    }

    location /media/ {
        alias /app/media/;
    }
}

uwsgi_pass的值和uwsgi.ini配置中socket的值必须一致。

配置mysql

/etc/my.cnf加入以下代码以配置默认编码为UTF8

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

接下来三条命令

# start mariadb
sudo systemctl start mariadb
# Harden MySQL Server
sudo mysql_secure_installation
sudo systemctl enable mariadb

nginx开机自启动

使用vi /etc/init.d/nginx写入如下代码

#!/bin/bash
# chkconfig: - 85 15
nginx=/usr/sbin/nginx
conf=/etc/nginx/nginx.conf

case $1 in
    start)
        echo -n "Starting Nginx"
        $nginx -c $conf
        echo " done"
    ;;

    stop)
        echo -n "Stopping Nginx"
        $nginx -s stop
        echo " done"
    ;;

    test)
        $nginx -t -c $conf
    ;;

    reload)
        echo -n "Reloading Nginx"
        $nginx -s reload
        echo " done"
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    show)
        ps -aux|grep nginx
    ;;

    *)
        echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
    ;;

esac

然后使用三条命令:

sudo chmod +x /etc/init.d/nginx
sudo chkconfig --add nginx
sudo chkconfig nginx on

uwsgi开机自启动

同理,使用vi /etc/init.d/uwsgi写入

#!/bin/bash
# chkconfig: - 85 15
conf=配置文件路径
pid=pidfile路径
case $1 in
    start)
        cd $path
        uwsgi $conf
    ;;

    stop)
        uwsgi --stop $pid
    ;;

    show)
        ps -ef|grep uwsgi
    ;;

    reload)
        uwsgi --reload $pid
    ;;

    *)
        echo -n "Usage: $0 {start|restart|stop|show}"
    ;;

esac

然后使用三条命令:

sudo chmod +x /etc/init.d/uwsgi
sudo chkconfig --add uwsgi
sudo chkconfig uwsgi on

最后,使用命令sudo service uwsgi startsudo service nginx start开启网站。

多站点配置方法

配置文件写法同上,写好多个配置文件放在一个文件夹下,以官方文档推荐的emperor模式运行即可,例如我将多个配置文件放在site.d中。
那么启动它们只需要执行uwsgi --emperor /website/site.d --daemonize /website/site.d/emperor.log即可。

然后在Nginx里添加对应的站点配置。即可。

对于多个类似配置文件,建议使用uwsgi自带的变量来编写,这样既能减少维护成本,又方便快捷。变量的中文文档在此 magicvars

如果你觉得本文值得,不妨赏杯茶
CentOS7上安装Python3
自定义Django后台名称