Ex_treme's blog.

django+nginx+uwsgi生产环境配置

2018/05/06 Share

生产环境配置

mysql基础配置

1
2
3
4
5
6
#修改配置文件
$ nano /etc/mysql//mysql.conf.d/mysqld.cnf
bind 0.0.0.0
#给所有用户赋权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;

依赖包环境配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#导出依赖包
pip list
pip freeze > requirements.txt
pip install -r requiements.txt
#加速下载
pip install -i https://pypi.douban.com/simple -r requiements.txt
#参考文档地址
projectsedu.com
#测试uwsgi
cd /home/pzs741/PycharmProjects/IQAS
#进入虚拟环境测试
conda env list
source activate sas
#更改默认的python版本号
sudo rm /usr/bin/python
sudo ln -s /home/pzs741/anaconda3/bin/python3.6 /usr/bin/python

uwsgi配置

1
2
3
4
5
6
7
sudo pip install uwsgi -I
sudo apt-get install libpcre3 libpcre3-dev
pip uninstall uwsgi
sudo apt-get remove uwsgi
pip install uwsgi
cd ~/PycharmProjects/IQAS
uwsgi --http :8000 --module IQAS.wsgi

Nginx 安装与配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件以外的所有文件。

sudo apt-get purge nginx nginx-common # 卸载所有东东,包括删除配置文件。

sudo apt-get autoremove # 在上面命令结束后执行,主要是卸载删除Nginx的不再被使用的依赖包。

sudo apt-get remove nginx-full nginx-common #卸载删除两个主要的包。

sudo apt-get install nginx #安装
/etc/init.d/nginx start #启动
/etc/init.d/nginx stop #关闭
/etc/init.d/nginx restart #重启
sudo service nginx restart #重启nginx

#查看nginx配置文件路径
sudo nginx -t
工程文件根目录IQAS.conf内容

server {
listen 80;
server_name 192.168.43.132 www.piggrush.com;
charset utf-8;
access_log /home/pzs741/PycharmProjects/IQAS/nginx_access.log;
error_log /home/pzs741/PycharmProjects/IQAS/nginx_error.log;
client_max_body_size 75M;


location /static {
alias /home/pzs741/PycharmProjects/IQAS/static;
}

location /media {
alias /home/pzs741/PycharmProjects/IQAS/media;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
include /home/pzs741/PycharmProjects/IQAS/mime.types;
}
}

uWSGI的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
#在自己工程目录下创建uwsgi.ini文件,写入以下内容
[uwsgi]
socket = 127.0.0.1:8000
chdir=/home/pzs741/PycharmProjects/IQAS
module=IQAS.wsgi
master = true
processes=2
threads=2
max-requests=2000
chmod-socket=664
vacuum=true
daemonize = /home/pzs741/PycharmProjects/uwsgi.log

所有配置相关文件

1
2
3
4
5
6
7
8
9
工程路径: /home/pzs741/PycharmProjects/IQAS/
工程静态文件路径: /home/pzs741/PycharmProjects/IQAS/static
wsgi.py的路径: /home/pzs741/PycharmProjects/IQAS/IQAS/wsgi.py
uwsgi.ini的路径: /home/pzs741/PycharmProjects/IQAS/uwsgi.ini
uwsgi日志路径: /home/pzs741/PycharmProjects/IQAS/uwsgi.log
destiny.conf的路径: /home/pzs741/PycharmProjects/IQAS/IQAS.conf
uwsgi_params的路径: /etc/nginx/conf/uwsgi_params
nginx访问日志路径: /home/pzs741/PycharmProjects/IQAS/nginx_access.log
nginx错误日志路径: /home/pzs741/PycharmProjects/IQAS/nginx_error.log

启动uWSGI

uwsgi --ini /home/pzs741/PycharmProjects/IQAS/IQAS.ini

启动nginx

  • sudo ln -s 你的目录/Mxonline/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/
  • python manage.py collectstatic
  • sudo /usr/sbin/nginx

调试

1
2
3
4
5
#重启uwsgi
pkill -f uwsgi
#重启nginx
sudo killall -9 nginx
sudo nginx

image

CATALOG
  1. 1. 生产环境配置
    1. 1.1. mysql基础配置
    2. 1.2. 依赖包环境配置
    3. 1.3. uwsgi配置
    4. 1.4. Nginx 安装与配置
    5. 1.5. uWSGI的配置文件
    6. 1.6. 所有配置相关文件
    7. 1.7. 启动uWSGI
    8. 1.8. 启动nginx
    9. 1.9. 调试