Nginx + uWSGI start Python application service

Nginx + uWSGI start Python application service

Reference and thanks: Erick-LONG and
Ziqiangxuetang uWSGI is a web application server, which has functions such as application server, proxy, process management and application monitoring. It supports the WSGI protocol, and it also supports its own uWSGI protocol. The protocol is said to have very high performance and low memory usage, which is about half of mod_wsgi. I have not tested it. It also supports multi-application management and application performance monitoring. Although uWSGI itself can be used directly as a web server, it is generally recommended to use it as an application server together with Nginx, which can better utilize the powerful functions of Nginx on the web side. In this article, we will introduce how to build a uWSGI+Ngnix environment to run Python applications.

Install uWSGI

(env27)wfq@ubuntu:~$ sudo env27/bin/pip install uwsgi

However, there are some minor problems with viewing the installation log, and I don't know if it will affect the subsequent use:

Could not find .egg-info directory in install record for uwsgi

uwsgi part of the installation log:

    ############## end of uWSGI configuration #############
    total build time: 41 seconds
    *** uWSGI is ready, launch it with /home/wfq/python27/bin/uwsgi ***
  Could not find .egg-info directory in install record for uwsgi
Successfully installed uwsgi
Cleaning up...
(env27)wfq@ubuntu:~$ 
WSGI application demo: uwsgi_test.py

Write a Hello World WSGI application and save it in the uwsgi_test.py file:

wfq@ubuntu:~/ops$ pwd
/home/wfq/ops
wfq@ubuntu:~/ops$ ls
8888  9999  asb  db.sqlite3  django_wsgi.py  django_wsgi.pyc  logs  manage.py  myapp.log  nohup.out  ops  test  tools  uwsgi_socket.ini  uwsgi_test.py
wfq@ubuntu:~/ops$ cat uwsgi_test.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]
wfq@ubuntu:~/ops$

To run it in uWSGI, execute the command:

wfq@ubuntu:~/ops$ /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py &

Check the boot status:

wfq@ubuntu:~/ops$ 
wfq@ubuntu:~/ops$ ps -ef | grep 9999
wfq       31035  30441  0 19:54 pts/0    00:00:00 /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py
wfq       31036  31035  0 19:54 pts/0    00:00:00 /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py
wfq       31039  30441  0 19:55 pts/0    00:00:00 grep --color=auto 9999
wfq@ubuntu:~/ops$ 

Access check succeeded, so Nice
write picture description here

Command description:
The "--http" parameter in the above command specifies the HTTP listening address and port, and the "--wsgi-file" parameter specifies the WSGI application entry. uWSGI will automatically search for the application object named "application" and call it.

Further, uWSGI can support multi-process and multi-threaded application startup, and can also monitor the running status of the application. We changed the startup command to:

wfq@ubuntu:~/ops$ /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py --master --processes 2 --threads 2 --stats 127.0.0.1:8999

After executing it, uWSGI will start 2 application processes, each with 2 threads, and a master master process (monitor the status of other processes, and restart if a process dies). At the same time, you can access "127.0.0.1:8999" to get the application running information in JSON format (I failed to check and skip it for the time being). uWSGI also provides a tool command "uwsgitop" to monitor the running status of the application like top. You can Use pip to install it.

wfq@ubuntu:~/nginx/logs$ ps -ef | grep 9999
wfq       31063  30441  0 20:00 pts/0    00:00:00 /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py --master --processes 2 --threads 2 --stats 127.0.0.1:8999
wfq       31064  31063  0 20:00 pts/0    00:00:00 /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py --master --processes 2 --threads 2 --stats 127.0.0.1:8999
wfq       31065  31063  0 20:00 pts/0    00:00:00 /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py --master --processes 2 --threads 2 --stats 127.0.0.1:8999
wfq       31066  31063  0 20:00 pts/0    00:00:00 /home/wfq/python27/bin/uwsgi --http :9999 --wsgi-file uwsgi_test.py --master --processes 2 --threads 2 --stats 127.0.0.1:8999
wfq       31070  30476  0 20:00 pts/2    00:00:00 grep --color=auto 9999
wfq@ubuntu:~/nginx/logs$ 

