Uwsgi: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 10: Line 10:
==Installing==
==Installing==
* pip3 install uwsgi
* pip3 install uwsgi
* Create virtual python environment
* pip3 install flask
virtualenv --clear --always-copy -p /usr/bin/python3 venv
* Activate the virtual environment and install flask
pip3 install flask


Now 1 application can be started (second is complaining about the python plugin and encodings module although the .ini is exactly the same)
==Configuration==


/etc/uwsgi-emperor/emperor.ini
[uwsgi]
  autoload = true
  no-orphan = true
  emperor = /etc/uwsgi-emperor/vassals
  master = true
  emperor-nofollow = true
  processes = 1
  workers = 2
  uid = www-data
  gid = www-data
  pidfile = /run/uwsgi/pid
  socket = /tmp/%n.sock
  chmod-socket = 666
  chown-socket = www-data
  log-date = false


==Stopping==
For all applications you can use this configuration file in /etc/uwsgi-emperor/vassels/app1.ini. uwsgi is installed using pip so no loading of a python module is needed.
Several suggestions made on the web:


This works:
[uwsgi]
  master = false
  processes = 1
  pidfile = /run/uwsgi/%n.pid
  vaccum = true
  chmod-socket = 666
  socket = /tmp/%n.sock
  uid = www-data
  gid = www-data
  pythonpath = /opt/flask/%n
  module = %n


<code>kill `pidof uwsgi`</code>
The %n will be replaced by the first part of the file-name (app1 in this example). In /opt/flask/app1 you can put your code.


Other suggestion that looks cleaner:
===Raspberry pi===
On my raspberry pi uwsgi was installed with some extensions to ease configuration. E.g. to use .xml file for configuration. It was also not installed by pip so the uwsgi-plugin-python3 needed to be installed too.


<code>uwsgi --stop /run/uwsgi/pid</code> (or whereever your pid-file is)
/etc/uwsgi/apps-available/emperor.xml
<syntaxhighlight lang=xml>
<uwsgi>
  <autoload>true</autoload>
  <no-orphan>true</no-orphan>
  <emperor>/etc/uwsgi/vassals-enabled</emperor>
  <master>true</master>
  <emperor-nofollow>true</emperor-nofollow>
  <processes>1</processes>
  <uid>www-data</uid>
  <gid>www-data</gid>
</uwsgi>
</syntaxhighlight>
 
/etc/uwsgi/apps-available/apps.xml
<syntaxhighlight lang=xml>
<uwsgi>
  <plugins>python3</plugins>
  <master>false</master>
  <processes>1</processes>
  <vaccum>true</vaccum>
  <chmod-socket>666</chmod-socket>
  <socket>/tmp/%n.sock</socket>
  <uid>www-data</uid>
  <gid>www-data</gid>
  <pythonpath>/var/www/flask/%n</pythonpath>
  <module>%n</module>
</uwsgi>
</syntaxhighlight>
 
Make them available
<syntaxhighlight lang=bash>
cd /etc/uwsgi/apps-enabled
ln -s ../apps-available/emperror.xml
cd /etc/uwsgi/vassals-enabled
ln -s ../apps-available/apps.xml app1.xml
ln -s ../apps-available/apps.xml app2.xml
</syntaxhighlight>
 
==Daemon management==
;<code>/usr/local/bin/uwsgi -d --ini /etc/uwsgi-emperor/emperor.ini --log-syslog</code>
:Start with logging to [[rsyslog]], add following line to the rsyslogd configuration
:<code>:programname, contains, "uwsgi" /var/log/uwsgi/uwsgi.log</code>
 
;<code>kill `pidof uwsgi`</code>
:This works for stopping all processes.
 
;<code>uwsgi --stop /run/uwsgi/pid</code>
:This looks cleaner but does not work on my system
 
;<code>uwsgi --reload /run/uwsgi/app1.pid</code>
:Restart a vassel (application)
;<code>kill -1 `cat /run/uwsgi/app1.pid`</code>
:Restart a vassel (application)

Latest revision as of 10:48, 10 August 2025

Lots of confusion going around on the net about installing and using uwsgi. Here my notes.

Used documentation

https://github.com/unbit/uwsgi/issues/1688

https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-applications-using-uwsgi-web-server-with-nginx

https://stackoverflow.com/questions/35262299/uwsgi-emperor-mode-not-working-outside-of-virtualenv

Installing

  • pip3 install uwsgi
  • pip3 install flask

Configuration

/etc/uwsgi-emperor/emperor.ini

[uwsgi]
 autoload = true
 no-orphan = true
 emperor = /etc/uwsgi-emperor/vassals
 master = true
 emperor-nofollow = true
 processes = 1
 workers = 2
 uid = www-data
 gid = www-data
 pidfile = /run/uwsgi/pid
 socket = /tmp/%n.sock
 chmod-socket = 666
 chown-socket = www-data
 log-date = false

For all applications you can use this configuration file in /etc/uwsgi-emperor/vassels/app1.ini. uwsgi is installed using pip so no loading of a python module is needed.

[uwsgi]
 master = false
 processes = 1
 pidfile = /run/uwsgi/%n.pid
 vaccum = true
 chmod-socket = 666
 socket = /tmp/%n.sock
 uid = www-data
 gid = www-data
 pythonpath = /opt/flask/%n
 module = %n

The %n will be replaced by the first part of the file-name (app1 in this example). In /opt/flask/app1 you can put your code.

Raspberry pi

On my raspberry pi uwsgi was installed with some extensions to ease configuration. E.g. to use .xml file for configuration. It was also not installed by pip so the uwsgi-plugin-python3 needed to be installed too.

/etc/uwsgi/apps-available/emperor.xml

 <uwsgi>
  <autoload>true</autoload>
  <no-orphan>true</no-orphan>
  <emperor>/etc/uwsgi/vassals-enabled</emperor>
  <master>true</master>
  <emperor-nofollow>true</emperor-nofollow>
  <processes>1</processes>
  <uid>www-data</uid>
  <gid>www-data</gid>
 </uwsgi>

/etc/uwsgi/apps-available/apps.xml

 <uwsgi>
  <plugins>python3</plugins>
  <master>false</master>
  <processes>1</processes>
  <vaccum>true</vaccum>
  <chmod-socket>666</chmod-socket>
  <socket>/tmp/%n.sock</socket>
  <uid>www-data</uid>
  <gid>www-data</gid>
  <pythonpath>/var/www/flask/%n</pythonpath>
  <module>%n</module>
 </uwsgi>

Make them available

cd /etc/uwsgi/apps-enabled
ln -s ../apps-available/emperror.xml
cd /etc/uwsgi/vassals-enabled
ln -s ../apps-available/apps.xml app1.xml
ln -s ../apps-available/apps.xml app2.xml

Daemon management

/usr/local/bin/uwsgi -d --ini /etc/uwsgi-emperor/emperor.ini --log-syslog
Start with logging to rsyslog, add following line to the rsyslogd configuration
:programname, contains, "uwsgi" /var/log/uwsgi/uwsgi.log
kill `pidof uwsgi`
This works for stopping all processes.
uwsgi --stop /run/uwsgi/pid
This looks cleaner but does not work on my system
uwsgi --reload /run/uwsgi/app1.pid
Restart a vassel (application)
kill -1 `cat /run/uwsgi/app1.pid`
Restart a vassel (application)