Django + uwsgi + nginx + virtualenv

django建表:

python3 manage.py makemigrations   创建脚本
python3 manage.py migrate   迁移

setting静态文件配置

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "statics"),
]
STATIC_ROOT = '/opt/production/production/static'

收集静态文件

python manage.py  collectstatic

改中文,还上海时区

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

uwsgi.ini配置

[uwsgi]
#使用nginx连接时使用,Django程序所在服务器地址
socket=0.0.0.0:10001
#直接做web服务器使用,Django程序所在服务器地址
;http=0.0.0.0:10001
#项目目录
chdir=/opt/payslip
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=payslip/wsgi.py
# 进程数
processes=2
# 线程数
threads=2
# uwsgi服务器的角色
master=True
# 存放进程编号的文件
pidfile=uwsgi.pid
# 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。我们以前的runserver是依赖终端的
daemonize=uwsgi.log
# 指定依赖的虚拟环境
virtualenv=/home/shion/.virtualenvs/payslip

Nginx配置:

 1 server {
 2     listen 10000;
 3     listen [::]:10000 default_server;
 4 
 5 
 6     root /var/www/html;
 7 
 8     # Add index.php to the list if you are using PHP
 9     index index.html index.htm index.nginx-debian.html;
10 
11     server_name localhost.com;
12 
13     location / {
14         # First attempt to serve request as file, then
15         # as directory, then fall back to displaying a 404.
16         include /etc/nginx/uwsgi_params;
17         uwsgi_pass 0.0.0.0:10001;
18         root html;
19         index index.html index.htm;
20     }
21     location /static {
22         alias /opt/payslip/static;
23         
24     }
Nginx.conf

注意事项

  1、uwsgi里面配置的端口号 和 nginx反向代理的端口不能一直,不然会导致uwsgi无法启动

  2、 virtualenv环境应该是python3 环境, 之前没注意导致查了很久的问题  

mkvirtualenv -p /usr/bin/python3.6 payslip

nginx相关命令

nginx -s reload  平滑重启
nginx -s stop  停止nginx
nginx  启动nginx

Supervisor管理进程 

安装和配置supervisor

apt install supervisor   #安装
vim /etc/supervisor/supervisor.conf

  [program:zy_payslip]
  command=/root/.virtualenvs/payslip/bin/uwsgi /opt/zy_payslip/uwsgi.ini
  stopasgroup=true
  killasgroup=true

# 加载配置

supervisroctl update

# 重新载入配置

supervisorctl reload

supervisor重启进程

sudo supervisorctl restart payslip

 网络要开放域名解析端口

http://payslip.xxxx.xxxx:10006/manage/    无法访问
http://192.168.100.11:10006/manage/   可以访问
开放 payslip.xxxx.xxxx  到 192.168.100.11 的10006端口

  

posted on 2019-11-15 16:52  拾玥大熊  阅读(152)  评论(0编辑  收藏  举报