Create a systemd service script for running Gunicorn to serve your application
Published: 2023-08-18 20:41:06

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`:

   ```bash
   sudo nano /etc/systemd/system/gunicorn.service
   ```

   Or with `vi`:

   ```bash
   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:

```plaintext
[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'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:
   ```bash
   id -un
   ```

To get your primary group name:
   ```bash
   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:
   ```bash
   sudo systemctl daemon-reload
   ```

Enable the service to start on boot:
   ```bash
   sudo systemctl enable gunicorn
   ```

Start the service:
   ```bash
   sudo systemctl start gunicorn
   ```

Verify that the service is running without errors:
   ```bash
   sudo systemctl status gunicorn
   ```

You can also restart, stop, or check the logs of the service using systemd commands:
   ```bash
   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.