Skip to content

Instantly share code, notes, and snippets.

@eliyas5044
Created October 29, 2024 02:01
Show Gist options
  • Save eliyas5044/59eb728476fc674f3ce7e7eed67c6b64 to your computer and use it in GitHub Desktop.
Save eliyas5044/59eb728476fc674f3ce7e7eed67c6b64 to your computer and use it in GitHub Desktop.
Laravel Services with Systemd

Laravel Services with Systemd

This guide outlines how to configure Laravel services with systemd for background tasks like queues and custom commands.

1. Laravel Queue Worker Service

  1. Create the Service File

    Create a service file at /etc/systemd/system/laravel_queue.service. Make sure to adjust the User, Group, and working directory path to match your server configuration.

    [Unit]
    Description=Laravel Queue Worker
    After=network.target
    
    [Service]
    User=www-data
    Group=www-data
    Restart=always
    ExecStart=/usr/bin/php /var/www/html/artisan queue:work --sleep=3 --tries=3 --max-time=3600
    StandardOutput=append:/var/www/html/storage/logs/queue.log
    StandardError=append:/var/www/html/storage/logs/queue_error.log
    WorkingDirectory=/var/www/html
    
    [Install]
    WantedBy=multi-user.target
  2. Enable and Start the Service

    Run the following commands to reload systemd, enable the service to start on boot, and start it immediately.

    sudo systemctl daemon-reload
    sudo systemctl enable laravel_queue
    sudo systemctl start laravel_queue

2. Laravel Reverb Websocket Service

  1. Create the Service File

    Similarly, create a service file for the websocket at /etc/systemd/system/laravel_reverb.service. Adjust User, Group, and working directory path as needed.

    [Unit]
    Description=Laravel Reverb Websocket
    After=network.target
    
    [Service]
    User=www-data
    Group=www-data
    Restart=always
    ExecStart=/usr/bin/php /var/www/html/artisan reverb:start
    WorkingDirectory=/var/www/html
    
    [Install]
    WantedBy=multi-user.target
  2. Enable and Start the Service

    To reload systemd and enable the service on boot:

    sudo systemctl daemon-reload
    sudo systemctl enable laravel_reverb
    sudo systemctl start laravel_reverb

Additional Commands

To check the status, restart, or stop these services, you can use the following commands:

# Check service status
sudo systemctl status laravel_queue
sudo systemctl status laravel_reverb

# Restart service
sudo systemctl restart laravel_queue
sudo systemctl restart laravel_reverb

# Stop service
sudo systemctl stop laravel_queue
sudo systemctl stop laravel_reverb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment