(转)Nginx + uwsgi + web.py + MySQLdb
2016-01-05 17:39
381 查看
一、背景知识:
Web服务器上有多种方式运行Python:
传统上的CGI方式:基本已被淘汰
mod_python的模块方式:仍在广泛使用,但生产环境中也被逐步淘汰
uwsgi方式:目前生产环境下最受推崇的方式,尤其是Nginx+uwsgi服务器组合是上线的首选。
Python与Web服务器的关系基本上和PHP与Web服务器的关系类似,可以参考该文:[ CentOS6下搭建 Nginx + PHP + MySQL服务器 ]。
uwsgi服务器的作用类似于java里面的Tomcat服务器。至于在上面跑什么样的Python程序那可能性就太多了。
例如:
按照uwsgi的规范直接编写的python代码
重量级Web框架:Django编写的应用
轻量级Web框架:Flask编写的应用
轻量级Web框架:web2py编写的应用
Python下的Web框架真是太多了。很难说孰好孰坏,应该说没有最好的,只有最合适的。最简单的一个选择原则是:
如果开发的只是一个简单的web应用的话,推荐使用轻量级的。
如果是开发一个复杂的网站,使用Django的话很多成熟的模块都可以直接使用,开发效率会非常高。
这里选择web.py框架作为例子。
二、安装Nginx + uwsgi + web.py + MySQLdb
1. 升级Python至2.7
参照: CentOS下编译安装Python2.7
2. 安装Nginx
#rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm #yum install nginx
3. 安装MySQL数据库
#yum -y install mysql mysql-server mysql-devel libdbi-dbd-mysql
#service mysqld start
#chkconfig mysqld on
4. 安装MySQLdb ( mysql-python )
#easy_install mysql-python
5. 安装web.py ( 官网 )
#easy_install web.py
6. 安装uwsgi ( 官网 )
#easy_install uwsgi
7. 配置uwsgi
uwsgi 的配置文件 可支持xml yaml ini等格式。这里使用ini格式的配置文件。默认路径为/etc/uwsgi.ini。
8. 创建uwsgi开机自启动脚本,便于进行系统管理
vi /etc/init.d/uwsgi,内容如下:
将脚本属性修改为可执行:
#chmod 755 /etc/init.d/uwsgi
启用开机自动启动:
#chkconfig uwsgi on
启动uwsgi服务:
#service uwsgi start
9. 配置nginx下的uwsgi站点
例如新增以下一个站点mysite。
vi /etc/nginx/conf.d/mysite.conf, 内容:
10. 启动Nginx服务
#service nginx start
#chkconfig nginx on
三、编写一个Hello World!
#vi /www/mysite/hello.py,内容:
浏览器访问:
http://localhost:9091/ http://localhost:9091/t
原文:http://www.tudaxia.com/archives/727?utm_source=tuicool&utm_medium=referral
Web服务器上有多种方式运行Python:
传统上的CGI方式:基本已被淘汰
mod_python的模块方式:仍在广泛使用,但生产环境中也被逐步淘汰
uwsgi方式:目前生产环境下最受推崇的方式,尤其是Nginx+uwsgi服务器组合是上线的首选。
Python与Web服务器的关系基本上和PHP与Web服务器的关系类似,可以参考该文:[ CentOS6下搭建 Nginx + PHP + MySQL服务器 ]。
uwsgi服务器的作用类似于java里面的Tomcat服务器。至于在上面跑什么样的Python程序那可能性就太多了。
例如:
按照uwsgi的规范直接编写的python代码
重量级Web框架:Django编写的应用
轻量级Web框架:Flask编写的应用
轻量级Web框架:web2py编写的应用
Python下的Web框架真是太多了。很难说孰好孰坏,应该说没有最好的,只有最合适的。最简单的一个选择原则是:
如果开发的只是一个简单的web应用的话,推荐使用轻量级的。
如果是开发一个复杂的网站,使用Django的话很多成熟的模块都可以直接使用,开发效率会非常高。
这里选择web.py框架作为例子。
二、安装Nginx + uwsgi + web.py + MySQLdb
1. 升级Python至2.7
参照: CentOS下编译安装Python2.7
2. 安装Nginx
#rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm #yum install nginx
3. 安装MySQL数据库
#yum -y install mysql mysql-server mysql-devel libdbi-dbd-mysql
#service mysqld start
#chkconfig mysqld on
4. 安装MySQLdb ( mysql-python )
#easy_install mysql-python
5. 安装web.py ( 官网 )
#easy_install web.py
6. 安装uwsgi ( 官网 )
#easy_install uwsgi
7. 配置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
8. 创建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
9. 配置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; } }
10. 启动Nginx服务
#service nginx start
#chkconfig nginx on
三、编写一个Hello World!
#vi /www/mysite/hello.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
原文:http://www.tudaxia.com/archives/727?utm_source=tuicool&utm_medium=referral
相关文章推荐
- Nginx日志文件配置与切割
- Nginx配置文件nginx.conf中文详解
- nginx提示Error: Too many open files的解决办法
- 搭建Nginx+PHP环境
- 简单配置nginx使之支持pathinfo
- 虚拟机上CentOS 7.0编译安装Nginx+MySQL+PHP(一)
- Nginx + CGI/FastCGI + C/Cpp
- Zabbix监控nginx性能
- Nginx负载均衡(主备)+Keepalived
- Nginx 搭建Web服务器 一
- RedHat6(CentOS6)下PHP+Nginx安装
- php-fpm nginx修改上传文件大小限制
- Nginx安装部署
- Nginx开发从入门到精通
- 使用nginx+passenger将本地Rails项目部署到Centos系统
- nginx配置支持pathinfo模式的url访问(原创)
- Centos 7 Nginx 服务
- Nginx模块开发入门
- nginx实现带参数目录域名重定向二级域名方法
- nginx配置文件讲解