部署python web应用有很多种方法,最常用的方法:
客户端请求 >> nginx >> uwsgi >> python web应用
安装uwsgi
apt-get install uwsgi
安装nginx
apt-get install nginx
配置uwsgi,它是一个python web server,类似的还有gunicorn等。
例如python web项目地址是:/home/www/test/
新建一个ini配置文件:
[uwsgi]
socket = /home/www/test.sock #uwsgi和nginx通信文件
chown-sock = root:www-data
chmod-sock = 660
chdir = /home/www/test #项目目录
wsgi-file = /home/www/test/wsgi.py #项目wsgi文件
master = true #允许主进程存在
pidfile = /home/www/test.pid #pid文件位置,记录主进程pid号
processes = 4 #线程数
vacuum = true #服务器退出后自动清理环境
daemonize = /var/log/test.log #日志文件
socket-timeout=10
启动uwsgi:
uwsgi --ini /home/www/test_uwsgi.ini
配置nginx,用它来接受客户端请求并发送给uwsgi,同时处理静态文件。
在/etc/nginx/sites-enabled/
创建配置文件test_nginx
server {
server_name xxx.com;
charset UTF-8;
access_log /var/log/test_access.log;
error_log /var/log/test_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/www/test.sock;
uwsgi_read_timeout 2100;
}
#静态文件配置
location /static/ {
expires 30d;
add_header Cache-Control private;
alias /home/www/test/static/;
}
}
启动nginx:
service nginx start
测试即可。
确定python web项目在本地开发环境能成功运行。查看日志文件确认uwsgi启动失败原因。大多数是由于web项目的配置导致!可以使用一个最简单的web应用(Helloword)测试环境搭建情况。