一、安装uwsgi
sudo pip install uwsgi
安装完成后查看uwsgi版本:
y@ubuntu:~$ uwsgi --version
2.0.17.1
二、nginx配置
打开/usr/local/nginx/conf/nginx.conf配置文件,在http模块下的server模块下新增以下内容:
location /uwsgi { #这段配置表示Nginx会将 /uwsgi路径下的请求转发到127.0.0.1:9090端口上,即uWSGI服务器上
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
添加完内容后的配置文件内容如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location /uwsgi { #这段配置表示Nginx会将 /uwsgi路径下的请求转发到127.0.0.1:9090端口上,即uWSGI服务器上
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
uwsgi就是http模块中的location地址。
三、编写uwsgi程序
在自己的主目录下新建一个目录uwsgi,然后新建一个python脚本,名字为:uwsgi_helloworld.py,内容如下:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
继续新建一个python脚本,,名字为:uwsgi_helloworld2.py,内容如下:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World2!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
四、启动
1.启动nginx
sudo /usr/local/nginx/sbin/nginx
nginx相关命令的使用可以参考Ubuntu下nginx+python+fastcgi环境搭建与测试
2.启动uwsgi
进入上面编写好的uwsgi_helloworld.py脚本所在的目录,打开命令行,执行:
sudo uwsgi -s 127.0.0.1:9090 --plugin python --wsgi-file uwsgi_helloworld.py
如下:
y@ubuntu:~/uwsgi$ sudo uwsgi -s 127.0.0.1:9090 --plugin python --wsgi-file uwsgi_helloworld.py
open("./python_plugin.so"): No such file or directory [core/utils.c line 3721]
!!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No such file or directory !!!
*** Starting uWSGI 2.0.17.1 (64bit) on [Tue Sep 18 19:34:16 2018] ***
compiled with version: 7.3.0 on 19 September 2018 01:43:26
os: Linux-4.15.0-33-generic #36-Ubuntu SMP Wed Aug 15 16:00:05 UTC 2018
nodename: ubuntu
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /home/y/uwsgi
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 31577
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:9090 fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x55ae7a3e04e0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55ae7a3e04e0 pid: 3753 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 3753, cores: 1)
因为根据nginx.conf配置文件,nginx会把uwsgi路径的请求转发到127.0.0.1:9090 端口,所以这里启动uwsgi时监听的是127.0.0.1:9090端口
若想停止uwsgi可结合 ps -aux 和 sudo kill -9 命令,如下:
y@ubuntu:~/uwsgi$ ps -aux | grep 9090
root 3736 0.0 0.0 64748 3972 pts/1 T 19:31 0:00 sudo uwsgi -s 127.0.0.1:9090 --plugin python --wsgi-file uwsgi_helloworld.py
root 3737 0.0 0.1 53616 12444 pts/1 T 19:31 0:00 uwsgi -s 127.0.0.1:9090 --plugin python --wsgi-file uwsgi_helloworld.py
y 3745 0.0 0.0 21536 1000 pts/1 R+ 19:32 0:00 grep --color=auto 9090
y@ubuntu:~/uwsgi$ sudo kill -9 3736 3737
[1]+ Killed sudo uwsgi -s 127.0.0.1:9090 --plugin python --wsgi-file uwsgi_helloworld.py
3.打开浏览器,输入:http://localhost/uwsgi (nginx配置文件中可以看到http监听的是80端口,而http请求默认就是80端口,所以这里可以不用填写端口号),可以看到输出结果:
Hello World!
五、以配置文件的方式启动uwsgi
上面启动uwsgi的命令:sudo uwsgi -s 127.0.0.1:9090 --plugin python --wsgi-file uwsgi_helloworld.py
参数非常多,我们可以将参数写在配置文件里,启动uwsgi时指定配置文件即可。配置文件可以是键值对的格式,也可以是XML,YAML格式,这里我们使用键值对的格式。在之前创建的uwsgi目录下新建一个配置文件uwsgiconf.ini,内容如下:
[uwsgi]
socket=127.0.0.1:9090
wsgi-file=uwsgi_helloworld.py
plugin=python
master=true
processes=4
threads=2
上述的配置文件表示uWSGI将启动4个进程,每个进程有2个线程,另外还有一个master主进程(监控其他进程状态,如果有进程死了,则重启)。
然后启动uWSGI:
y@ubuntu:~/uwsgi$ sudo uwsgi uwsgiconf.ini
再打开浏览器,输入:http://localhost/uwsgi
可以看到也显示了:Hello World!
六、部署多个应用
一个Nginx,可以同时运行多个应用,不一定是python脚本应用。我们期望通过不同的路径路由到不同的应用,
在nginx的配置文件nginx.conf中的http模块的server模块中加入以下内容:
location /uwsgi_helloworld {
include uwsgi_params;
uwsgi_param SCRIPT_NAME /uwsgi_helloworld;
uwsgi_pass 127.0.0.1:9090;
}
location /uwsgi_helloworld2 {
include uwsgi_params;
uwsgi_param SCRIPT_NAME /uwsgi_helloworld2;
uwsgi_pass 127.0.0.1:9090;
}
这里我们定义了2个uWSGI参数”SCRIPT_NAME”,值为应用的路径,分别为/uwsgi_helloworld 和 /uwsgi_helloworld2
完整的nginx.conf文件内容如下所示:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location /uwsgi_helloworld {
include uwsgi_params;
uwsgi_param SCRIPT_NAME /uwsgi_helloworld;
uwsgi_pass 127.0.0.1:9090;
}
location /uwsgi_helloworld2 {
include uwsgi_params;
uwsgi_param SCRIPT_NAME /uwsgi_helloworld2;
uwsgi_pass 127.0.0.1:9090;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
接下来,在uWSGI的启动配置中,注释掉”wsgi-file”项,并加上:
mount=/uwsgi_helloworld=uwsgi_helloworld.py
mount=/uwsgi_helloworld2=uwsgi_helloworld2.py
manage-script-name=true
“mount”参数表示将/uwsgi_helloworld地址路由到uwsgi_helloworld.py,将/uwsgi_helloworld2地址路由到uwsgi_helloworld2.py
”manage-script-name”参数表示启用之前在Nginx里配置的”SCRIPT_NAME”参数。
完整的uwsgiconf.ini内容如下:
[uwsgi]
socket=127.0.0.1:9090
#wsgi-file=uwsgi_helloworld.py
mount=/uwsgi_helloworld=uwsgi_helloworld.py
mount=/uwsgi_helloworld2=uwsgi_helloworld2.py
manage-script-name=true
plugin=python
master=true
processes=4
threads=2
然后重启nginx:
y@ubuntu:~$ sudo /usr/local/nginx/sbin/nginx -s stop
y@ubuntu:~$ sudo /usr/local/nginx/sbin/nginx
重启uwsgi,杀掉uwsgi进程后,执行sudo uwsgi uwsgiconf.ini重启即可:
y@ubuntu:~/uwsgi$ sudo uwsgi uwsgiconf.ini
打开浏览器,输入:http://localhost/uwsgi_helloworld
看到显示:Hello World!
输入:http://localhost/uwsgi_helloworld2
看到显示:Hello World2!
七、使用Python内置的WSGI服务器
如果只是用于开发测试使用,可以不用上面那么麻烦,需要安装和配置等很多操作步骤,可以直接使用Python内置的WSGI服务器,这个模块叫wsgiref,是用Python编写的WSGI服务器的参考实现,具体可参考网上的文章使用。
参考:
nginx+uwsgi+python捕获http消息并处理(一)
python nginx+uwsgi+WSGI 处理请求详解