Tag Archives: systemd

Launching Python applications with Gunicorn and systemd

At ServerBeep.com we love Python and use it widely. There are a lot of ways to deploy Python application. One of them is using Gunicorn.

Gunicorn is a Python WSGI HTTP Server for UNIX. So if you have WSGI complaint application you can run it with Gunicorn. It can be, for instance, Django or Flask powered applications.

From the other hand, last versions of Fedora Linux ship with systemd. Systemd is a featured replacement of old SysV system. Among other features there is one which allows to launch the processes after server boot. To use systemd for the applications called app launch create file /lib/systemd/system/gunicorn-app.service:

[Unit]
Description=gunicorn-app

[Service]
ExecStart=/usr/bin/gunicorn -D -n gunicorn-app -w5 tracwsgi:application -b 127.0.0.1:8000 --access-logfile /home/app/log/app.serverbeep.net-access.log --error-logfile /home/app/log/app.serverbeep.net-error.log
Type=forking
User=app
Group=app
Restart=always
StandardOutput=syslog
StandardError=syslog
WorkingDirectory = /home/app/

[Install]
WantedBy=multi-user.target

And enable it:

systemctl enable gunicorn-app.service

Now after server boots you will get your application run. After you start it Gunicorn will handle the requests which are sent to 127.0.0.1:8000. Next step could be nginx or Apache setup as reverse proxy. It means that webserver listens well-known port 80 and serves some files (usually static ones) directly and send rest of requests to Gunicorn. It responses back and webserver provides a reply to a user.

With systemd you don’t have to setup supervisord or other processes monitoring and management tools. Systemd will take care of it. It’s easy and neat way to launch the daemons.