The de-facto standrad for serving python web applications is wsgi. I want to serve (at least one) python web app and therefore install uwsgi as the "link" between nginx and the web app. Since I have python 3.6 already installed, I need to take care not to install the legacy python version of uwsgi.

remote> sudo pkg install uwsgi-py36-2.0.16

To enable uwsgi, I'll need to add the following line to /etc/rc.conf


Uwsgi on FreeBSD expects its configuration file(s) to be located at /usr/local/etc/uwsgi/uwsgi.ini. The folder and file propably doesn't exist, so we need to create it:

remote> cd /usr/local/etc
remote> sudo mkdir uwsgi

Propably I want to serve multiple uwsgi web applications. Luckily uwsgi is prepared for this and offers a so called 'Emperor mode'. The different web applications themselves are called 'vassals'. The following creates the accoriding folder for storing web app configurations.

remote> cd /usr/local/etc/uwsgi
remote> sudo mkdir vassals

Of cause uwsgi itself needs an configuration file, so let's create it:

remote> sudo vim /usr/local/etc/uwsgi/uwsgi.ini
# enter emperor mode and scan the directory for application configurations
emperor = /usr/local/etc/uwsgi/vassals

# set user and group
uid = www
gid = www
emperor-tyrant = true

master = True
logto = /var/log/uwsgi.log
memory-report = True

# START: settings for all applications:

vassal-set = uid = www
vassal-set = gid = www

# set the plugin-directory
# vassal-set = plugins-dir=/usr/local/lib/uwsgi/

# change the rights to the sockets to a sensible value
vassal-set = chmod-socket=664

# use four processes
vassal-set = processes=4

# have at least two workers available, start up to ten workers per application
vassal-set = cheaper-algo=spare
vassal-set = cheaper=2
vassal-set = cheaper-initial=2
vassal-set = workers=5
vassal-set = cheaper-step=1

# workers on a task longer than 30 seconds should be terminated
vassal-set = harakiri=60
# to prevent memory leaks, workers should be restarted after 5000 requests
vassal-set = max-requests=5000

# clean up after exiting
vassal-set = vacuum=true

# END: settings for all applications:

All configuration entries starting with vassal-set = set some defaults for the specific web application configurations.

The uwsgi service can be started with

remote> sudo service uwsgi start


To get some stats from running uwsgi web applications, I'll also install uwsgitop:

remote> sudo pkg install uwsgitop-py36-0.10

Unfortunately, the program is installed as uwsgitop-3.6, but we can simplify this

remote> cd /usr/local/bin
remote> sudo ln -s uwsgitop-3.6 uwsgitop