Chow's Notes

python wsgi web程序部署方式记录

用web.py框架写的程序

一、Nginx (做代理)+Gunicorn(后端服务器)

因为gunicorn是开启多个worker进程,如果程序有一个全局的对象,需要实时更新访问共享,各个进程之间是不能共享的,可以视情况只开一个worker

二、Nginx(FastCGI)+Spawn-fcgi +flup

nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 8809;
server_name 10.100.113.48;
access_log logs/wstapp_access.log;
error_log logs/wstapp_error.log;
location /static {
root /var/apps/WordsServiceApp;
}
location / {
include fastcgi_params;
include fastcgi.conf;
fastcgi_param PATH_INFO $fastcgi_script_name; # [2]
fastcgi_pass 127.0.0.1:9901;
}
}

启动文件:

1
2
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()

或者

1
2
3
import flup.server.fcgi as flups
flups.WSGIServer(application, multithreaded=True, multiprocess=False, bindAddress=('127.0.0.1', 5678)).run()

启动命令

1
spawn-fcgi -d /var/apps/WordsServiceApp/ -f /var/apps/WordsServiceApp/main.py -a 127.0.0.1 -p 9901 -n

参数 n 显示程序打印信息

web.py的session 在这种部署方式有问题,每次请求session会失效

三、Apache2 (apr,apr-util)+ mod_wsgi

apr-1.5.2.tar.gz

1
./configure --prefix=/usr/local/apr

apr-util-1.5.4

1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config

httpd-2.4.23

1
./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/

mod_wsgi-4.3.0

centos编译安装mod_wsgi的问题

安装mod_wsgi到apache

mod_wsgi-4.3.0

1
2
3
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/local/python2.7/bin/python
make
make install

编译时 如果报错:

1
2
/usr/bin/ld: /usr/local/python2.7/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32 against `.
rodata.str1.8' can not be used when making a shared object;

已经安装的libpython2.7.so.1.0有问题(改版本可能是被编译成了32位),需要重新编译安装python
1.注释掉 python源码下setup.py下的两行代码

1
2
3
4
def detect_modules(self):
# Ensure that /usr/local is always used
#add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
#add_dir_to_list(self.compiler.include_dirs, '/usr/local/in

1
2
./configure --prefix=/usr/local/python2.7 --enable-shared
make && make install

后会在/usr/local/python2.7/lib目录下重新生成 libpython2.7.so.1.0,
将其拷贝到/usr/local/lib

1
/sbin/ldconfig -v

重新编译 mod_wsgi

最后输出

1
chmod 755 /usr/local/apache2/modules/mod_wsgi.so

证明成功

==============

httpd.conf的配置(Apache版本Apache/2.4.23)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<VirtualHost *:8891>
WSGIScriptAlias / /var/apps/WordsServiceApp/main.py
Alias /static/ /var/apps/WordsServiceApp/static/
ServerName 10.100.113.48
#ServerName example.com
#ServerAlias www.example.com
<Directory /var/apps/WordsServiceApp/static>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /var/apps/WordsServiceApp/>
Require all granted
</Directory>
ErrorLog /var/apps/WordsServiceApp/wst_httpd.error.log
LogLevel warn
</VirtualHost>

报错找不到模块
main.py中加入

1
2
3
import os
import sys
sys.path.append(os.path.dirname(__file__))#only running for in apache

如果程序 有用路径,最好都用绝对路径。

因为http也是启动了多个进程来处理请求,所以全局的对象也不能共享,

不会预加载处理 ,并不会随服务器启动而执行

=====================

对于需要全局共享的简单数据类型,可以用外部存储 比如 memecached缓存,redis,文件。

但是类实例的复杂对象,要么开一个进程处理,要么 改程序 换种方法实现。

四、uwsgi

没实践过

好像 该服务器 ,有文件更新,会自动重启