centos6.5下配置django+uwsgi+nginx

本人系统使用的是cetos6.5,python版本为3.5html

安装django

可利用以下命令行安装python

pip install django

默认会安装到python目录下的bin文件夹下,以下图所示
这里写图片描述nginx

  • 若是已经设置了python3.5环境变量可直接使用django-admin来建立项目
  • 若是没有可利用以下命令来建立软链接使django-admin任何目录下都可使用,注意:本人的python3.5目录为/usr/local/python3.5/,请对照本身的目录进行更换
ln -s /usr/local/python3.5/bin/django-admin.py /usr/bin/django-admin

测试安装是否正确:

本人在本身的/root/django目录下面采用以下命令建立了django的项目web

django-admin startproject hello

此时在本目录咱们会发现一个hello的项目已经建立
这里写图片描述shell

执行以下命令来启动django项目django

cd hello
python manage.py runserver 0.0.0.0:8002

打开浏览器输入http://localhost:8002/,若是显示以下界面,则表示django安装成功
这里写图片描述vim

安装uwsgi

能够执行以下命令进行安装浏览器

pip install uwsgi
  • 若是已经设置了python3.5环境变量可直接使用uwsgi命令来部署服务
  • 若是没有可利用以下命令来建立软链接使uwsgi命令在任何目录下都可使用,注意:本人的python3.5目录为/usr/local/python3.5/,请对照本身的目录进行更换
ln -s /usr/local/python3.5/bin/uwsgi /usr/bin/uwsgi

测试安装是否正确

在家目录中建立test.py文件,写入以下内容:ruby

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

经过以下命令来运行该文件bash

uwsgi --http :8002 --wsgi-file test.py

这里写图片描述
在浏览器中输入http://localhost:8002/,若是出现以下结果,表示安装成功
这里写图片描述

将uwsgi与django结合

刚刚咱们建立的django项目位于/root/django/hello
执行以下命令来检验uwsgi是否能与django项目成功结合

uwsgi --http :8008 --chdir /root/django/hello --wsgi-file hello/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192

经常使用选项以下所示:

  • http : 协议类型和端口号

  • processes : 开启的进程数量

  • workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)

  • chdir : 指定运行目录(chdir to specified directory before apps loading)

  • wsgi-file : 载入wsgi-file(load .wsgi file)

  • stats : 在指定的地址上,开启状态服务(enable the stats server on the specified
    address)

  • threads : 运行线程。因为GIL的存在,我以为这个真心没啥用。(run each worker in prethreaded

  • mode with the specified number of threads)

  • master : 容许主进程存在(enable master process)

  • daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最经常使用的,仍是把运行记录输出到一个本地文件上。

  • pidfile : 指定pid文件的位置,记录主进程的pid号。

  • vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

注意:–wsgi-file后面跟的是相对目录

此时在浏览器中输入:http://localhost:8008/,出现以下界面,表示二者结合成功
这里写图片描述

鉴于后面跟的参数较多,咱们能够将这些参数放在一个ini文件中,固然,uwsgi支持多种类型的配置文件,如xml,ini等。此处,使用ini类型的配置。
在hello项目目录下建立hello_uwsgi.ini文件,文件内容以下:

# hello_uwsgi.ini file
[uwsgi]

# Django-related settings

http = :8008

# the base directory (full path)
chdir           = /root/django/hello

# Django s wsgi file
module          = hello.wsgi

# process-related settings
# master
master          = true

# maximum number of worker processes
processes       = 4

# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum          = true

此时执行以下命令:

uwsgi --ini myweb_uwsgi.ini

此时在浏览器中输入:http://localhost:8008/,出现以下界面,表示使用ini文件成功
这里写图片描述

安装nginx

能够执行以下命令进行安装

cd /usr/local/
wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure 
make && make install

执行完以后咱们会发如今/usr/local目录会多出一个nginx目录
这里写图片描述


测试安装是否正确

因为80端口较容易被别的程序占用,需执行以下命令来修改nginx配置文件中的端口号

vim nginx/conf/nginx.conf

以下图所示将listen对应的80改为8088,并保存退出
这里写图片描述
执行以下命令来启动nginx服务

nginx/sbin/nginx

这里写图片描述

打开浏览器输入http://127.0.0.1:8088/,若是出现以下界面则表示nginx安装正确
这里写图片描述

将nginx与uwsgi以及django结合

执行以下命令来修改nginx.conf文件

vim /usr/local/nginx/conf/nginx.conf

这里写图片描述
主要修改如上图所示:
listen端口改成8092
在location中添加:

include uwsgi_params;
uwsgi_pass 127.0.0.1:8008;(端口要与ini中的端口一致)

执行以下命令来启动:

uwsgi --ini /root/django/hello/hello_uwsgi.ini & /usr/local/nginx/sbin/nginx

打开浏览器输入http://localhost:8008/,显示以下结果表示成功
这里写图片描述