您的位置:首页 > 数据库 > SQL

在nginx上部署django项目--------Gunicorn+Django+nginx+mysql

2017-03-06 11:05 681 查看
一、安装nginx

以前的博客我有写,这里就不写了

http://www.cnblogs.com/wt11/p/6420442.html

二、安装mysql

我用的mysql5.7 64位的二进制包,官网下载安装编译好的二进制包,解压直接使用即可

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

安装二进制mysql5.7http://blog.csdn.net/wb96a1007/article/details/51559741

三、安装gunicorn

pip install gunicorn

在项目目录EdmureBlog下新建gunicorn的配置文件gunicorn.conf.py

[root@localhost EdmureBlog]# pwd
/EdmureBlog

[root@localhost EdmureBlog]# ls
backend db.sqlite3 gunicorn.conf.py manage.py nginx.conf repository static utils
blogss debug.log EdmureBlog gunicorn.error.log Monaco.ttf __pycache__ singleton.py templates web

gunicorn.conf.py

import multiprocessing

bind = "127.0.0.1:8080"
workers = 2  #workers是工作线程数,一般设置成:服务器CPU个数 + 1
errorlog = '/EdmureBlog/gunicorn.error.log'
#accesslog = './gunicorn.access.log'
#loglevel = 'debug'
proc_name = 'gunicorn_blog_project'


四、在EdmureBlog下新建nginx的配置文件dj.conf ,将其链接到/app/nginx/conf/extra下 ln -s /EdmureBlog/dj.conf /app/nginx/conf/extra/dj.conf

/EdmureBlog/dj.conf

server {
listen 8000;
server_name www.wt.com;   #访问django项目的网站
access_log /EdmureBlog/logss/nginx.access.log;#访问日志
error_log //EdmureBlog/logss//nginx.error.log;#错误日志

location / {
proxy_pass http://127.0.0.1:8080;#动态请求交给gunicorn,8080端口就是gunicorn用的端口 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 /EdmureBlog/logss/static/robots.txt;
}

location /favicon.ico {
alias /EdmureBlog/logss/static/img/favicon.ico;
}

location ~ ^/(media|static)/  {  # 注意这个static,如果项目中静态文件的存放目录是  /项目/statics,那么这里也要写成media|statics,然后在html文件中引用css和js的时候,路径也要写成/statics/css/...
root    /EdmureBlog/;  #静态文件存放路径
expires 30d;
}

# this prevents hidden files (beginning with a period) from being served
location ~ /\. {
access_log off; log_not_found off; deny all;
}

}


nginx主配置文件/app/nginx/conf/nginx.conf

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}
http {
include       mime.types;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include       extra/dj.conf; #django站点
include       extra/www.conf; # 其他站点
include       extra/bbs.conf;  #其他站点
include       extra/status.conf;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;

}


五、同时,django项目的站点名(www.wt.com)加入到 settings.py的ALLOWED_HOSTS里。

ALLOWED_HOSTS = ['www.wt.com',]

如果要外部访问,则打开8000端口:

/sbin/iptables -I INPUT -p tcp --dport 8000 -j ACCEPT

service iptatbles save

六、运行gunicorn:

[root@localhost EdmureBlog]# pwd
/EdmureBlog

[root@localhost EdmureBlog]# nohup /usr/local/bin/gunicorn EdmureBlog.wsgi:application -c /EdmureBlog/gunicorn.conf.py &

七、启动nginx

/app/nginx/sbin/nginx -t 检查配置语法是否正确

/app/nginx/sbin/nginx 启动nginx服务

八、关闭防火墙,没有dns的话,Windows修改hosts文件,浏览器访问www.wt.com即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: