Nginx+Python+uwsgi+Django的web開發環境安裝及配置
nginx安裝
nginx的安裝這里就略過了...
python安裝
通常系統已經自帶了,這里也略過
uwsgi安裝
安裝步驟如下:
yum -y install python-devel
wget -c https://projects.unbit.it/downloads/uwsgi-2.0.14.tar.gz
tar zxf uwsgi-2.0.14.tar.gz
cd uwsgi-2.0.14
make
# python uwsgiconfig.py --build
報錯處理
plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
主要是沒有安裝python-devel
yum install python-devel
讓Nginx支持uwsgi配置
相關網址
http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html
#uwsgi_pass unix:///tmp/uwsgi.sock;
uwsgi_pass 127.0.0.1:6000;
include uwsgi_params;
uwsgi_params文件內容
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
集群配置
upstream uwsgicluster {
server unix:///tmp/uwsgi.sock;
server 192.168.1.235:3031;
server 10.0.0.17:3017;
}
uwsgi_pass uwsgicluster;
配置uwsgi
配置說明
http://uwsgi-docs.readthedocs.io/en/latest/Configuration.html
相關配置選項
http://uwsgi-docs.readthedocs.io/en/latest/Options.html
進入剛安裝uwsgi的目錄
執行./uwsgi
可以查看已經安裝成功
有多種方式可以進行uwsgi配置,可以使用命令行進行配置,也可以使用配置文件
配置文件有ini xml json yaml等4種方式的配置方式,任選一種
命令行示例:
uwsgi --http-socket :9090 --psgi myapp.pl
uwsgi 的參數:
-M 開啟Master進程
-p 4 開啟4個進程
-s 使用的端口或者socket地址
-d 使用daemon的方式運行, 注意, 使用-d后, 需要加上log文件地址, 比如-d /var/log/uwsgi.log
-R 10000 開啟10000個進程后, 自動respawn下
-t 30 設置30s的超時時間, 超時后, 自動放棄該鏈接
-limit-as 32 將進程的總內存量控制在32M
-x 使用配置文件模式
等價於配置文件
[uwsgi]
http-socket = :9090
psgi = myapp.pl
也可配置好后,通過命令行載入配置文件
uwsgi --ini http://uwsgi.it/configs/myapp.ini # HTTP
uwsgi --xml - # standard input
uwsgi --yaml fd://0 # file descriptor
uwsgi --json 'exec://nc 192.168.11.2:33000' # arbitrary executable
相關配置
[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
生產環境推薦開啟
master = true
threads = 2
stats = 127.0.0.1:9191
python自動重載,當修改py文件后,自動重新載入,僅開發時用
py-autoreload = 2
[uwsgi]
workdir = /var
ipaddress = 0.0.0.0
; start an http router on port 8080
http = %(ipaddress):8080
; enable the stats server on port 9191
stats = 127.0.0.1:9191
; spawn 2 threads in 4 processes (concurrency level: 8)
processes = 4
threads = 2
; drop privileges
uid = nobody
gid = nogroup
; serve static files in /var/www
static-index = index.html
static-index = index.htm
check-static = %(workdir)/www
; skip serving static files ending with .lua
static-skip-ext = .lua
; route requests to the CGI plugin
http-modifier1 = 9
; map /cgi-bin requests to /var/cgi
cgi = /cgi-bin=%(workdir)/cgi
; only .lua script can be executed
cgi-allowed-ext = .lua
; .lua files are executed with the 'lua' command (it avoids the need of giving execute permission to files)
cgi-helper = .lua=lua
; search for index.lua if a directory is requested
cgi-index = index.lua
https支持
openssl genrsa -out foobar.key 2048
openssl req -new -key foobar.key -out foobar.csr
openssl x509 -req -days 365 -in foobar.csr -signkey foobar.key -out foobar.crt
[uwsgi]
https = :9090,foobar.crt,foobar.key
chdir = path_to_web2py
module = wsgihandler
master = true
processes = 8
啟動uwsgi
uwsgi --ini /configs/myapp.ini
uwsgi --http-socket 127.0.0.1:3031 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
uwsgi --socket 127.0.0.1:3031 --chdir /home/foobar/myproject/ --wsgi-file myproject/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
重新啟動uwsgi
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid`
# or the convenience option --reload
uwsgi --reload /tmp/project-master.pid
# or if uwsgi was started with touch-reload=/tmp/somefile
touch /tmp/somefile
停止uwsgi
kill -INT `cat /tmp/project-master.pid`
# or for convenience...
uwsgi --stop /tmp/project-master.pid
開機啟動uwsgi
/etc/rc.local中添加啟動腳本,如
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log
xinted方式
相關文檔
http://uwsgi-docs.readthedocs.io/en/latest/Inetd.html
service uwsgi
{
disable = no
id = uwsgi-000
type = UNLISTED
socket_type = stream
server = /root/uwsgi/uwsgi
server_args = --chdir /root/uwsgi/ --module welcome --logto /tmp/uwsgi.log
port = 3031
bind = 127.0.0.1
user = root
wait = yes
}
關於socket,http,http-socket,uwsgi-socket的區別
socket綁定到特定的unix/tcp socket使用默認協議,如將py請求轉發至后端socket接口,如nginx轉發請求,可以開啟該接口
可以是一個端口:如 socket = :6000,對應的nginx配置為uwsgi_pass 127.0.0.1:6000
也可以是一個sock文件 socket = /tmp/uwsgi.sock,對應的nginx配置為uwsgi_pass unix:///path/to/your/mysite/mysite.sock;
http,綁定到特定的unix/tcp socket使用http協議,端口會暴露在外
http-socket綁定到特定的unix/tcp socket使用http協議,本地http協議,端口不會暴露在外
uwsgi-socket 綁定到特定的unix/tcp socket使用uwsgi的協議,類似socket,nginx也可轉發到該選項配置的端口
可以配置開機啟動,一個簡單的配置/etc/init/uwsgi.conf
配置文件添加以下代碼
# simple uWSGI script
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
exec uwsgi --master --processes 4 --die-on-term --socket :3031 --wsgi-file /var/www/myapp.wsgi
或者使用Emperor,可以配置多個app,在/etc/uwsgi目錄下添加多個配置
# Emperor uWSGI script
description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [06]
respawn
exec uwsgi --master --die-on-term --emperor /etc/uwsgi
nginx+uwsgi的配置實例
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
實戰nginx配置,測試用
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:6000; # for a web port socket (we'll use this first)
}
server {
listen 8080;
server_name uwsgi.com;
location /hi {
alias /home/www/hi/;
}
location ~* .*\.py($|/) {
# uwsgi_pass 127.0.0.1:6000;
uwsgi_pass django;
include uwsgi_params;
# uwsgi_param UWSGI_SCRIPT index;
# uwsgi_param UWSGI_PYHOME $document_root;
# uwsgi_param UWSGI_CHDIR $document_root;
}
access_log /home/httplogs/test.com-access.log main;
error_log /home/httplogs/test.com-error.log;
}
實戰uwsgi配置,測試用
[uwsgi]
# socket = :6000
# http = :6000
uwsgi-socket = :6000
# http-socket = :6000
master = true
chdir = /home/www/
wsgi-file = /home/www/test.py
processes = 4
stats = 127.0.0.1:9090
daemonize = /home/log/log.log
pidfile = /tmp/uwsgi.pid
vacuum = true
# disable-logging = true
py-autoreload = 2
多應用部署
http://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html?highlight=mount
[uwsgi]
socket = 127.0.0.1:3031
; mount apps
mount = /app1=app1.py
mount = /app2=app2.py
; rewrite SCRIPT_NAME and PATH_INFO accordingly
manage-script-name = true
使用virtualenv方式