1、安装Django
pip isntall django==1.9.5
2、安装Nginx
yum -y install nginx
3、 配置成开机启动,并手动启动nginx服务:
chkconfig nginx on
service nginx start
4、修改etc/nginx/nginx.conf
user root;
5、然后安装gunicorn:
pip install gunicorn
6、在blog_project下新建gunicorn的配置文件gunicorn.conf.py
workers是工作线程数,一般设置成:服务器CPU个数 + 1
import multiprocessing
bind = "127.0.0.1:8080"
workers = 2
errorlog = '/home/shawn/blog_project/gunicorn.error.log'
accesslog = './gunicorn.access.log'
loglevel = 'debug'
proc_name = 'gunicorn_blog_project'
7、配置文件/etc/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 root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
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 /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;
server {
listen 80;
#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 / {
#try_files @uri @hua_shan;
proxy_pass http://127.0.0.1:5000;
proxy_redirect default;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
#location @hua_shan {
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header Host $http_host;
# proxy_pass http://127.0.0.1:5000;
#}
#error_page 404 /404.html;
# location = /40x.html {
#}
#error_page 500 502 503 504 /50x.html;
# location = /50x.html {
#}
}
server {
listen 8080;
#listen [::]:80 default_server;
server_name _wn;
#root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
#try_files @uri @hua_shan;
proxy_pass http://127.0.0.1:5001;
proxy_redirect default;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
server {
listen 81; #监听端口
server_name -;
access_log /home/wuwg/huashanjxhfc/nginx.access.log;
error_log /home/wuwg/huashanjxhfc/nginx.error.log;
location / {
proxy_pass http://127.0.0.1:8000; #本机访问的ip
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /robots.txt {
alias /home/wuwg/huashanjxhfc/static/robots.txt;
}
location /favicon.ico {
alias /home/wuwg/huashanjxhfc/static/img/favicon.ico;
}
location ~ ^/(media|static)/ {
root /home/wuwg/huashanjxhfc;
expires 30d;
}
# this prevents hidden files (beginning with a period) from being served
location ~ /\. {
access_log off; log_not_found off; deny all;
}
}
}
8、设置项目目录settings.py
INSTALLED_APPS = [
‘app’
‘gunicorn’
]
9、可以运行一下gunicorn:
sudo nohup /usr/local/bin/gunicorn blog_project.wsgi:application -c /home/shawn/blog_project/gunicorn.conf.py&
同时,必须把nginx.conf里server_name后面的内容(localhost)加入到 settings.py里的ALLOWED_HOSTS
ALLOWED_HOSTS = ['localhost','example.com']
如果要外部访问,则打开80端口:
sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo service iptatbles save
service nginx restart