本机环境介绍

虚拟机操作系统版本如下

[root@node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

IP地址为:192.168.3.3/24
安装epel源

rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
# 或者
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

如果之前有epel源,需要先删除:

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup

安装Nginx依赖包

yum install -y openssl-devel pcre-devel 

安装Nginx

[root@node1 /usr/local/src/nginx-1.10.2]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
[root@node1 /usr/local/src]# tar xf nginx-1.10.2.tar.gz 
[root@node1 /usr/local/src]# cd nginx-1.10.2/
[root@node1 /usr/local/src/nginx-1.10.2]# ./configure
[root@node1 /usr/local/src/nginx-1.10.2]# make && make install

修改nginx配置文件如下:

[root@EMMGTEST2 ~]# egrep -v '^$|#' /usr/local/nginx/conf/nginx.conf 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen       80;
        server_name  deng t20.com;
    access_log      /var/log/nginx/myweb_access.log;
    error_log       /var/log/nginx/myweb_error.log;
    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
    }   
    location /static {
        alias /application/django/mysite/static/;
     }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

创建nginx accesslog路径

[root@node1 /usr/local/src/nginx-1.10.2]# mkdir /var/log/nginx

检查配置文件没有问题

[root@node1 /usr/local/src/nginx-1.10.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动nginx

[root@node1 /usr/local/src/nginx-1.10.2]# /usr/local/nginx/sbin/nginx

安装pip(python的安装程序)

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
#当有多个版本的python时,要在特定的版本安装pip需要用那个版本的python执行get-pip.py, 比如在python2.6上安装pip:python2.6 get-pip.py

安装uwsgi

pip install uwsgi

安装django,版本不正确可能起不来,版本要和自己的电脑上的版本一致,要不会有问题。django在1.10的时候template还有session貌似都做过改动,

pip install django==1.9.8

创建目录

mkdir /wis/django

把mysite文件夹拷贝到django下面
确认uwsgi.ini配置文件没有问题:

[root@node1 /wis/django/mysite]# egrep -v '^$|#' uwsgi.ini 
[uwsgi]
socket = :8000
chdir           = /wis/django/mysite
module          = mysite.wsgi
master          = true
processes       = 4
vacuum          = true

[root@node1 /wis/django/mysite]# pwd
/wis/django/mysite

在当前目录启动uwsgi

[root@node1 /wis/django/mysite]#nohup uwsgi --ini uwsgi.ini &

一般生产中我们都是通过unix socket文件来传输,如下(socket属性):

[root@v178 ~]# stat /run/mysite.socket 
  文件:"/run/mysite.socket"
  大小:0               块:0          IO 块:4096   套接字
设备:12h/18d   Inode:99694       硬链接:1
权限:(0664/srw-rw-r--)  Uid:(    0/    root)   Gid:(  995/   nginx)
最近访问:2017-05-15 08:55:58.154113380 +0800
最近更改:2017-05-15 08:55:58.154113380 +0800
最近改动:2017-05-15 08:55:58.154113380 +0800
创建时间:-
[root@v178 ~]# ll /run/mysite.socket 
srw-rw-r-- 1 root nginx 0 5月  15 08:55 /run/mysite.socket

我们的uwsgi配置文件要修改如下:

[root@v178 moji]# cat uwsgi.ini |grep -v '#'
[uwsgi]
socket = /run/mysite.socket
chdir           = /application/django/moji
module          = moji.wsgi
master          = true
processes       = 4
vacuum          = true
chmod-socket = 664
chown-socket = root:nginx

nginx location配置如下:

location / { 
    include uwsgi_params;
    uwsgi_pass unix:/run/mysite.socket;
}  

本文没有做优化设置,要做进一步的了解,大家可以参考nginx官方文档了。
还有要说的是socket文件之所以放到/run下面是由于在/tmp下面有权限问题(开始放到tmp下面是没问题的,后来不知道为啥就不行了),和这个最后的t有关系,具体的就没仔细研究了

[root@v178 ~]# ls -ld /tmp
drwxrwxrwt. 19 root root 4096 5月  15 09:17 /tmp