最近项目需要安装和配置多站点环境,所以把多站点开发环境配置和安装记录下来,帮助其他人少走坑。
首先安装一些开发环境基本的包和类库,
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
安装完成之后,我们接下来安装Python。我在之前介绍过Centos升级或者更新默认Python的两种方式,如果没有升级或者安装过的可以查阅和参考Centos Upgrade Python。
安装uwsgi有两种方式一种是通过源码第二种是通过python包管理工具pip。我们可以采用比较简单pip方式来安装:
pip install uwsgi
uwsgi --version
通过一个新建一个t.py脚本文件来测试uwsgi是否能正常运行工作,
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return u"Hello World"
然后在终端运行:
uwsgi --http :8001 --wsgi-file t.py
#http指定了uwsgi监听的http端口号。
#wsgi-file指定wsgi执行的脚本文件
在本地浏览器输入 http://127.0.0.1:8001,如果看到**Hello World**那么代表uwsgi安装成功。
#依据官网介绍的nginx仓库来安装
yum install nginx
uwsgi支持ini、xml等多种配置方式,个人感觉ini配置的这种方式是比较方便的。
[uwsgi]
socket = 127.0.0.1:8001 //socket可支持两种方式,一种是通过端口号另外一种是通过unix socket。
master = true //主进程
vhost = true //多站模式
#no-stie = true //多站模式时不设置入口模块和文件
workers = 2 //子进程数
reload-mercy = 10
vacuum = true //退出、重启时清理文件
max-requests = 1000
limit-as = 512
buffer-sizi = 30000
chidir = /opt/projetc/test # 项目根目录
#wsgi-file = manage.py # 程序的启动文件
#callable = app # 程序变量名
#pidfile = /var/run/uwsgi_8001.pid //pid文件,用于下面的脚本启动、停止该进程
plugins = python
daemonize = /website/uwsgi_8001.log
基于supervisor的uwsgi的设置如下:
配置完之后,设置uwsgi开机启动,在/ect/init.d/目录下新建uwsgi_8001文件,如果觉得麻烦也可以用supervisor。内容如下:
#! /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://lnmp.org
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi_8001
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/opt/$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_8001
如果想设置成开机启动则运行如下:
#添加服务
chkconfig --add uwsgi_8001
#设置开机启动
chkconfig uwsgi_8001 on
也可以用来配置,下面让我们来看一下supervisor的配置:
echo_supervisord_config > /etc/supervisord.conf
vi /etc/supervisord.conf
[program:uwsgi]
command = uwsgi --ini /opt/uwsgi.ini # 跟手动启动的命令一样
stopsignal=QUIT
autostart=true
autorestart=true
stdout_logfile=/var/log/uwsgi/supervisor_wsgi.log # 运行日志
stderr_logfile=/var/log/uwsgi/supervisor_wsgi_err.log # 错误日志
然后再运行如下命令:
supervisorctl reload
nginx如果是通过yum安装的话,已经集成了服务,如果通过源码安装也要设置成相应的开机启动。
upstream django {
server 127.0.0.1:8001;
}
server {
listen 80;
server_name 192.168.1.14;
charset utf-8;
client_max_body_size 75M;
location /static {
alias /opt/project/static;
}
location / {
root /opt/project;
uwsgi_pass django;# 转发端口,需要和uwsgi配置当中的监听端口一致
include uwsgi_params;# 导入uwsgi配置
#uwsgi_param UWSGI_PYTHON /opt/project/venv; # Python解释器所在的路径(这里为虚拟环境)
#uwsgi_param UWSGI_CHDIR /opt/project; # 项目根目录
#uwsgi_param UWSGI_SCRIPT manage:app; # 项目的主程序
}
}
如果想配置多个查念只需要重复创建uwsgi和nginx配置即可,然后运行如下命令:
service uwsgi_8001 start
service uwsgi_8002 start
service nginx start