CentOS7+Gunicorn+Nginx部署Django
步骤
gunicron配置
保证防火墙关闭
保证阿里云服务器的安全组中配置了相应端口号
- 安装gunicron
pip install gunicron
- 将gunicron加入INSTALLED_APPS中,并将服务器ip加入ALLOWED_HOSTS中
INSTALLED_APPS = [
'simpleui',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'haystack',
'blogger',
'comment',
'post',
'ckeditor',
'ckeditor_uploader',
'gunicron', # 加入该行
]
ALLOWED_HOSTS = ['IP地址']
- 查看防火墙状态,并关闭防火墙
systemctl status firewalld # 查看防火墙状态
systemctl stop firewalld # 关闭防火墙
# systemctl start firewalld # 开启防火墙
- gunicron运行指令
gunicorn mypoject.wsgi:application -b 0.0.0.0:8000
# mypoject为项目名称,wsgi为项目目录下的wsgi.py文件,application固定写法
如果项目运行后丢失样式,gunicron出现 Not Found 静态文件的情况,在根urls加入如下代码
from django.contrib.staticfiles.urls import staticfiles_urlpatterns # 加入该行
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns() # 加入该行
- 在外部浏览器中输入 “http://ip:8000/自己定义的url路由”
http://ip地址:8000/index
nginx配置
- 安装Nginx
yum install nginx
安装完成后Nginx会自动运行,可以在外部浏览器测试下Nginx是否正常开启
# 输入服务器IP
http://IP地址
看到"Welcome to nginx",说明正常启动
- 删除default
- 删除 /etc/nginx/sites-enabled/default
- 创建 /etc/nginx/sites-enabled/项目名
编辑创建的文件
server {
listen 80 default_server;
# listen [::]:80 default_server;
server_name _; # 如果你映射了域名,可以写在这里,如:server_name example.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
proxy_pass http://127.0.0.1:8000; #转发的地址,即Gunicorn运行的地址
proxy_redirect off;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ .*{
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# location /static {
# alias /home/xzx/flask_pro/flask_blog/bluelog/bluelog/static/;
# expires 30d; #设置缓存过期时间
# }
}
- 设置 /etc/nginx/nginx.conf
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;
include /etc/nginx/sites-enabled/*; # 添加该行,保证读取 "/etc/nginx/sites-enabled/项目名" 文件
server {
# listen 80 default_server;
# 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 / {
# }
}
- Nginx检查语法是否错误
[root]$ nginx -t
- 启动Nginx
systemctl start nginx
启动Django应用
gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000
外部浏览器测试:http://IP地址/index
- 后台脱机运行Django应用
nohup gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000 &
- 附加知识; Nginx部署多个web应用(Django+Flask)
- 已按照上面方式部署成功Django,再部署一个flask,同时运行两个web
创建 /etc/nginx/sites-enabled/项目名
编辑创建的文件
server {
listen 8001 default_server; # 修改外部访问端口
# listen [::]:80 default_server;
server_name _; # 如果你映射了域名,可以写在这里,如:server_name example.com;
access_log /var/log/nginx/blueblog_access.log;
error_log /var/log/nginx/blueblog_error.log;
location / {
proxy_pass http://127.0.0.1:8892; #转发的地址,即Gunicorn运行的地址
proxy_redirect off;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ .*{
proxy_pass http://127.0.0.1:8892;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# location /static {
# alias /home/xzx/flask_pro/flask_blog/bluelog/bluelog/static/;
# expires 30d; #设置缓存过期时间
# }
}
- 记得为flask创建wsgi.py文件
运行flask项目
nohup gunicorn -w 4 -b 127.0.0.1:8892 wsgi:app &
外部浏览器输入:http://IP地址:8001/即可访问