部署环境:
- CentOS 7.4
- Django 2.0.6
- Nginx 1.12.2
- guniron 19.8.1
安装配置django、nginx、guniron
一、django
pip install django==2.0.6
二、nginx
1、安装
yum -y install epel-release
yum -y install nginx
2、启动
systemctl start nginx
systemctl enable nginx
3、修改配置文件
/etc/nginx/nginx.conf
user root root;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
root /opt/django-blog;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
/etc/nginx/conf.d/django_site.conf
upstream blog-gunicorn {
server 127.0.0.1:8000 fail_timeout=0;
}
# 将未加密的流量重定向到加密的网站
server {
listen 80;
server_name site.com;
return 301 https://$server_name$request_uri;
}
# 主服务块
server {
# 设置监听的端口,指定监听的域名
listen 443 default ssl;
client_max_body_size 4G;
server_name site.com;
# 指定日志位置
access_log /var/log/nginx/blog.access_log;
error_log /var/log/nginx/blog.error_log;
# 告诉 nginx 你的 ssl 证书
ssl on;
ssl_certificate /etc/nginx/ssl/1_site.com_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/2_site.com.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 设置根目录
root /opt/django-blog;
# 为 Nginx 指定静态文件路径
location /static/ {
# Autoindex the files to make them browsable if you want
autoindex on;
# The location of your files
alias /opt/django-blog/static/;
# Set up caching for your static files
expires 1M;
access_log off;
add_header Cache-Control "public";
proxy_ignore_headers "Set-Cookie";
}
# 为 Nginx 指定你上传文件的路径
location /media/ {
autoindex on;
# The location of your uploaded files
alias /opt/django-blog/media/;
# Set up aching for your uploaded files
expires 1M;
access_log off;
add_header Cache-Control "public";
proxy_ignore_headers "Set-Cookie";
}
location / {
# Try your static files first, then redirect to Gunicorn
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://$1$3;
proxy_pass http://127.0.0.1:8000;
}
}
4、重载配置
systemctl reload nginx
三、安装gunicorn
pip insatll gunicorn
安装好之后,可以创建django项目根目录创建 gunicorn-conf.py
import multiprocessing
bind = "0.0.0.0:8000"
workers = 2
workers = multiprocessing.cpu_count() * 2 + 1
errorlog = "/opt/django-blog/gunicorn.error.log"
#loglevel = "debug"
proc_name = "gunicorn_blog"
reload = True
daemon = True
然后再项目根目录下之下一下命令:
gunicorn -c gunicorn-conf.py -D --error-logfile /项目根目录/gunicorn-error.log 项目名.wsgi
重启nginx:
systemctl reload nginx
然后访问:
https://site.com
四、安装supervisor,需要用python2进行安装,python3不支持(可选安装)
yum -y install supervisor
git clone
cd supervisor/
python2 setup.py install
在/etc/supervisord.conf末尾根据自己的项目加入配置项,以下为例子:
[program:django_blog]
command=/usr/bin/gunicorn -c /opt/django-blog/gunicorn.conf django_blog.wsgi:application
directory=//opt/django-blog
autostart=true
autorestart=true
stdout_logfile=/opt/django-blog/logs/gunicorn.log
stderr_logfile=/opt/django-blog/logs/gunicorn.err
启动supervirsor:
supervisord -c /etc/supervisord.conf
网友评论