How to Set Up LAMP Server on Linux

LAMP is an acronym for Linux, Apache, MySQL, and PHP. It is a widely used software application stack for web applications, is used for web development and deployment. Learning how to set up LAMP server on Linux is necessary for several web-based functions. 

In this article, you will learn how to rapidly set up the fast-functioning LAMP server on Linux systems. 

Set up LAMP Server on Linux

The process of setting up LAMP Server on Linux is very simple. The first step is to connect your server using an SSH connection. You can also check out the detailed guide on how to set up SSH connections on Linux servers. However, if you are using a local server, simply open the terminal using the Ctrl + Alt + T shortcut. After that, follow the steps given below to deploy the server successfully. 

1. Update the Necessary Packages

It is crucial to update all the packages first before installing any packages for the LAMP server. For this step, you will run the following command:

sudo apt update
Set Up LAMP Server in Linux

This will run the standard update protocol to update all the packages and distributions to the latest versions. 

2. Install Apache 

Next, we will install the Apache package. Apache is the most widely used web server on Linux. Since we have already updated the packages, we are now ready for installation. Execute the command given below to install Apache:

sudo apt install apache2

You will be asked to enter the root password. Type it and press the “Enter” key to continue with the installation.

3. Configure Firewall to Set Up LAMP Server

For a web server, it is essential to configure a firewall in such a way that it allows HTTP and HTTPS traffic on your server. Apache profiles are available on the pre-installed ufw firewall. To check the profiles available, type the following command:

sudo ufw app list

It will give you a similar output as shown below:

Configure Firewall

We will use Apache’s full profile to allow HTTP and HTTPS traffic. But first, check if the Apache full profile has the ports for HTTP traffic.

sudo ufw app info "Apache Full"

Now, we will enable this profile using the allow command. For example:

sudo ufw allow in "Apache Full"

Alternatively, you can also use the following commands to allow HTTP and HTTPS traffic.

sudo ufw allow http
sudo ufw allow https
set up rules

At this stage, the Apache configuration is complete. You can check your local host (https://localhost/) or visit your server IP address. If you see the following page, you have successfully configured the Apache web server.

Set up LAMP  server on Linux Apache

4. Install MySQL

MySQL, the most widely used software for RDBMS (Relational Database Management Systems), is a part of the LAMP stack. We will install this using the install command. 

sudo apt install mysql-server

To ensure that you have installed it successfully, check its version using the --version flag. 

sudo mysqld --version

You will get a similar output on the screen.

Set up LAMP Server on Linux MySQL

Secondly, you will create your user on MySQL by executing the following command:

mysql -uroot

After that, type the following commands:

CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY '<New-Password-Here>';
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;

This will successfully create your user. 

Next, we will secure MySQL connection and limit its access. To perform this step, execute the following command:

sudo mysql_secure_installation

Follow the guided procedure and create a strong password for security reasons. If the password is not strong, it will be rejected. Hence, it is essential to follow the recommended password guidelines, which include: 

  • Use both upper and lower case letters
  • Include both letters and numbers
  • Using special characters
  • Using a password that you have not used previously

Lastly, you can choose to remove anonymous users and disable remote login. Press Enter key to continue with the default settings. To enter MySQL console, type:

sudo mysql

If you have configured it correctly, you will enter its console. To exit from the console, type exit, and press Enter key. For example:

mysql > exit;

Finally, you have now successfully installed the MySQL server. Let’s move on to the next step.

5. Install PHP

PHP is a scripting language to create dynamic web pages. In this step, you will install all the necessary libraries required to work with the Apache platform. To install PHP, type and execute: 

sudo apt install php libapache2-mod-php php-mysql
Set Up PHP

Secondly, verify its installation using the --version flag.

sudo php -v
Set Up PHP

Thirdly, we will change the priority of our Apache files to give the topmost priority to our index.php file. For this step, execute the following command:

sudo nano /etc/apache2/mods-enabled/dir.conf

After that, move the index.php to the top of the list.

<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.xhtml index.html

Lastly, save the changes by pressing “Ctrl + X” and exiting the editor. 

6. Configure phpMyAdmin to Set Up LAMP Server

The last step in setting up the LAMP Server is to install phpMyAdmin. Since MySQL is difficult to control from the command line, we use phpMyAdmin tools to perform this task. Type the command given below and execute it:

sudo apt install phpmyadmin

Secondly, you will create a symbolic link to the html directory. This will allow you to save your PHP files in the /var/www/html directory since this is the location from where the LAMP server runs the files. Navigate to the directory by using the cd command. 

For example:

cd /var/www/html

Next, create the link using the ln command as shown below:

ln -s /usr/share/phpmyadmin phpmyadmin

After that, restart the web server to implement the changes.

sudo service apache2 restart

Alternatively, you can also use this command to restart Apache. 

sudo systemctl restart apache2

To verify that Apache is working correctly, and your PHP operations are in place, create a dummy index.php as shown below:

sudo nano /var/www/html/index.php

After that, insert the phpinfo() function to print the relevant PHP information on the console.


Then exit the editor. After that, head over to your browser and visit the local host (http://localhost/) or your Server IP Address. You will get PHP details on the screen as shown in the image below.

Configure PHP

Finally, you have learned how to set up a LAMP server on your Linux machines. Also, if you would like to know more about PHP configuration, check out the details on installing PHP.

If this guide helped you, please share it.

Leave a Reply
Related Posts