在Windows服务器下用Apache和mod_wsgi配置Python应用的教程
最近开发了一个GoogleAnalytics相关的应用,但需要在Windows下部署,结合网上的相关经验,最终选择了apache+mod_wsgi这样的配置。
修改python应用
Notethatmod_wsgirequiresthattheWSGIapplicationentrypointbecalled'application'.Ifyouwanttocallitsomethingelsethenyouwouldneedtoconfiguremod_wsgiexplicitlytousetheothername. (via:wiki)
因为mod_wsgi默认要求入口名称为application所以我们需要对自己的pythonweb应用做一些修改。
假设我们使用flask搭建的应用,而默认的入口名称为app,建立一个wsgi_handler.wsgi
importsys,os sys.path.insert(0,os.path.dirname(__file__)) fromapplicationimportappasapplication
下载安装httpd
应用的入口修改好之后,就需要安装apache和mod_wsgi了,我使用的是32位的系统,64位系统下载的安装包可能与32位的不同。
打开页面http://apache.dataguru.cn//httpd/binaries/win32/,下载httpd-2.2.22-win32-x86-no_ssl.msi,下载后运行程序,按提示安装,具体过程这里不详述。
安装并配置mod_wsgi
目前Windows下对python支持的最好的应该就是[mod_wsgi][mw]了。
下载https://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-win32-ap22py27-3.3.so
将下载的文件重命名为mod_wsgi.so后移动到apache的modules目录:
在conf/httpd.conf中加入如下配置
LoadModulewsgi_module modules/mod_wsgi.so
配置应用vhost
在conf/httpd.conf中启用vhosts配置文件
#Virtualhosts Includeconf/extra/httpd-vhosts.conf
编辑conf\extra\httpd-vhosts.conf删除无效的示例代码,并加入应用的配置
NameVirtualHost*:5000 <VirtualHost*:5000> ServerNamelocalhost WSGIScriptAlias/E:\Projects\ga-data\wsgi_handler.wsgi <DirectoryE:\Projects\ga-data> Orderdeny,allow Allowfromall </Directory> </VirtualHost>
其中E:\Projects\ga-data替换成应用真实的路径,尽量避免将应用放在中文或者有包含空格的路径中
接下来启动Apache并访问http://localhost:5000即可。