在nginx上布置django项目——–Gunicorn+Django+nginx+mysql

一、安装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即可 

网站地图xml地图