In the previous two tutorials, we first installed a classic LAMP server, then Laravel with its Breeze and Vue.JS additions.
This tutorial shows how to configure them to start automatically as services using systemd.
Why is this necessary? It’s quite simple. As we saw in the previous tutorial, if we don’t turn them into services, they have to be started each time in a separate terminal window and can only be stopped with Ctrl-C.
systemd uses so-called units. Each of these is a unit file in the /etc/systemd/system/ directory. The startup parameters are specified in them.
To install such a unit, we need to create a new file in the /etc/systemd/system/ directory with the name of the new service, enable it, and then start it for testing. You no longer have to deal with them, the system ensures that they run automatically in the background. Of course, before installing the services, you must stop them with the Ctrl-C key combination in the terminal windows where you started them.
Let’s start with Vue.JS. First, we create and save the unit file with a text editor.
~$ sudo nano /etc/systemd/system/vuejs.service //the filename could be anything ending with .service
This should have the following (the username is the same as the one we entered when installing the distribution):
[Unit]
Description=npm dev backend // for example
[Service]
Type=simple
Restart=always
User=username
Group=username
WorkingDirectory=/home/username/example-app
ExecStart=/usr/bin/npm run dev
[Install]
WantedBy=multi-user.target
Finally, enable and run it.
~$ sudo systemctl enable vuejs
~$ sudo systemctl start vuejs
If the operation was successful, we should see Lavarel + Vue at http://localhost:5173/ . After that comes the Laravel application unit.
~$ sudo nano /etc/systemd/system/example-app.service
This should also contain something similar to the previous unit file:
[Unit]
Description=Laravel Example App
After=network.target
[Service]
User=username
Group=username
Restart=always
WorkingDirectory=/home/username/example-app
ExecStart=/usr/bin/php artisan serve
[Install]
WantedBy=multi-user.target
Finally, enable and run it.
~$ sudo systemctl enable vuejs
~$ sudo systemctl start vuejs
After all that, the Laravel application sample page should appear in your browser at http://localhost:8000. Moreover, it is now the same after every launch of your WSL distribution.
That’s all for now, see you later!