1.通常centos 都自带有python ,centos5默认的版本是2.43 建议安装2.7 版本,安装 ngixn(略)
2.安装Python工具
yum install python-devel libxml2 libxml2-devel python-setuptools zlib-devel wget openssl-devel pcre pcre-devel sudo gcc make autoconf automake
3.安装pip
wget http://pypi.python.org/packages/source/p/pip/pip-1.0.2.tar.gz --no-check-certificate tar xvfz pip-1.0.2.tar.gz cd pip-1.0.2 python setup.py install
之所以安装 pip ,我们后面可以用pip 命令直接安装 uwsgi 和 django
4.安装uwsgi
pip install uwsgi
或者你可以去官网下载自己解压编译安装
5.安装virtualenv (主要用于多站点创建虚拟环境)
pip install virtualenv
mkdir /srv/vpython cd /srv/vpython virtualenv --no-site-packages mywebsite cd /srv/vpython/mywebsite source bin/activate
会看到提示符前面多了一个(mywebsite)。现在运行python,可以看到sys.path已经更改为/srv/vpython/mywebsite目录下的各个包。
这个目录下的是供该站点使用的虚拟包,换句话说我们再增加新站点的时候也要创建属于它的虚拟包 不能共用一份哦
7.部署django
cd /srv/www/ django-admin.py startproject mywebsite
在/srv/www/website/下创建 uwsgi.py 代码如下:
import sys import os sys.path.append(os.path.abspath(os.path.dirname(__file__))) sys.path.append('/srv/www') os.environ['DJANGO_SETTINGS_MODULE'] = 'mywebsite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
vim /etc/nginx/conf.d/mywebsite.conf
server { listen 80 ; server_name 127.0.0.1; root /srv/www/mywebsite; index index.html index.htm; access_log /var/log/nginx/access.log ; error_log /var/log/nginx/error.log ; location / { uwsgi_pass 127.0.0.1:3031; include uwsgi_params; uwsgi_param UWSGI_SCRIPT uwsgi; uwsgi_param UWSGI_PYHOME $document_root; uwsgi_param UWSGI_CHDIR $document_root; } }
注意上面标红的地方是我们在在/srv/www/website/下创建 uwsgi.py 的文件名 不用带.py
9.创建 uwsgi 的配置文件 可支持xml yaml ini等格式 我习惯用ini,将其放在etc目录下,当然你也可以放到其他地方
vim /etc/uwsgi.ini
添加代码如下:
[uwsgi] socket=127.0.0.1:3031 master=true vhost=true gid=nginx uid=nginx workers=10 max-requests=1000 limit-as=512 pidfile=/var/run/uwsgi.pid daemonize=/var/log/uwsgi.log
注意我使用的端口是3031 因此我们可以看nginx 配置文件里的端口也是3031,还有注意有防火墙的话放行该端口,
装完需要的包后,用deactivate命令退出虚拟环境。
10.创建 uwsgi的服务脚本,在lnmp nginx脚本上直接改的,支持stop start reload
vim /etc/ini.d/uwsgi
#! /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: http://blog.csdn.net/youngqj
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/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
添加执行权限
chmod +x uwsgi
11 安装 mysql-server mysql-devel 同时还要安装 mysql-python
yum install mysql-server yum install mysql-devel pip install mysql-python
12 .启动 service uwsgi start service nginx start service mysqld start
13 .完毕
注意:如果要添加一个站点 我们要重复 创建python 虚拟环境 然后再安装相应的web框架 如 web.py 等
添加nginx 配置文件 特别注意 uwsgi_param UWSGI_PYHOME 要填写正确哦。