本文记录 LNMP 1.1环境中集成uWSGI部署Django应用的经过。
测试用的VPS 环境为Ubuntu 14.04 x64 , Python 2.7.6
一、安装平台
1、安装pip
#apt-get install python-gevent python-pip
这里坑比较多,建议先装好pip 然后进行下面的步骤,后面的几个包都可以用pip 安装
2.pip安装应用示例
(1)安装包
# pip install simplejson
(2)更新包
# pip install --upgrade simplejson
(3)移除包
# pip uninstall simplejson
3、安装MySql-python
#pip install MySql-python
4、安装uWSGI
#pip install uWSGI
5、安装Django
#pip install Django
创建django项目
项目位置:/home/django/mysite
创建项目方法:
#cd /home/django
#django-admin.py startproject mysite
二、平台配置
1、Nginx相关配置
#mkdir /usr/local/nginx/conf/django
#cd /usr/local/nginx/conf/django
#vi django_uwsgi.conf
内容如下:
# Django project
server {
listen 80;
server_name 域名;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
uwsgi_param UWSGI_CHDIR /home/django/mysite;
uwsgi_param UWSGI_SCRIPT django_wsgi;
access_log off;
}
}
编辑完成后在nginx.conf追加子配置文件django_uwsgi.conf
#cd ..
#vi nginx.conf
内容如下:
http{
........
include django/*.conf;
}
2、UWSGI配置
#mkdir -p /home/uwsgi;
#cd /home/uwsgi
#mkdir -p /home/django/logs
#echo "" > /home/django/logs/django.log
#vi 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>/home/django/mysite</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>/home/django/logs/django.log</daemonize>
</uwsgi>
注意,如果下面几步启动不了可以更改 200 到 20 试试,可能存在端口占用的情况
3、创建django应用模块
#cd /home/django/mysite
#vi django_wsgi.py
内容如下:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
django 1.7之后 更正为:
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
if django.VERSION < 1.7:
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
else:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
否则可能出现无休止的502错误
4、启动服务
重启前需要先把所有的uwsgi进程删掉
killall -9 uwsgi
重启:
#/usr/local/bin/uwsgi -x /home/uwsgi/uwsgi.xml
#/usr/local/nginx/sbin/nginx -s reload #重启nginx服务
5、查看进程
# ps -ef|grep uwsgi|grep -v grep
6、监听端口
# netstat -an|grep 9000
7、访问测试:
输入你刚才绑定的域名 ,注意 django 项目里的 ALLOWED_HOSTS = [] 设置成你的域名 [‘domian’] or [‘*’]
三、uWSGI/Django应用服务自启动
1、uWSGI启动脚本
#cd /home/uwsgi/
#vi uwsgiserver
内容如下:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# Check if user is root
if [ $(id -u) != "0" ]; then
printf "Error: You must be root to run this script!\n"
exit 1
fi
if [ "$1" = "start" ]; then
psid=`ps aux|grep "uwsgi"|grep -v "grep"|wc -l`
if [ $psid -gt 2 ];then
echo "uwsgi is running!"
exit 0
else
/usr/local/bin/uwsgi -x /home/uwsgi/uwsgi.xml
/usr/local/nginx/sbin/nginx -s reload
fi
echo "Start uwsgi service [OK]"
elif [ "$1" = "stop" ];then
killall -9 uwsgi
echo "Stop uwsgi service [OK]"
elif [ "$1" = "restart" ];then
killall -9 uwsgi
/usr/local/bin/uwsgi -x /home/uwsgi/uwsgi.xml
/usr/local/nginx/sbin/nginx -s reload
echo "Restart uwsgi service [OK]"
else
echo "Usages: uwsgiserver [start|stop|restart]"
fi
需要注意的是这里需要修改下这个脚本的权限,不然nginx 重启会有问题
则可以通过脚本 /home/uwsgi/uwsgiserver start|stop|restart来 启动|停止|重启 uWSGI服务。
2、开机自启动uWSGI服务
在/etc/rc.d/rc.local中加入:
/home/uwsgi/uwsgiserver start
Ubuntu Server 12.04 安装Nginx+uWSGI+Django环境
Django Python MySQL Linux 开发环境搭建
Django 的详细介绍:请点这里
Django 的下载地址:请点这里