DEV Community

Tonny Kirwa
Tonny Kirwa

Posted on • Updated on

Create a systemd service script for running Gunicorn to serve your application

To create or edit a gunicorn.service file in Linux for running a Flask application, you need to create a systemd service unit file. This service unit file will define how Gunicorn should run your Flask application as a service. Here's a step-by-step guide:

Create or Edit the Gunicorn Service File:

Open a terminal on your Linux system and use a text editor to create or edit the gunicorn.service file. You can use editors like nano or vi:

   sudo nano /etc/systemd/system/gunicorn.service
Enter fullscreen mode Exit fullscreen mode

Or with vi:

   sudo vi /etc/systemd/system/gunicorn.service
Enter fullscreen mode Exit fullscreen mode

To create a systemd service script for running Gunicorn to serve your application, you'll need to create a file named gunicorn.service with the following contents and place it in the appropriate directory on your server:

[Unit]
Description=Gunicorn instance to serve application
After=network.target

[Service]
User=your_username
Group=your_groupname
WorkingDirectory=/path/to/your/app
Environment="PATH=/path/to/venv/bin"
ExecStart=/path/to/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5003 web_dynamic.2-hbnb:app
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Replace the placeholders with your actual values:

  • your_username: Your username on the system.
  • your_groupname: Your primary group name on the system.
  • /path/to/your/app: The absolute path to your application's root directory.
  • /path/to/venv/bin: The absolute path to your virtual environment's bin directory.
  • web_dynamic.2-hbnb:app: The Python import path to your Gunicorn app object.

Username and Primary Group Name on a Unix-like system

To find your username and primary group name on a Unix-like system, you can use the id command. Open a terminal and type the following commands:

To get your username:

   id -un
Enter fullscreen mode Exit fullscreen mode

To get your primary group name:

   id -gn
Enter fullscreen mode Exit fullscreen mode

These commands will display your username and primary group name respectively. You can then use these values to replace the placeholders in the gunicorn.service file.

After creating the gunicorn.service file, you need to place it in the appropriate directory for systemd unit files. Typically, this directory is /etc/systemd/system/.

Then, follow these steps:

Reload the systemd manager configuration to make it aware of the new service file:

   sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Enable the service to start on boot:

   sudo systemctl enable gunicorn
Enter fullscreen mode Exit fullscreen mode

Start the service:

   sudo systemctl start gunicorn
Enter fullscreen mode Exit fullscreen mode

Verify that the service is running without errors:

   sudo systemctl status gunicorn
Enter fullscreen mode Exit fullscreen mode

You can also restart, stop, or check the logs of the service using systemd commands:

   sudo systemctl restart gunicorn
   sudo systemctl stop gunicorn
   journalctl -u gunicorn
Enter fullscreen mode Exit fullscreen mode

Finally, test your application to ensure it's serving content as expected using curl commands similar to what you mentioned in your instructions.

Remember that the gunicorn.service script must be tailored to your specific environment and application setup. Make sure to adjust paths, usernames, and other parameters accordingly.

Also, note that systemd services require administrative privileges to manage. Be sure to use sudo as needed while performing these steps.

Top comments (8)

Collapse
 
sinamathew profile image
Sina Mathew

Insightful. I installed gunicorn globally, so I comment out the Evironment line. But I can't get the service running. can not having a user group also cause that?

Collapse
 
tkirwa profile image
Tonny Kirwa

Hey. Did you succeed?

Collapse
 
fadygrab profile image
Fady GA 😎

Nice guide 👌 keep them coming

Collapse
 
tkirwa profile image
Tonny Kirwa

Thank you for the appreciation... More on the way. You can also recommend what you'd like to see - I'll put effort on getting a working solution.

Collapse
 
fadygrab profile image
Fady GA 😎

Nothing in particular. Just do your thing 😊

Thread Thread
 
tkirwa profile image
Tonny Kirwa

Thank you

Collapse
 
chatelo profile image
Benard Ronoh • Edited

Saviour, it saved me the hustle of massive documentations.

Collapse
 
tkirwa profile image
Tonny Kirwa

Welcome Mambeet 🐊😂