Web服务器上有多种方式运行Python:
- 传统上的CGI方式:基本已被淘汰
- mod_python的模块方式:仍在广泛使用,但生产环境中也被逐步淘汰
- uwsgi方式:目前生产环境下最受推崇的方式,尤其是Nginx+uwsgi服务器组合是上线的首选。
Python与Web服务器的关系基本上和PHP与Web服务器的关系类似,可以参考该文:CentOS6下搭建 Nginx + PHP + MySQL服务器。
uwsgi服务器的作用类似于java里面的Tomcat服务器。至于在上面跑什么样的Python程序那可能性就太多了。例如:
- 按照uwsgi的规范直接编写的python代码
- Django编写的应用(重量级Web框架)
- Flask编写的应用(轻量级Web框架)
- web2py编写的应用(轻量级Web框架)
Python下的Web框架种类非常多,很难说孰好孰坏。应该说没有最好的,只有最合适的。一个比较实用的选择原则是:
- 如果是开发一个简单的web应用的话,推荐使用轻量级的。
- 如果是开发一个复杂的网站,Django可以提供很多成熟的模块,开发效率会非常高。
这里选择web.py框架作为例子。
安装Nginx + uwsgi + web.py + MySQLdb
-
升级Python至2.7
-
安装Nginx
#rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm #yum install nginx
-
安装MySQL数据库
#yum -y install mysql mysql-server mysql-devel libdbi-dbd-mysql #service mysqld start #chkconfig mysqld on
-
安装MySQLdb (mysql-python)
#easy_install mysql-python
-
安装web.py (官网)
#easy_install web.py
-
安装uwsgi (官网)
#easy_install uwsgi #ln -s /usr/local/python2.7/bin/uwsgi /usr/bin/uwsgi
-
配置uwsgi
uwsgi 的配置文件 可支持xml yaml ini等格式。这里使用ini格式的配置文件。默认路径为
/etc/uwsgi.ini
。[uwsgi] #使用动态端口,启动后将端口号写入以下文件中 socket = /tmp/uwsgi_vhosts.sock #也可以指定使用固定的端口 #socket=127.0.0.1:9031 pidfile=/var/run/uwsgi.pid daemonize=/var/log/uwsgi.log master=true vhost=true gid=nginx uid=nginx #性能相关的一些参数,具体内容查看官网文档 workers=50 max-requests=5000 limit-as=512 #允许加载系统环境变量 reload-os-env=true
-
创建uwsgi开机自启动脚本,便于进行系统管理
vi /etc/init.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 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 755 /etc/init.d/uwsgi
启用开机自动启动:
#chkconfig uwsgi on
启动uwsgi服务:
#service uwsgi start
-
配置nginx下的uwsgi站点
例如新增以下一个站点mysite。
vi /etc/nginx/conf.d/mysite.conf
, 内容:server { listen 9091; server_name localhost; root /www/mysite; index index.html index.htm; access_log /var/log/nginx/mysite_access.log; error_log /var/log/nginx/mysite_error.log; location / { #使用动态端口 uwsgi_pass unix:///tmp/uwsgi_vhosts.sock; #uwsgi_pass 127.0.0.1:9031; include uwsgi_params; uwsgi_param UWSGI_SCRIPT uwsgi; uwsgi_param UWSGI_PYHOME $document_root; uwsgi_param UWSGI_CHDIR $document_root; uwsgi_param UWSGI_SCRIPT index; #需要指明PYTHON_EGG_CACHE,否则默认路径为/root/.python-eggs,没有读写权限 uwsgi_param UWSGI_SETENV PYTHON_EGG_CACHE=/tmp/.python-eggs; } }
-
启动Nginx服务
#service nginx start #chkconfig nginx on
编写一个Hello World!
#vi /www/mysite/index.py
,内容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
urls = (
'/t', 'test', #测试
'/', 'home'
)
app = web.application(urls, globals())
#返回wsgi接口
application = app.wsgifunc()
class test:
'测试'
def GET(self):
# 开发测试用
referer = web.ctx.env.get('HTTP_REFERER', 'http://google.com')
client_ip = web.ctx.env.get('REMOTE_ADDR')
host = web.ctx.env.get('host')
fullpath = web.ctx.fullpath
user_agent = web.ctx.env.get('HTTP_USER_AGENT')
data = ""
data += 'Client: %s<br/>\n' % client_ip
data += 'User-agent: %s<br/>\n' % user_agent
data += 'FullPath: %s<br/>\n' % fullpath
data += 'Referer: %s<br/>\n' % referer
return data
def POST(self):
pass
class home:
'根目录请求的处理'
def GET(self):
return "Hello Web.py"
def POST(self):
return self.GET()
if __name__ == "__main__":
app.run()
浏览器访问:
http://localhost:9091/
http://localhost:9091/t