Centos uwsgi配置和使用

廢話還是不多說,開整。

1、首先安裝uwsgi,方法很簡單

 pip3 install uWSGI

2、安裝好之後,我們會在django中使用

配置uwsgi.ini

在Django中settings.py同級目錄下配置uwgi.ini配置文件

[uwsgi]
# 配置服務器的監聽ip和端口,讓uWSGI作爲nginx的支持服務器的話,設置socke就行;如果要讓uWSGI作爲單獨的web-server,用http
socket = 127.0.0.1:3309

# 配置項目目錄(此處設置爲項目的根目錄)
chdir = /root/www

# 配置入口模塊 (django的入口函數的模塊,即setting同級目錄下的wsgi.py)
wsgi-file = www/wsgi.py

# 開啓master, 將會多開一個管理進程, 管理其他服務進程
master = True

# 服務器開啓的進程數量
processes = 4

# 以守護進程方式提供服, 輸出信息將會打印到log中
daemonize = wsgi.log

# 服務器進程開啓的線程數量
threads = 4
www
# 退出的時候清空環境變量
vacuum = true

# 進程pid
pidfile = uwsgi.pid

# 配uWSGI搜索靜態文件目錄(及django項目下我們存放static文件的目錄,用uWSGI作爲單獨服務器時才需要設置,此時我們是用nginx處理靜態文件)
# check-static = /home/python/Desktop/ttsx

保存後,使用命令啓動。

uwsgi --ini uwsgi.ini

是否啓動成功,查看一下進程:

ps aux | grep uwsgi

 啓動成功。

如何設置,開機自啓動服務

在/etc/systemd/system 目錄下,創建server_uwsgi.service 文件

寫入如下內容:

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
[Unit]
Description=uWSGI serve Django
After=network.target

[Service]
WorkingDirectory=/root/official 
ExecStart=/usr/local/bin/uwsgi --ini /root/official/www/uwsgi.ini
ExecStop=/usr/local/bin/uwsgi --stop /root/official/www/uwsgi.pid
ExecReload=/usr/local/bin/uwsgi --reload /root/official/www/uwsgi.pid
[Install]
WantedBy=multi-user.target

[Unit]
Description=uWSGI instance to serve myproject   隨便起名
After=network.target

[Service]
[Unit]
Description=uWSGI serve Django 隨便起名
After=network.target

[Service]
WorkingDirectory=/root/official  這裏是你的Django根目錄
ExecStart=/usr/local/bin/uwsgi --ini /root/official/www/uwsgi.ini  這是你的啓動文件
ExecStop=/usr/local/bin/uwsgi --stop /root/official/www/uwsgi.pid 這是啓動pid文件
ExecReload=/usr/local/bin/uwsgi --reload /root/official/www/uwsgi.pid 這是重啓pid文件
[Install]
WantedBy=multi-user.target
 

下面在將server_uwsgi.service添加到服務中:


systemctl enable /etc/systemd/system/server_uwsgi.service

之後,我們可以使用命令啓動

systemctl stop server_uwsgi.service 關閉uwsgi服務
systemctl start server_uwsgi.service 開啓uwsgi服務
systemctl restart server_uwsgi.service 重啓uwsgi服務

這裏要注意的是,uwsgi.ini文件中,不能設置

daemonize = wsgi.log

需要設置成

log = wsgi.log

這樣纔可以開機自啓動。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章