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
Or with vi
:
sudo vi /etc/systemd/system/gunicorn.service
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
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'sbin
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
To get your primary group name:
id -gn
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
Enable the service to start on boot:
sudo systemctl enable gunicorn
Start the service:
sudo systemctl start gunicorn
Verify that the service is running without errors:
sudo systemctl status gunicorn
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
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)
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?
Hey. Did you succeed?
Nice guide 👌 keep them coming
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.
Nothing in particular. Just do your thing 😊
Thank you
Saviour, it saved me the hustle of massive documentations.
Welcome Mambeet 🐊😂