How to start XAMPP Automatically in Ubuntu Linux

This tutorial explains how to start XAMPP automatically in Ubuntu Linux step by step. Learn how to create a script in /etc/init.d/ directory which starts XAMPP automatically during the boot process. With this simple script there is no need to start XAMPP manually each time when Ubuntu starts.

XAMPP provides necessary web server environment in local system to run, test and develop PHP and MySQL based web applications.

For this tutorial, I assume that XAMPP is installed Ubuntu Linux. If you want to learn how to install XAMPP in Ubuntu Linux, see this tutorial.

How to install XAMPP in Ubuntu

It explains each installation steps in detail with examples.

Since XAMPP is the third party software, Ubuntu does not start it in boot process. Before we use it, we have to start it manually with following command

$sudo /opt/lampp/lampp start

start xampp manually

If XAMPP is used frequently, starting it manually is not a good choice. For example, consider a web developer who regularly uses XAMPP in developing and testing the web apps. In this case, developer needs to start XAMPP each time when system starts.

Sound boring, luckily we can get rid of this boring job by simply instructing Ubuntu to start XAMPP automatically in startup.

To instruct Ubuntu to start XAMPP automatically at boot time, we have to create a script file in /etc/init.d/ directory. Ubuntu checks and executes all scripts files from this directory at startup. Only a root user or the user who have root privilege can manage scripts in this directory. Since root user account is disabled by default in Ubuntu Linux, we cannot use it for this purpose. The user account which we create during the installation has necessary privilege. We can use that user account for this purpose.

Creating a script to stat XAMPP automatically

Login with the user account which was created during the installation or the user account which has root privilege and open the terminal.

In terminal run following command

sudo gedit /etc/init.d/lampp

This command creates and opens the necessary script file.

Before opening the file, system prompts us to authenticate the access. To authenticate the access, we have to type the same password which we used in login.

gedit /etc/init.d/lampp

Upon successful authentication, system will open a text file. In opened file, add following lines

#!/bin/bash
### BEGIN INIT INFO
# Provides: lampp
# Required-Start:    $local_fs $syslog $remote_fs dbus
# Required-Stop:     $local_fs $syslog $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start lampp
### END INIT INFO
/opt/lampp/lampp start

entries in script file

Until we save the file, all changes remain in memory. To save the change, click Save button. Once Save button is clicked, file contents will be formatted and saved as a script file.

entries in script file saved

This file will be treated as a regular text file until we make it executable script file by adding the execute permission. Run following commands to add and verify the execute permission in this file.

$ls –l /etc/init.d/lampp
$sudo chmod +x /etc/init.d/lampp
$ls –l /etc/init.d/lampp
  • First command lists the current file attributes.
  • Second command adds the execute permission in file attributes.
  • Third command verifies the execute permission has been successfully added in file attributes.

By default Linux does not add execute permission in file attributes. To learn how file system security works in Linux and how it can be managed, see this tutorial

Chmod command explained in Linux

It explains Linux file system security in detail with practical examples.

chmod script

Now run following command

$sudo update-rc.d lampp defaults

This command instructs Ubuntu to check and run our custom script file in default runlevels.

rc.d lampp default

That’s all we need to start XAMPP automatically. Now next time when system starts, XAMPP will be started automatically by Ubuntu in startup.

Testing

To test this setup, restart the system with following command

$reboot

reboot command

After restart, run following command

$systemctl list-units --type service

This command lists all services started by Ubuntu in startup. Since we only want to view whether the lampp service is started or not, we can narrow down the output of above command with grep command.

$systemctl list-units --type service |grep lampp

XAMPP uses lampp service to control all its operation in Ubuntu.

systemctl list-units

We can also verify the starting of XAMPP by accessing following URL in web browser

http://localhost

xampp testing

Same way to verify the starting of MySQL, access following URL

http://localhost/phpmyadmin

phpmyadim testing

Troubleshooting

During this process, you may get warning messages two times; while saving the script file and while instructing Ubuntu to check the script file. Let’s understand reason and possible options to deal with these warning messages in detail.

Warnings while saving script file

In Ubuntu 16.04, you may get following warning messages in terminal while saving /etc/init.d/lampp file from gedit editor.

gedit bug

These warning messages are triggered by a bug in this version. This bug was fixed in later version.

More information about this bug is available here. https://bugs.launchpad.net/ubuntu/+source/gedit/+bug/1575484

There are two options to deal with these warning messages; either upgrade the system or ignore the messages.

The best option is upgrading the system. But if upgrading is not possible, simply ignore these messages.

Warnings while instructing Ubuntu to read the script file

While updating rc.d you may see following warning.

rc.d warning

These warning messages indicate that something is wrong in /etc/init.d/lampp file. Before Ubuntu 16.04, comment section was not required in this file. Only adding following lines was sufficient.

script file without comment

But this approach no longer works. Starting from Ubuntu 16.04, we have to add all necessary lines including comment section.

If you get above warning messages or XAMPP does not start automatically after restart, check the /etc/init.d/lampp file again. Make sure that it has all necessary lines.

That’s all for this tutorial. If you like this tutorial, please share it with your favorite social platform.

ComputerNetworkingNotes Ubuntu Linux Tutorials How to start XAMPP Automatically in Ubuntu Linux