在django框架中,我们一般直接通过python manage.py runserver来启动提供服务,但是如果生产环境此方法不可行,而且容易导致异常退出,于是需要借助uwsgi来作为守护进程。
操作思路:
- 服务器中安装uwsgi插件
- 新建uwsgi.ini文件,写入uwsgi需要的参数
- 安装nginx,并配置
uwsgi_pass 127.0.0.1:9496
代理转发 - 启动nginx、uwsgi
安装uwsgi插件
代码语言:javascript复制pip install uwsgi
新建uwsgi.ini文件,写入uwsgi需要的参数
可直接在代码根目录中创建uwsgi.ini
文件,参考如下:
[uwsgi]
socket = 127.0.0.1:9496
chdir = /home/dengzhixu/crawl_data
wsgi-file = /home/dengzhixu/crawl_data/yibo_crawl_data/wsgi.py
processes = 4
threads = 2
#stats = 0.0.0.0:9496
buffer-size = 65536
#daemonize = /var/log/uwsgi.log
安装nginx,配置uwsgi_pass
转发
安装nginx步骤省略,可直接去lnmp.org
下载集成nginx
nginx添加vhost配置文件,参考如下
server
{
listen 9495;
#listen [::]:;
server_name crawl.com ;
index index.html index.htm default.html default.htm;
root /home/dengzhixu/crawl_data;
include rewrite/other.conf;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9496;
uwsgi_param UWSGI_SCRIPT ./yibo_crawl_data/demosite.wsgi;
uwsgi_param UWSGI_CHDIR /home/dengzhixu/crawl_data;
index index.html index.htm;
client_max_body_size 35m;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /.
{
deny all;
}
access_log /home/wwwlogs/crawl.com.log;
启动nginx、uwsgi
代码语言:javascript复制nginx
uwsgi -d --ini /home/dengzhixu/crawl_data/uwsgi.ini
配置systemd
自启动
创建一个systemd服务文件/lib/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Emperor
#After=syslog.target
After=network.target
[Service]
#PIDFile=/run/uwsgi.pid
ExecStart=/usr/local/bin/uwsgi --ini /home/dengzhixu/crawl_data/uwsgi.ini
#ExecStop=/usr/local/bin/uwsgi --stop $MAINPID
#ExecReload=/usr/local/bin/uwsgi -d --reload $MAINPID
# Requires systemd version 211 or newer
#RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
#Type=forking
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
开启自启动,并启动
代码语言:javascript复制systemctl start uwsgi.service
systemctl enable uwsgi.service
参考文献:
https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html