How to configure the Nginx + Python environment through uWSGI in Linux,
1. Install the ppa with uwsgi in ubuntu:
add-apt-repository ppa:stevecrozz/ppa apt-get update apt-get install uwsgi
2. Replace mod_wsgi with uwsgi
The overall configuration of Nginx is not mentioned here. If you already understand the basic configuration of Nginx, uwsgi is similar to this Configuration:
location / { include uwsgi_params uwsgi_pass 127.0.0.1:9090 }
For example, django is:
....... from django.core.handlers.wsgi import WSGIHandler application = WSGIHandler()
Then run uwsgi listening 9090, where-w is followed by the module name, that is, the configured myapp
uwsgi -s :9090 -w myapp
The website has been deployed.
3. uwsgi Parameters
The above is the simplest deployment of a single project. uwsgi still has many commendable features, such:
Four concurrent threads:
uwsgi -s :9090 -w myapp -p 4
Main Control thread + 4 threads:
uwsgi -s :9090 -w myapp -M -p 4
If the client executes the command for more than 30 seconds, the following command is skipped:
uwsgi -s :9090 -w myapp -M -p 4 -t 30
Limited Memory Space 128 M:
uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128
Automatic respawn with over 10000 req services:
uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000
Background running:
uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log
4. Configure multiple sites for uwsgi
To allow multiple sites to share a uwsgi service, you must run uwsgi as a virtual site: Remove "-w myapp" and "-vhost ":
uwsgi -s :9090 -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log --vhost
Virtualenv must be configured. virtualenv is a useful virtual environment tool for Python:
apt-get install Python-setuptools easy_install virtualenv
Set up one or more app benchmark environments:
virtualenv /var/www/myenv
Application environment. The software installed in this environment is only valid in this environment:
source /var/www/myenv/bin/activate pip install django pip install mako ...
Finally, configure nginx. Note that each site must occupy one server separately. The same server is directed to different applications in different locations and may fail for some reason. This is a bug.
server { listen 80; server_name app1.mydomain.com; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; uwsgi_param UWSGI_PYHOME /var/www/myenv; uwsgi_param UWSGI_SCRIPT myapp1; uwsgi_param UWSGI_CHDIR /var/www/myappdir1; } } server { listen 80; server_name app2.mydomain.com; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; uwsgi_param UWSGI_PYHOME /var/www/myenv; uwsgi_param UWSGI_SCRIPT myapp2; uwsgi_param UWSGI_CHDIR /var/www/myappdir2; } }
In this way, restart the nginx service and the two sites will be able to share a uwsgi service.
Articles you may be interested in:
- Nginx + Python web. py and Django framework Environment on Linux
- Tutorial on configuring Python + Django + Nginx + uWSGI + MySQL in Debian