部署环境: python3.6 + 腾讯云服务器
开始用uwsgi部署,不太通用,坑贼多,阻碍了很长时间。最后还是选择了gunicorn。
-
安装django,编写代码,调试运行通过。
ip:port访问成功
-
安装gunicorn & 配置gunicorn
- 安装gunicorn
pip install gunicorn
- 设置gunicorn的软连接
ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn
- project_name/gunicorn-config.py(与setting.py同级目录)
import multiprocessing
bind = ["0.0.0.0:8199"] # 与nginx配置的端口一致
chdir = "/usr/fin/test_blog/project_name"
timeout = 30
errorlog = "/usr/fin/test_blog/project_name/logs/error.log"
#accesslog = "/usr/fin/test_blog/project_name/logs/access.log"
#loglevel = 'debug'
proc_name = 'project_name' # 工程名
keepalive = 6
timeout = 65
graceful_timeout = 10
worker_connections = 100
- 启动gunicorn
方法一:
gunicorn -c project_name/gunicorn-config.py project_name.wsgi
方法二:
gunicorn --bind 0.0.0.0:8199 project_name.wsgi:application
启动成功后,http://123.207.255.182:8199/index/可以访问成功,如上图所示。
- 配置nginx
- /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
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 /usr/fin/nginx/logs/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;
server {
listen 80;
server_name 123.207.255.182;
root /usr/fin/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
proxy_pass http://123.207.255.182:8199;
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;
index index.html index.htm;
}
location /static {
alias /usr/fin/test_blog/project_name/static/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
- 检测nginx配置文件的语法
/etc/nginx/nginx.conf
- 启动nginx
service nginx restart
- 通过nginx访问django