python centos7使用uwsgi啟動服務
阿新 • • 發佈:2020-09-09
在django框架中,我們一般直接通過python manage.py runserver來啟動提供服務,但是如果生產環境此方法不可行,而且容易導致異常退出,於是需要藉助uwsgi來作為守護程序。
操作思路:
- 伺服器中安裝uwsgi外掛
- 新建uwsgi.ini檔案,寫入uwsgi需要的引數
- 安裝nginx,並配置
uwsgi_pass 127.0.0.1:9496
代理轉發 - 啟動nginx、uwsgi
安裝uwsgi外掛
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
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 remote-fs.target nss-lookup.target [Service] ExecStart=/usr/local/bin/uwsgi -d --ini /home/dengzhixu/crawl_data/uwsgi.ini ExecStop=/bin/kill -s QUIT $MAINPID # Requires systemd version 211 or newer #RuntimeDirectory=uwsgi Restart=always KillSignal=SIGQUIT Type=forking StandardError=syslog NotifyAccess=all [Install] WantedBy=multi-user.target
開啟自啟動,並啟動
systemctl start uwsgi.service
systemctl enable uwsgi.service
參考文獻:
https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html