Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603325
  • 博文数量: 244
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 09:53
个人简介

记录学习,记录成长

文章分类

全部博文(244)

我的朋友

分类: LINUX

2016-03-14 09:53:02

centos 6.5下搭建django+nginx+uwsgi环境

一.编译安装python
在centos上安装Python方法:

1.安装必要的开发包
[root@localhost ~]# yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

2.下载python 2.7.10
centos 6.5自带的有Python2.6.6版本,这里使用2.7.10版本
[root@localhost ~]# wget
[root@localhost ~]# cd /usr/local
[root@localhost src]# tar xf /root/Python-2.7.10.tgz -C ./

3.编译安装
[root@localhost Python-2.7.10]# ./configure --prefix /etc/python --enable-unicode=ucs4
[root@localhost Python-2.7.10]# make
[root@localhost Python-2.7.10]# make altinstall
安装完毕后可使用python2.7命令进入python2.7环境;

4.安装python包管理
4.1 安装setuptools
[root@localhost python]# /etc/python/bin/python2.7 ez_setup.py

4.2 安装pip
[root@localhost python]# /etc/python/bin/easy_install-2.7 pip
pip安装好后,可以用pip做如下事情:

  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion

二.安装uwsgi
uwsgi下载地址:

1.安装
[root@localhost ~]# /etc/python/bin/pip2.7 install uwsgi

[root@localhost ~]# /etc/python/bin/uwsgi --version
2.0.12


2.创建页面测试uwsgi是否正常
[root@localhost ~]# vim test.py
def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return "Hello,the world!!!"


3.在终端运行
[root@localhost ~]# /etc/python/bin/uwsgi --http :8001 --wsgi-file test.py 

uwsgi具体选项说明可以uwsgi --help查看


[root@localhost ]# ps -ef | grep uwsgi
root      2822  9321  0 20:49 pts/1    00:00:00 /etc/python/bin/uwsgi --http :8001 --wsgi-file test.py
root      2823  2822  0 20:49 pts/1    00:00:00 /etc/python/bin/uwsgi --http :8001 --wsgi-file test.py
root      2842  9274  0 20:51 pts/0    00:00:00 grep uwsgi


4.在浏览器地址栏中输入http://本地IP:8001,若有Hello,the world!!!输出则安装正确


三.安装django
1.安装
[root@localhost ~]# /etc/python/bin/pip2.7 install django


2.测试django是否正常运行
[root@localhost ~]# /etc/python/bin/django-admin.py  startproject  demosite

[root@localhost ~]# cd demosite/
[root@localhost demosite]# ls
demosite  manage.py


[root@localhost demosite]# /etc/python/bin/python2.7 manage.py runserver 0.0.0.0:8002

[root@localhost nginx]# ps -ef | grep python2.7
root      2880  9321  1 21:08 pts/1    00:00:01 /etc/python/bin/python2.7 manage.py runserver 0.0.0.0:8002
root      2883  2880  6 21:08 pts/1    00:00:04 /etc/python/bin/python2.7 manage.py runserver 0.0.0.0:8002
root      2893  9274  1 21:10 pts/0    00:00:00 grep python2.7


django正常运行;

四.安装Nginx
Nginx已经在之前安装过了,这里省略其安装过程;
http://blog.chinaunix.net/uid-30212356-id-5672283.html

五.配置uwsgi
1.在/etc目录下创建uwsgi配置文件
[root@localhost ~]# vim /etc/uwsgi9090.ini
[uwsgi]
socket = 127.0.0.1:9090 #指定uwsgi的客户端将要连接的socket的路径或者地址
master = true #启动主进程
vhost = true #开启虚拟主机模式
no-stie = true #不加载python的site.py模块
workers = 2 #为预先派生模式设置工作进程的数量
reload-mercy = 10 #设置等待平滑重启一个工作子进程结束的超时时间(秒)
vacuum = true #当服务器退出的时候自动删除unix socket文件和pid文件
max-requests = 1000 #当工作进程处理的请求数没达到设定值时,该进程会被回收重用
limit-as = 512 #限制每个uWSGI的进程的虚拟内存使用数
buffer-size = 30000 #设置用于uwsgi包解析的内部缓存区大小,默认是4k
pidfile = /var/run/uwsgi9090.pid   #指定pid文件位置
daemonize = /var/log/uwsgi9090.log #使进程在后台运行,指定日志文件的了位置


具体参数说明可参考如下资料:
uwsgi参数官方说明:
中文版:http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html


2.创建uwsgi控制脚本(根据自己安装uwsgi的路径更改脚本部分内容)
[root@localhost ~]# cat /etc/rc.d/init.d/uwsgi9090
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'
 
### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the uwsgi web server
# Description:       starts uwsgi using start-stop-daemon
### END INIT INFO
 
# Author:   licess
# website:  
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/etc/python/bin
DESC="uwsgi daemon"
NAME=uwsgi9090
DAEMON=/etc/python/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
set -e
[ -x "$DAEMON" ] || exit 0
 
do_start() {
    $DAEMON $CONFIGFILE || echo -n "uwsgi already running"
}
 
do_stop() {
    $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
    rm -f $PIDFILE
    echo "$DAEMON STOPED."
}
 
do_reload() {
    $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
}
 
do_status() {
    ps aux|grep $DAEMON
}
 
case "$1" in
 status)
    echo -en "Status $NAME: \n"
    do_status
 ;;
 start)
    echo -en "Starting $NAME: \n"
    do_start
 ;;
 stop)
    echo -en "Stopping $NAME: \n"
    do_stop
 ;;
 reload|graceful)
    echo -en "Reloading $NAME: \n"
    do_reload
 ;;
 *)
    echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
    exit 3
 ;;
esac
 
exit 0


将uwsgi其添加进服务:
[root@localhost ~]# chkconfig --add uwsgi9090


3.设置Nginx
修改server段部分配置如下:
server {
        listen       80;
        server_name  192.168.85.128;

        #charset koi8-r;

        location / {
            include  uwsgi_params;
            uwsgi_pass   127.0.0.1:9090; #和uwsgi中的配置一致
            uwsgi_param  UWSGI_SCRIPT  demosite.wsgi; #入口文件,即wsgi.py相对于项目根目录的位置,这个.相当于一层目录
            uwsgi_param  UWSGI_CHDIR   /root/demosite; #项目根目录
            index        index.html;
        }


六.测试
1.上述配置完成后开启uwsgi和Nginx服务
[root@localhost ~]# service uwsgi9090 start
Starting uwsgi9090: 
[uWSGI] getting INI configuration from /etc/uwsgi9090.ini

[root@localhost ~]# nginx -c /etc/nginx/nginx.conf

[root@localhost ~]# ps -ef | grep uwsgi
root      3074     1  0 09:26 ?        00:00:00 /etc/python/bin/uwsgi /etc/uwsgi9090.ini
root      3075  3074  0 09:26 ?        00:00:00 /etc/python/bin/uwsgi /etc/uwsgi9090.ini
root      3076  3074  0 09:26 ?        00:00:01 /etc/python/bin/uwsgi /etc/uwsgi9090.ini
root      3085  3046  0 09:30 pts/0    00:00:00 grep uwsgi

[root@localhost ~]# ps -ef | grep nginx
root      3078     1  0 09:27 ?        00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
nginx     3079  3078  0 09:27 ?        00:00:00 nginx: worker process         
root      3087  3046  0 09:30 pts/0    00:00:00 grep nginx
2.在浏览器地址栏中输入http://本地IP


文章来源:
http://www.cnblogs.com/xiongpq/p/3381069.html

可参考文章:



阅读(1141) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~