There are too many parameters in the above command. We can write the parameters in the configuration file and specify the configuration file when starting uWSGI. The configuration file can be in the format of key-value pairs, or it can be in XML or YAML format. Here we use the format of key-value pairs.

ini configuration file to start

Let's create a configuration file "uwsgi_socket.ini":
where the module is: /home/wfq/ops/django_wsgi.py
is the django_wsgi.py configuration file in the chdir directory

wfq@ubuntu:~/ops$ pwd
/home/wfq/ops
wfq@ubuntu:~/ops$ cat uwsgi_socket.ini 
[uwsgi]
socket=:8888
chdir=/home/wfq/ops
pythonpath=/home/wfq/python27/bin/python
module=django_wsgi
processes=2
daemonize=/home/wfq/ops/logs/uwsgi.log
wfq@ubuntu:~/ops$ 
The startup command is simplified to:
wfq@ubuntu:~$ sudo /home/wfq/python27/bin/uwsgi /home/wfq/ops/uwsgi_socket.ini
[uWSGI] getting INI configuration from /home/wfq/ops/uwsgi_socket.ini
wfq@ubuntu:~$ 
wfq@ubuntu:~$ ps -ef | grep uwsgi
root      31916      1  7 14:09 ?        00:00:00 /home/wfq/python27/bin/uwsgi /home/wfq/ops/uwsgi_socket.ini
root      31921  31916  0 14:09 ?        00:00:00 /home/wfq/python27/bin/uwsgi /home/wfq/ops/uwsgi_socket.ini
wfq       31923  31777  0 14:09 pts/2    00:00:00 grep --color=auto uwsgi
wfq@ubuntu:~$ sudo lsof -i:8888
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
uwsgi   31916 root    3u  IPv4 145255      0t0  TCP *:8888 (LISTEN)
uwsgi   31921 root    3u  IPv4 145255      0t0  TCP *:8888 (LISTEN)
uwsgi   31921 root    5u  IPv4 147033      0t0  TCP 192.168.174.132:8888->192.168.174.1:62511 (ESTABLISHED)
wfq@ubuntu:~$ 
wfq@ubuntu:~$ ps -ef | grep 31921
root      31921  31916  0 14:09 ?        00:00:00 /home/wfq/python27/bin/uwsgi /home/wfq/ops/uwsgi_socket.ini
wfq       32036  31777  0 14:30 pts/2    00:00:00 grep --color=auto 31921
wfq@ubuntu:~$ 

configure nginx

Copy a copy of ops.conf in /home/wfq/nginx/conf. The main configuration is as follows. The configuration indicates that Nginx will forward all received requests to the "192.168.174.132:8888" port, which is the uWSGI server.

    server {
        listen 8000;
        server_name 192.168.174.132;
        access_log /home/wfq/nginx/logs/ops.access.log;
        location / {
            include uwsgi_params;
            uwsgi_pass 192.168.174.132:8888;
        }
    }

Start nginx:

~/nginx/sbin/nginx -c ~/nginx/conf/ops.conf
wfq@ubuntu:~$ ps -ef | grep nginx
root      32006      1  0 14:23 ?        00:00:00 nginx: master process /home/wfq/nginx/sbin/nginx -c /home/wfq/nginx/conf/ops.conf
nobody    32007  32006  0 14:23 ?        00:00:00 nginx: worker process                                      
wfq       32009  31777  0 14:23 pts/2    00:00:00 grep --color=auto nginx
wfq@ubuntu:~$ 
wfq@ubuntu:~$ lsof -i:8000
wfq@ubuntu:~$ sudo lsof -i:8000
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   32006   root    8u  IPv4 146740      0t0  TCP *:8000 (LISTEN)
nginx   32007 nobody    8u  IPv4 146740      0t0  TCP *:8000 (LISTEN)
wfq@ubuntu:~$ 

Browser access port 8000
write picture description here

At this point, the configuration record is completed. The book that I started writing last night was out of power. After the interruption, I continue to complete it today. There may be some omissions.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325341398&siteId=291194637