如何正確的使用uwsgi

  • 作者:由 匿名使用者 發表于 攝影
  • 2021-08-06

如何正確的使用uwsgi 匿名使用者 1級 2017-07-14 回答

如何正確的使用uwsgi

簡單的安裝過程可以在這裡找到,這裡主要說一下如何配置uwsgi的服務,將uwsgi服務加入系統程序,你可以使用如下兩種方式安裝

apt-get

apt-get install uwsgi

該命令會自動將uwsgi安裝為一個服務,在 /etc/init。d/uwsgi 下,你可以使用以下命令來管理該服務:

sudo /etc/init。d/uwsgi start|stop|restart|reload

sudo service uwsgi start|stop|restart|reload

pip

pip install uwsgi

該命令會將uwsgi安裝在 /usr/local/bin/uwsgi ,你需要手動新增服務,建立 /etc/ini/uwsgi。conf 檔案,內容如下:

description “uWSGI Emperor”

start on runlevel [2345]

stop on runlevel [!2345]

respawn

exec /usr/local/bin/uwsgi ——emperor /etc/uwsgi/vassals/ ——logto /var/log/uwsgi/uwsgi。log

然後你就可以透過如下的命令來管理uwsgi的程序了:

sudo initctl start|stop|restart|reload| uwsgi

sudo service uwsgi start|stop|restart|reload

為你的網站建立配置檔案

在 /etc/uwsgi/vassals/ 目錄下建立一個ini的配置檔案,內容如下:

[uwsgi]

virtualenv=/home/cungen/sdk/python/env/

chdir=/var/www/api。cungen。tk

chmod-socket=777

chown-socket=www-data

module=www。wsgi

env=DJANGO_SETTINGS_MODULE=www。settings

master=True

vacuum=True

socket=/tmp/api。cungen。tk。sock

pidfile=/tmp/api。cungen。tk。pid

daemonize=/var/log/uwsgi/api。cungen。tk。log

gid=www-data

uid=www-data

virtualenv為你使用的virtualenv的路徑,chdir為你的專案路徑,module為你專案中的模組,%n改為你的專案名稱即可

修改nginx中專案的配置檔案

如我的為 /etc/nginx/sites-available/api。local。cg ,內容如下:

server {

listen 80;

root /var/www/api。cungen。tk;

index index。html index。htm;

access_log /var/log/nginx/api。cungen。tk-access;

error_log /var/log/nginx/api。cungen。tk-error error;

server_name api。cungen。tk;

location / {

try_files $uri @django;

}

location @django {

uwsgi_pass unix:///tmp/api。cungen。tk。sock;

include uwsgi_params;

}

## caches

include /etc/nginx/conf。d/caches。conf;

}

重啟服務:

sudo service nginx reload

sudo service uwsgi reload

如何正確的使用uwsgi 聖域神話 1級 2017-07-14 回答

多程序prefork已是wsgi最成熟的應用方法了, 在各種評測中, uwsgi的效能都是不錯的。 python本身執行緒不好使, 所以建議每一個worker程序裡只放一個執行緒, 維護一個數據庫連線即可, 無需使用連線池。 更好的方案是使用非同步模型

Top