This guide outlines how to configure Laravel services with systemd
for background tasks like queues and custom commands.
-
Create the Service File
Create a service file at
/etc/systemd/system/laravel_queue.service
. Make sure to adjust theUser
,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
-
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
-
Create the Service File
Similarly, create a service file for the websocket at
/etc/systemd/system/laravel_reverb.service
. AdjustUser
,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
-
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
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