uWSGI
uWSGI is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C.
Contents
Installation
Install package uwsgi in the official repositories. Note, the package does not come with plugins as it is just a compact package. External plugins have to be installed separately. It is a very efficient software due to the reason it is written in C. There are alternatives written in Python like gunicornAUR, but they are slower inherently.
Starting service
To enable the uWSGI service by default at start-up, run:
# systemctl enable uwsgi@helloworld
This will enable the service for the application configured in /etc/uwsgi/helloworld.ini
. Otherwise, you can also enable it through the socket interface with the following command:
# systemctl enable uwsgi@helloworld.socket
Alternatively, you can run the Emperor mode service. This mode enables a single uWSGI instance to run a bunch of different apps (called vassals) using a single main supervisor (called emperor). To enable it, type:
# systemctl enable emperor.uwsgi
You can also use the socket:
# systemctl enable emperor.uwsgi.socket
The configuration for this sits in /etc/uwsgi/emperor.ini
.
Configuring
uWSGI is configured in /etc/uwsgi/
. Details can be found in the uWSGI documentation.
Application configuration
The following is a simple example to get python support. You may need to install the uwsgi-plugin-python or uwsgi-plugin-python2 plugin.
[uwsgi] chdir = /srv/http/helloworld module = helloworld plugins = python
It is also possible to run uWSGI separately with the following syntax for instance:
uwsgi --socket 127.0.0.1:3031 --plugin python2 --wsgi-file ~/foo.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 --uid --gid
Php applications
Install the php plugin for uWSGI: uwsgi-plugin-php
/etc/uwsgi/mysite.ini
[uwsgi] ; maximum number of worker processes processes = 4 ; the user and group id of the process once it’s started uid = http gid = http socket = /run/uwsgi/%n.sock master = true chdir = /srv/http/%n ; php plugins = php ; jail our php environment php-docroot = /srv/http/%n php-index = index.php ; clear environment on exit vacuum = true
Nginx configuration
location = /index.php { include uwsgi_params; uwsgi_modifier1 14; uwsgi_pass unix:/run/uwsgi/mysite.sock; }
Nginx configuration
location / { root /usr/share/nginx/html; index index.html index.htm; include uwsgi_params; # uwsgi_pass unix:/var/run/uwsgi/helloworld.sock; uwsgi_pass 127.0.0.1:3031; }
Nginx configuration (with chrooted Nginx)
First create ini file that will point to your application:
/etc/uwsgi/application1.ini
[uwsgi] chroot = /srv/http chdir = /www/application1 wsgi-file = application1.py plugins = python socket = /run/application1.sock uid = http gid = http threads = 2 stats = 127.0.0.1:9191 vacuum = true
Since we are chrooting to /srv/http
above configuration will result in following unix socket being created /srv/http/run/application1.sock
You will need to disable notifications within your service file:
/etc/systemd/system/multi-user.target.wants/uwsgi\@application1.service
[Unit] Description=uWSGI service unit After=syslog.target [Service] PIDFile=/run/%I.pid RemainAfterExit=yes ExecStart=/usr/bin/uwsgi --ini /etc/uwsgi/%I.ini ExecReload=/bin/kill -HUP $MAINPID ExecStop=/bin/kill -INT $MAINPID Restart=always StandardError=syslog KillSignal=SIGQUIT [Install] WantedBy=multi-user.target
Once modified run:
# systemctl daemon-reload
Enable your service:
# systemctl enable uwsgi@application1
Start the service:
# systemctl start uwsgi@application1
Edit /srv/http/etc/nginx/nginx.conf
and add new server
section within it that would contain at least following:
/srv/http/etc/nginx/nginx.conf
... server { listen 80; server_name 127.0.0.1; location / { root /www/application1; include uwsgi_params; uwsgi_pass unix:/run/application1.sock; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } ...
Restart nginx
# systemctl restart nginx
At this point your application should be served, issue 127.0.0.1
on your browser.