Web Application Setup - uWSGI Set Up

webapp server python uwsgi

Set Up uWSGI To Server Your Application

  1. Create your uWSGI configuration file:

    ... touch /var/www/appname/config/uwsgi-appname.ini
    
  2. Contents of your uwsgi-appname.ini should look like this:

    [uwsgi]
    
    plugins = python3,logfile
    
    chdir = /var/www/appname
    home = /var/www/appname/venv
    wsgi-file = /var/www/appname/wsgi.py
    
    master = True 
    cheap = True
    idle = 600
    die-on-idle = True
    manage-script-name = True
    
  3. Link your config file so uwsgi can find it:

    ... cd /etc/uwsgi/apps-enabled
    ... ln -s /var/www/appname/config/uwsgi-appname.ini appname.ini
    

SYSTEMD UWSGI SET UP

  1. Create systemd socket and server files for your uwsgi app:

    ... touch uwsgi@appname.socket
    ... touch uwsgi@appname.service
    

    The content of these files are generic - for additional apps these could be copied or linked with the new app name (e.g. uwsgi@appname2.socket). Systemd will will interpolate the conent between the @ and . in the file/link name with the %i directive in the files.

  2. Your uwsgi@appname.socket file:

    [Unit]
    Description=Socket for uWSGI app %i
    
    [Socket]
    ListenStream=/var/run/uwsgi/%i.socket
    SocketUser=www-%i
    SocketGroup=www-data
    SocketMode=0660
    
    [Install]
    WantedBy=sockets.target
    
  3. Your uwsgi@appname.service file:

    [Unit]
    Description=%i uWSGI app
    After=syslog.target
    
    [Service]
    ExecStart=/usr/bin/uwsgi \
            --ini /etc/uwsgi/apps-enabled/uwsgi-%i.ini \
            --socket /var/run/uwsgi/%i.socket
    User=www-%i
    Group=www-data
    Restart=on-failure
    KillSignal=SIGQUIT
    Type=notify
    StandardError=syslog
    NotifyAccess=all
    
  4. Link these files to the systemd config directory:

    ... cd /etc/systemd/system
    ... ln -s /var/www/appname/config/uwsgi@appname.socket ./
    ... ln -s /var/www/appname/config/uwsgi@appname.service ./
    
  5. Start up uWSGI:

    ... sudo systemctl enable uwsgi@appname.socket
    ... sudo systemctl enable uwsgi@appname.service
    ... sudo systemctl start uwsgi@appname.socket