Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.

How To Install Linux, Apache, MYSQL, PHP (LAMP Stack) on Ubuntu

19th April 2020 6 mins read
Share Article     Share On WhatsApp   Buy Me Coffee | StackCoder Buy Coffee

Hello friends in this post I am gonna show you how to install LAMP in Ubuntu server.

LAMP stands for L - LINUX, A - Apache, M - MYSQL, P - PHP


Before moving ahead with this article I request you guys to setup up Ubuntu server in Digital Ocean, Linode or any other cloud platform. If you don't have an account then click on the link and get the free credits to play around :)

If your still using password logins or root SSH logins. Then I highly recommend you to check on How To Do Basic Server Security Setup For Ubuntu / Linux

Login to you server and follow along with the following steps.

Step 1 - Install APACHE web server

Apache is a very famous web server in the market for decades, it handles all your server incoming requests on port 80

sudo apt-get update
sudo apt-get install apache2

To cross verify whether the installation is successful or not you can simply open your server ip_address in browser as follows


You will see a APACHE successfully installed page. Which loads from /var/www/html/ folder

Step 2 - Install MYSQL database & secure with password

MYSQL is a relational database which is used to store the dynamic data and fetch the data which will come from login, register and various other types of forms

sudo apt-get install mysql-server

Now the MYSQL database is installed. As earlier now MYSQL wont prompt for password. So to securely install MYSQL server use the following commands

sudo mysql_secure_installation

You might be prompted with VALIDATE PASSWORD PLUGIN please select NO as per my experiences. It will create a lot of fuss for the passwords at later point.

By chance if you select YES option then you will get the following prompt

There are three levels of password validation policy. Select difficulty based on your requirement.

LOW  Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 

Next add the root user password and confirm the password.

New password:

Re-enter new password:

For best security practices its good idea to press YES for the subsequent prompts. This prompts include remove anonymous users, remove test database, disable remote root logins and flush privileges to save changes into affect.

By default you will be root user with auth_socket enabled connection. Which means while connecting to database no need of any passwords. At some point later this might cause security issues. So its better to add the password authentication for all requesting connections.

Let me show you the same. First lets login to MYSQL

sudo mysql

Next run the following to see the details of the root user

SELECT user,authentication_string,plugin,host FROM mysql.user;

Once you run the above command you can see that root user doesn't have the authentication_string ie password.

| user       | authentication_string           | plugin        | host   |
| root       |                      | auth_socket      | localhost |

To set the password run the following command, make sure to change the your_password with valid required password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';

Run the following command so that the changes make will take an affect.


Now again if you re-run the above command to check if the password is set or not then it will set with mysql_native_password. No worries if your not able to see the authentication_string as it will be encrypted by MYSQL 

| user       | authentication_string           | plugin        | host   |
| root       | *549966GJ45794839F92C1571D6D78F | mysql_native_password | localhost |

Now you can safely exit from MYSQL prompt. If you try to login as sudo mysql then it wont allow. You need to add your_password to do so.

Step 3 – Install PHP and its packages

Now you have installed APACHE web server and MYSQL its time to setup your PHP so that your PHP scripts runs.

APACHE process all your PHP scripts with the help of MOD_PHP ie PHP for APACHE module, it allows APACHE to interpret all your PHP files when any of the scripts gets called.

Now install all the PHP packages in Ubuntu server with following command. No need to add specific version like the way I will do in the following command. But make sure that your local development and now installing PHP version matches else if you use composer in future you will have to face some problems.

sudo apt install php7.2 libapache2-mod-php php7.2-mysql php7.2-curl php7.2-zip php7.2-json

If your installing LARAVEL then better to run the following command to install all packages at once

sudo apt-get install php7.2-fpm php7.2-cli php7.2-common php7.2-curl php7.2-gd php7.2-gmp php7.2-json php7.2-mbstring php7.2-mysql php7.2-xml php7.2-zip

You can add composer, zip, unzip, git in the same command instead of adding later :)

Step 4 - Setting Up PHP With APACHE Web Server

Once the PHP , MYSQL & APACHE installation is done then your left with configuring APACHE. First you need to setup APACHE for your site. Basically you will find all APACHE configurations in the following path


To check if any of these configurations are enabled or not then you need to check in the following path


Make sure to replace with your domain name in the following configuration.

sudo nano /etc/apache2/sites-available/

Now add the following configuration to the file. In the following if you don't have domain name still then just add the ip_address of your domain name.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Now you have added APACHE configuration, lets activate it by the following command

sudo a2ensite

As a default APACHE will have 000-default.conf configuration which will be activated we have to unlink from /etc/apache2/sites-enabled/000-default.conf with the following command

sudo a2dissite 000-default.conf

Step 5- Test & restart your APACHE configuration

To test if the configuration file is working or not use the following command to check

sudo apache2ctl configtest

You will be able see the following output

Syntax OK

Now restart you APACHE server

sudo systemctl restart apache2

Step 6 - Finally check if the configuration is working

Now you have install LAMP and its time to check if it paid your efforts. First create testing.php file inside /var/www/html/ like /var/www/html/testing.php

sudo nano /var/www/hml/testing.php

And add the following code

echo 'LAMP installtion completed.';

Now browse URL or http://ip_address/testing.php from browser and you must be able to see the following output

LAMP installation completed.

Congrats on setting up your LAMP stack.


Now you know how to install LAMP stack. If you have not read on how to perform basic server security on your sever then I highly recommend read my article on it How To Do Basic Server Security Setup For Ubuntu / Linux

Hope it help you. Happy coding :)

Author Image

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing