Forem

Edgaras
Edgaras

Posted on

Running Laravel Reverb in the Background Using systemd

If you're using Laravel's Reverb WebSocket server, managing it efficiently is key, especially in production environments. In this post, I’ll show you how to configure and run the Reverb WebSocket server in the background using systemd. This approach ensures your WebSocket server starts with the system and restarts automatically in case of failure.

Step 1: Create the systemd Service File

First, create a service file. This file tells systemd how to manage the server process.

Run the following command to create and edit the service file:

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

Now, add the following configuration to the file:

[Unit]
Description=Laravel Reverb WebSocket Server
After=network.target

[Service]
ExecStart=/usr/bin/php /var/www/example.test/artisan reverb:start --port=6001 --host=0.0.0.0
Restart=always
User=www-data
Group=www-data
WorkingDirectory=/var/www/example.test

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

Breaking Down the Parameters

  • [Unit]:

    • Description: Describes the service for your reference.
    • After=network.target: Ensures the service starts only after the network is up.
  • [Service]:

    • ExecStart: Specifies the command to start the Reverb server. Update the path (/var/www/example.test) to match your Laravel installation directory.
    • Restart=always: Automatically restarts the service on failure.
    • User/Group: Runs the service as the www-data user and group, which is typical for web servers.
    • WorkingDirectory: Sets the working directory for the command, ensuring Laravel's environment is loaded correctly.
  • [Install]:

    • WantedBy=multi-user.target: Ensures the service starts in the standard multi-user mode.

Step 2: Reload systemd and Enable the Service

Once the service file is created, reload systemd to pick up the changes:

sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Next, enable the service so it starts automatically on boot:

sudo systemctl enable reverb.service
Enter fullscreen mode Exit fullscreen mode

Step 3: Start the Service

To start the service manually for the first time, use:

sudo systemctl start reverb.service
Enter fullscreen mode Exit fullscreen mode

This will launch the Reverb WebSocket server in the background.

Step 4: Check the Service Status

To verify that the service is running correctly, use:

sudo systemctl status reverb.service
Enter fullscreen mode Exit fullscreen mode

This command provides detailed output about the current state of the service, including whether it’s active, running, or if there are errors.

Additional Commands

Stopping the Service

If you need to stop the service, run:

sudo systemctl stop reverb.service
Enter fullscreen mode Exit fullscreen mode

This immediately halts the WebSocket server without removing it from startup.

Restarting the Service

If you’ve made changes to the service or the Laravel application, restart it using:

sudo systemctl restart reverb.service
Enter fullscreen mode Exit fullscreen mode

This stops the current service instance and starts a new one.

For a deeper understanding of systemd service configuration, read this post.

Top comments (1)

Collapse
 
fstrube profile image
Franklin Strube

Nice! This is a good, concise walkthrough. Do you have experience using Pusher? Is it worth it to switch to Reverb?