A-A+

nginx+uwsgi+django+python环境部署文档

2014年02月11日 Python 评论 1 条 阅读 21,440 views 次

为获得更好的阅读效果,请下载本文PDF文档:

python+django+nginx+uwsgi环境部署文档下载地址:

http://www.linuxyw.com/download/python_django_nginx_uwsgi.pdf

环境:

系统:Centos 6.3  64位

Nginx版本:1.4.4

Django版本:1.5.5

Python版本:2.6.6

Uwsgi版本:1.4.2

安装MySQL-python:

yum install -y MySQL-python

也可以用源码包进行安装,下载地址:http://sourceforge.net/projects/mysql-python/files/mysql-python/

安装好后,进入python,import MySQLdb看下是否正常,如果这里出错,那么以后你项目中使用到mysql时,将会出错,在uwsgi给出的信息中就是导入MySQLdb出错。

安装nginx:

cd /usr/local/src
useradd www
wget http://nginx.org/download/nginx-1.4.4.tar.gz
tar xf nginx-1.4.4.tar.gz
cd nginx-1.4.4
./configure --prefix=/usr/local/nginx --user=www --group=www
make
make install

更多的安装参数,可自行定义,我这里只是用最简单的几个参数

安装uwsgi:

cd /usr/local/src
wget http://projects.unbit.it/downloads/uwsgi-1.4.2.tar.gz
tar -xf uwsgi-1.4.2.tar.gz
cd uwsgi-1.4.2
make
cp uwsgi /usr/bin

uwsgi最新版本有2.x的,官网地址: http://projects.unbit.it/uwsgi/

安装django:

cd /usr/local/src
wget 'https://www.djangoproject.com/download/1.5.5/tarball/'
tar xf Django-1.5.5.tar.gz
cd Django-1.5.5.tar.gz
python setup.py install

目前django最新版是1.6.2,用哪个版本,根据自己需求

创建django项目:

项目位置:/python
创建项目方法:

mkdir -p /python
cd /python
django-admin.py startproject umework

umework 是我的项目名,这个可根据自己的项目名称自定义了,如果你已经有了django项目了,以上创建项目步骤可省略。我这里是把项目放在/python目录下。

配置nginx:

在nginx配置文件中添加以下内容:

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

server {
        listen  80;
        server_name umework.linuxyw.com;
     
        location / {
            include     uwsgi_params;
            uwsgi_pass   127.0.0.1:9000;
            uwsgi_param UWSGI_CHDIR  /python/umework;
            uwsgi_param UWSGI_SCRIPT django_wsgi;
            #access_log  off;
            access_log /usr/local/nginx/logs/access.log;
            }
    }

以下是全配置文件,关于性能调优方面,根据自己的情况进行优化:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
server {
        listen  80;
        server_name umework.linuxyw.com;
     
        location / {
                root /python/umework;
            include     uwsgi_params;
            uwsgi_pass   127.0.0.1:9000;
            uwsgi_param UWSGI_CHDIR  /python/umework;
            uwsgi_param UWSGI_SCRIPT django_wsgi;
            #access_log  off;
            access_log /usr/local/nginx/logs/access.log;
            }
    }
}

配置uwsgi:

在nginx目录中添加一个uwsgi配置文件

vim /usr/local/nginx/conf/uwsgi.xml

<uwsgi>
 <socket>127.0.0.1:9000 </socket>
 <listen>200 </listen>
 <master>true </master>
 <pidfile>/usr/local/nginx/uwsgi.pid </pidfile>
 <processes>8 </processes>
 <pythonpath>/python/umework </pythonpath>
 <pythonpath>/python </pythonpath>
 <module>django_wsgi</module>
 <profiler>true </profiler>
 <memory-report>true </memory-report>
 <enable-threads>true </enable-threads>
 <logdate>true </logdate>
 <limit-as>6048 </limit-as>
 <daemonize>/dev/null</daemonize>
</uwsgi>

socket 这里要跟nginx.conf配置中的uwsgi_pass一致,这里都用127.0.0.1:9000

daemonize 这里是uwsgi的信息,我这里不记录,全导到/dev/null上去了

创建django应用模块:

在项目目录里面,创建以下文件和内容,我的项目在/python/umework/

注意第三行中的umework.settings.这个umework是我的项目名,如果你不是按着我文档做的,就在这里修改成自己的项目名就OK了

vim /python/umework/django_wsgi.py

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'umework.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

本文博客:www.linuxyw.com

配置nginx启动脚本:

vim /etc/init.d/nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
.  /etc/rc.d/init.d/functions
# Source networking configuration.
.  /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
     
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

配置uwsgi启动脚本:

vim /etc/init.d/uwsgi
#!/bin/bash
# uwsgi script
# it is v.0.0.1 version.
# chkconfig: - 89 19
# description: uwsgi script
# processname: uwsgi
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
     
uwsgi_config=/usr/local/nginx/conf/uwsgi.xml
uwsgi_pn=`ps aux|grep -v "grep"|grep -c "uwsgi"`
uwsgi_pid=`ps -eo pid,comm|grep uwsgi|sed -n 1p|awk '{print $1}'`
uwsgi_PID=/usr/local/nginx/logs/uwsgi.pid
uwsgi=/usr/bin/uwsgi
RETVAL=0
prog="uwsgi"
# Source function library.
.  /etc/rc.d/init.d/functions
     
     
if [ $(id -u) != "0" ]; then
    printf "Error: You must be root to run this script!\n"
    exit 1
fi
     
     
# Start nginx daemons functions.
start() {
if [ $uwsgi_pn -gt 5 ];then
        action "uwsgi is running!" /bin/true
    exit 0
fi
    daemon $uwsgi -x ${uwsgi_config}
        action "uwsgi start ..." /bin/true
}
# Stop nginx daemons functions.
stop() {
if [ $uwsgi_pn -gt 5 ]
then
        kill -9 `ps -eo pid,comm|grep uwsgi|sed -n 1p|awk '{print $1}'`
    RETVAL=$?
        action "uwsgi stopping ..." /bin/true
else
        action "uwsgi not running!" /bin/false
fi
}
     
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
*)
        echo $"Usage: $prog {start|stop|restart}"
        exit 1
esac
exit $RETVAL

启动nginx和uwsgi:

chmod a+x /etc/init.d/nginx
chmod a+x /etc/init.d/uwsgi
service nginx start
service uwsgi start

访问测试:

输入你在nginx中配置的url,如我配置的是:umework.linuxyw.com

当出现以下界面时,或你的程序首页时,说明配置成功

1 条留言  访客:0 条  博主:0 条   引用: 1 条

来自外部的引用: 1 条

  • 搭建Nginx+uWSGI+Django+MySQL服务器 | RAMBO BLOG

给我留言

Copyright © linux系统运维 保留所有权利.   Theme  Ality 粤ICP备13023035号-1

用户登录

分享到: