If your like me and you are using Xampp to host your LAMP stack on Ubuntu, you'll want it to auto start on boot so you can reboot your system whenever you'd like or so it can auto start up upon power loss. The easiest way I've found to do this is to write a script that systemd will run on boot. It's pretty simple and I'd love to show you.
Before we begin, I'm going to assume you are logged in as the root user on your system. If you aren't, type sudo -i
It's actually really simple to create the script we need for this, and it's pretty expandable as well. First you want to run ls to make sure you aren't going to override any files currently in the root directory. If you see any .sh files, just avoid naming your script with those files.
To create the file, run touch xampp-start.sh && nano xampp-start.sh
Now, type in sudo and then the directory of xampp, if you don't know your directory, it's most likely on the default directory which is /opt/lampp/.
sudo /opt/lampp/lampp restart
Even though we aren't technically restarting Xampp, it will help fix issues that systemd has detected. Like I mentioned previously, you'll want to replace /opt/lampp/ with your directory of xampp. Leave in the last lamppafter the directory in the command. Now you can type CTRL+X and Y and push ENTER on your keyboard to close out of the nano text editor.
Now that your script has been created, we'll want to automate it. To automate it, move into the systemd directory by typing cd /etc/systemd/system/. This is still assuming you are the root user.
Run ls again to make sure you aren't going to override an existing file. Now, you'll want to run touch xampp.service && nano xampp.service. Now if that file name is already taken (which it probably shouldn't be), you'll want to name it something else. Just remember the name you chose for later. Now, you can paste in the following and I'll show you what parameters to modify.
[Unit]
Description=Xampp
After=network.target
After=network-online.target
[Service]
ExecStart=/bin/bash /root/start.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
As it is now, these commands for this systemd service will auto run your xampp script on boot after the network connection is established. (Xampp won't start until your server has a network connection. That will avoid common issues with auto starting xampp. One parameter you'll want to change is ExecStart=/bin/bash /root/xampp-start.sh
Currently, it's going to run a script called start.sh, but that might not be the name of the script you created. Replace that with the name of the script you made previously in this guide.
Now that your new service is created, you'll want to run systemctl start xampp.service. If you changed the name of your .service file, change the systemctl command to fit the name of your file. It might ask you to run systemctl daemon-reload. Now, you'll run systemctl enable xampp.service.
That's all!