Skip to content

Instantly share code, notes, and snippets.

@bockor
Last active February 22, 2021 02:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bockor/ff2aef9f5be1ea98ac6fb927bb460d7d to your computer and use it in GitHub Desktop.
Save bockor/ff2aef9f5be1ea98ac6fb927bb460d7d to your computer and use it in GitHub Desktop.
gunicorn-as-a-systemd-service with python3 and bottle framework
Gunicorn, or Green Unicorn, is a UNIX-compatible WSGI HTTP server that is commonly used to run Python applications.
Developed in 2010 by Benoit Chesneau, this open source project is similar to uWSGI, mod_wsgi, and CherryPy.
Gunicorn is often implemented with a reverse proxy server like NGINX, which typically handles requests for static resources and then passes on the requests to Gunicorn.
Gunicorn processes the dynamic portion of the request and returns a response to NGINX, which sends the response back to the client.
Gunicorn can be used to serve Python applications and is compatible with frameworks like Django, Flask and Bottle.
It’s easy to configure, lightweight, and only needs 4–12 worker processes to handle hundreds or thousands of requests per second.
# create python3 virtual environment
# ==================================
echo $PWD
/home/bruno/Downloads/tbc/gunicorn
python3 -m venv ./gunicorn_venv
. gunicorn_venv/bin/activate
which python3
pip3 install bottle gunicorn
deactivate
which python3
# myapp.py
# ========
#Ref: https://www.fullstackpython.com/blog/python-3-bottle-gunicorn-ubuntu-1604-xenial-xerus.html
#Ref: https://www.fullstackpython.com/green-unicorn-gunicorn.html
#Ref: https://www.vultr.com/docs/how-to-setup-gunicorn-to-serve-python-web-applications
#Ref: https://vsupalov.com/what-is-gunicorn/
#Ref: http://blog.yprez.com/running-a-bottle-app-with-gunicorn.html
#Ref: https://pythonspeed.com/articles/gunicorn-in-docker/
#Ref: https://www.youtube.com/watch?v=Rj0CaUHuNqE
#Ref: https://www.youtube.com/watch?v=VAHCxvXgfWA
#Ref: https://bartsimons.me/gunicorn-as-a-systemd-service/
#Run gunicorn from CLI: gunicorn -w 2 --access-logfile - myapp:app
import bottle
from bottle import route, run, Response
# a basic URL route to test whether Bottle is responding properly
@route('/')
def index():
return Response("It works!\n")
# these two lines are only used for python myapp.py
if __name__ == '__main__':
run(host='0.0.0.0', port=8000, debug=True, reloader=True)
# this is the hook for Gunicorn to run Bottle
app = bottle.default_app()
# myapp.service
# =============
[Unit]
Description=MyApp Service
[Service]
#User=bruno
#Group=bruno
WorkingDirectory=/home/bruno/Downloads/tbc/gunicorn
ExecStart=/home/bruno/Downloads/tbc/gunicorn/gunicorn_venv/bin/gunicorn -w 2 myapp:app
# Create systemd *.service file
# Reference it in: /home/bruno/.config/systemd/user/
# ln -s /home/bruno/Downloads/tbc/gunicorn/myapp.service
# systemctl --user daemon-reload
# Start service: systemctl --user start myapp
# Check service status:
# systemctl --user status myapp
# systemctl --user list-units --type=service --state=active
# Open the URL using wget and show also the RESPONSE headers
# wget -S -q -O - http://localhost:8000
#Final directory tree of the working directory
tree -L 1
.
├── gunicorn_venv
├── myapp.py
├── myapp.service
└── __pycache__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment