django+nginx+gunicorn+supervisro部署
一、nginx
1、yum install -y nginx #默认安装后的配置文件路径:/etc/nginx/nginx.conf
2、新建项目的配置文件,因为默认配置文件会包含子配置文件,目录为:/etc/nginx/conf.d/*.conf
比如:新建 blog.conf,内容为:
server {
charset utf-8;
listen 80;
server_name app.mysite.com;
#找不到icon文件时不报错
location = /favicon.ico {access_log off;log_not_found off;}
#下面的静态文件路径后面不加/
location /static {
#这里的路径为django的配置文件settings.py中设置的静态文件收集目录:STATIC_ROOT = os.path.join(BASE_DIR,'collect_static')
alias /root/www/blog/blog/collect_static;
}
location /media {
alias /root/www/blog/blog/media/;
}
location / {
#下面两行为uwsgi模式
#include uwsgi_params;
#uwsgi_pass unix:/run/uwsgi/meikar.sock;
#下面的为使用gunicorn服务,其中8000为将来gunicorn服务端口
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} #非静态文件或媒体文件,将由uwsgi处理
#websocket配置
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
二、gunicorn
1、安装 pipenv install gunicorn
测试是否可以启动服务:gunicorn blog.wsgi:application --bind 0.0.0.0:8000
这时操作的目录应该在项目blog的根目录下,blog.wsgi:application这个库的调用才正确
三、supervisor
1、yum install supervisor #安装
* 在centos8中,可能yum安装时会报错,这时安装源:yum install epel-release
2、默认配置文件路径为/etc/supervisro.conf,因为它可以管理多个进程,为了方便它默认包含自定义配置文件
/etc/supervisord.d/*.ini
这时新建自己的配置文件,如blog.ini
[program:meikar]
#这里的命令是启动gunicorn服务进程的命令,和上面gunicorn测试服务的一样
command=/root/www/blog/.venv/bin/gunicorn -w2 -b0.0.0.0:8000 blog.wsgi:application
#在执行命令前要切换到的目录,就是项目的根目录,manage.py所在的目录,只有这样,上面的blog.wsgi:application才能找到对应的模块文件
directory=/root/www/blog/blog startsecs=0 stopwaitsecs=0 autostart=true autorestart=true redirect_stderr=true stdout_logfile=/root/www/meikar/meikar/log/gunicorn.inf stderr_logfile=/root/www/meikar/meikar/log/gunicorn.err
#如果日志中有中文会报错,所以设置为utf8
environment=LANG="en_US.utf8", LC_ALL="en_US.UTF-8", LC_LANG="en_US.UTF-8"
*启动supervisor:
supervisord -c /etc/supervisord.conf
*停止
supervisorctl shutdown
如果报错:
第一种:Error: Cannot open an HTTP server: socket.error reported errno.ENOENT (2)
For help, use /usr/bin/supervisord -h
则:systemctl start supervisord
第二种:error: another program is already listening on a port that one of our HTTP servers is config...rvisord.
执行命令 find / -name supervisor.sock 然后 unlink /路径/supervisor.sock 最后再执行启动命令 systemctl start supervisord
*设置开机启动:
systemctl enable supervisord
检查是否开机启动:systemctl is-enabled supervisord
查看状态:systemctl status supervisord.service
supervisorctl 常用命令
查看任务状态:supervisorctl status
启动任务:supervisorctl start <name>
停止任务:supervisorctl stop <name>
重启任务:supervisorctl restart <name>
清除日志文件:supervisorctl clear <name>
清除多个日志文件:supervisorctl clear <name> <name>
清除所有日志文件:supervisorctl clear all
移除任务:supervisorctl remove <name>
*/etc/supervisor.conf中有两种客户端管理方式,一种是TCP,一种是SOCK,只要配置一种即可
如果采用的是tcp方式,即网页的方式,使用了哪个端口要在防火墙中开放对应的端口,例如
firewall-cmd --permanent --zone=public --add-port=8080/tcp
firewall-cmd --reload
查看端口的开放情况:
firewall-cmd --list-ports
三、django
1、在根目录下收集静态文件
python manage.py collectstatic
这时会在根目录下生成settings.py中配置的静态文件目录collect_static(nginx的/static指定的目录就是这个)
接下来生成模型对应的表文件
python manage.py makemigrations
python manage.py migrate
有时APP对应的表没有生成:python manage.py makemigrations blog 然后再执行migrate迁移就可以了
***如果在生成数据库时报错:
# http://冒号:密码@ip:port/库号
最新文章
- Fedora 24中的日志管理
- Android Soap实例
- 屠龙之路_狭路相逢勇者胜_EighthDay
- 第二章 D - Number Sequence(1.5.10)
- Android 核心组件 Activity 之下
- 中国IT武林大会暨中国首席技术官2016年度人物颁奖盛典概况
- 浅尝JavaScript document对象
- lumion材质系统室内渲染6.3
- 画一条0.5px的线
- 转:wcf大文件传输解决之道(1)
- [POJ3416]Crossing
- SQL Server查询中特殊字符的处理方法
- R vs Python:载入包 import &; library
- Cognos报表展示图片小技巧
- Windows 程序 dump 崩溃调试
- Linux 入侵检测
- 搜索引擎原理和SEO
- mysql事务隔离
- 最小生成树与Prim算法
- 12.OpenStack镜像和存储服务配置