nginx+uwsgi+python环境搭建
2013-06-30 TECH config Nginx python uwsgi uwsgi manage shell script
参考资料:
http://amix.dk/blog/post/19689
http://toomuchdata.com/2012/06/25/how-to-install-python-2-7-3-on-centos-6-2/
一、安装python2.7
现在基本上所有的*nix系统都自带python,但版本比较老,比如Centos6.4 自带的为pyton2.6
你可以选择先卸载旧版本再安装新版本最简单
centos下不删除旧版情况下安装新版python2.7:
yum groupinstall "Development tools" yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel cd ~ wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 tar xf Python-2.7.3.tar.bz2 cd Python-2.7.3 ./configure --prefix=/usr/local make && make altinstall
二、安装python包管理和常用包
easy_install: https://pypi.python.org/pypi/distribute
cd ~ wget http://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz tar xf distribute-0.6.35.tar.gz cd distribute-0.6.35 python2.7 setup.py install easy_install --version
virtualenv: https://pypi.python.org/pypi/virtualenv
直接通过easy_install安装,virtual + env 顾名思义,主要帮助我们管理多个网站时的包依赖,让各个网站相互独立
easy_install virtualenv virtualenv --version
pip: https://pypi.python.org/pypi/pip
安装pip的好处是可以pip list、pip uninstall 管理python包, easy_install没有这个功能,只有uninstall
easy_install pip pip --version pip list
uwsgi: https://pypi.python.org/pypi/uWSGI
uwsgi参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi pip list uwsgi --version
三、nginx配置
我定义了upstream主要是为以后服务器扩展做准备,如果你不需要的话下面的 uwsgi_pass 直接写ip端口即可
另外需注意 UWSGI_SCRIPT 根据自己的实际情况修改,比如django为django.wsgi
upstream uwsgi_host { server 127.0.0.1:9090; } server { listen 80; server_name localhost; root /home/wwwroot/python_dev; location / { include uwsgi_params; uwsgi_pass uwsgi_host; uwsgi_param UWSGI_SCRIPT index; uwsgi_param UWSGI_PYHOME $document_root; uwsgi_param UWSGI_CHDIR $document_root; } access_log off; }
四、uwsgi配置
本例为多网站vhost模式,采用yaml格式,你可以自己根据的情况修改
uwsgi参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
使用这个配置的时候 注意把后面的注释去掉
uwsgi: socket: 127.0.0.1:9090 master: true //主进程 vhost: true //多站模式 no-site: true //多站模式时不设置入口模块和文件 vacuum: true //退出、重启时清理文件 gid: www //uwsgi启动 用户组 uid: www //uwsgi启动 用户 workers: 10 //子进程数 max-requests: 1000 //处理都少请求后重启子进程 limit-as: 512 //内存限制 pidfile: /var/run/uwsgi.pid //uwsgi pid文件用于: --reload --stop daemonize: /home/wwwlogs/uwsgi.log //日志文件
五、hello world
存为index.py 对应nginx配置用 “UWSGI_SCRIPT index;” 部分
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
nginx加载网站配置、启动uwsgi
serice nginx reload uwsgi /etc/uwsgi.yaml
这时你打开 http://localhost/ 或者 http://localhost:9090/ 都会看到已经正常工作了
日志文件我的uwsgi.yaml中指定的 /home/wwwlogs/uwsgi.log, 如果遇到错误,你可以查看日志文件逐个修复
六、防火墙与uwsgi管理
如果在第五步中提示ip、端口无法使用多半是防火墙的原因
iptables给localhost 9090端口访问权限即可
一个uwsgi的服务脚本,在lnmp nginx脚本上直接改的,支持stop start reload,但还没实现gracefull restart操作,小型应用完全没问题
#! /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 DAEMON=/usr/local/bin/uwsgi CONFIGFILE=/etc/$NAME.yaml 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
暂无评论