• 22 Aug 2023

How to Install LAMP Stack on Ubuntu 16.04/18.04


by Vnnovate Solutions

This brief tutorial is going to explain to you about how to install Apache2, MariaDB and PHP on Ubuntu Linux 16.04 / 18.04 and 18.10 servers.

LAMP stands for Linux (Ubuntu), Apache2 HTTP Server, MariaDB or MySQL Database Server and PHP Scripting Language…

It is a group of open source software and building block of many of the web applications and majority of the content management systems (CMS) in use today.

Please follow the following steps to install LAMP Stack in ubuntu:

Step 1: Prepare Ubuntu Linux

To install LAMP, it is prerequisite to have an Ubuntu server on your system. This post assumes you’ve already installed Ubuntu server.

After installing Ubuntu server, run the following command to update the server:

sudo apt update && sudo apt dist-upgrade && sudo apt autoremove

Step 2: Install Apache2 HTTP Server

To install Apache2, run the following command:

sudo apt update && sudo apt install apache2

After installing Apache2, the following commands can be used to stop, start, and enable the Apache2 service to always start up with the server boots.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

After running these commands, please check in the browser whether it is working fine or not. To check this, enter your hostname or IP address in your browser, and it should show the welcome page of the Apache2 default page. The page should look like the one below:

Step 3.0: Install MariaDB Database Server

MySQL was originally the default database server among Linux systems, MariaDB has taken over. To install it, run the commands below.

sudo apt-get install mariadb-server mariadb-client

After installing MariaDB database server, the commands below can be used to stop, start, and enable MariaDB service to always start up when the server boots.

On Ubuntu 16.04 LTS

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

On Ubuntu 18.04 LTS and 18.10

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

After that, run the commands below to secure MariaDB server by creating a root password and disabling remote root access.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

Restart MariaDB server

To test if MariaDB is installed, type the commands below to log in to MariaDB server

sudo mysql -u root -p

Then type the password you created above to sign in. If successful, you should see the MariaDB welcome message.

Step 3.1: Install MySQL

MySQL is a database management system. Basically, it will organize and provide access to databases where your site can store information.

If you want to install MySQL, follow the below steps:

sudo apt install mysql-server

When the installation is complete, run a simple security script that comes pre-installed with MySQL, which will remove some dangerous defaults and lock down access to your database system. Start the interactive script by running:

sudo mysql_secure_installation

Answer Y for yes, or anything else to continue without enabling.

If you answer “yes,” you’ll be asked to select a level of password validation. Keep in mind that if you enter 2 for the strongest level, you will receive errors when attempting to set any password which does not contain numbers, upper and lowercase letters, and special characters, or which is based on common dictionary words.

Regardless of whether you chose to set up the VALIDATE PASSWORD PLUGIN, your server will next ask you to select and confirm a password for the MySQL root user. This is an administrative account in MySQL that has increased privileges. Think of it as being similar to the root account for the server itself (although the one you are configuring now is a MySQL-specific account). Make sure this is a strong, unique password, and do not leave it blank.

If you enabled password validation, you’ll be shown the password strength for the root password you just entered, and your server will ask if you want to change that password. If you are happy with your current password, enter N for “no” at the prompt:

For the rest of the questions, press Y and hit the ENTER key at each prompt. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

If you prefer to use a password when connecting to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:

sudo mysql

Next, check which authentication method each of your MySQL user accounts uses with the following command:

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

To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change the password: to a strong password of your choosing:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run FLUSH PRIVILEGES, which tells the server to reload the grant tables and put your new changes into effect:


Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:

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

You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your server, you can exit the MySQL shell:

mysql> exit

At this point, your database system is now set up, and you can move on to installing PHP, the final component of the LAMP stack.

Step 4: Install PHP

The last component of the LAMP stack is PHP… It’s the P in the LAMP stack… To install PHP and related PHP modules, run the commands below…

sudo apt install php libapache2-mod-php php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-zip php-curl

After installing PHP, run the commands below to find the version installed on the server…

php -v

You should see an output like the one below

PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )

Copyright (c) 1997-2018 The PHP Group

Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

The version number determines the location of PHP default configuration file… For PHP 7.2, the location is as shown below:

sudo nano /etc/php/7.2/apache2/php.ini

Replace the version number above with the version of PHP installed..,..

When the file opens, make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.

file_uploads = On allow_url_fopen = On memory_limit = 256M upload_max_filesize = 100M max_execution_time = 360 date.timezone = America/Chicago

After making the change above, save the file and close out.

Step 5: Restart Apache2

When you’re done making PHP changes above, run the commands below to restart Apache2 HTTP server for PHP settings to apply…

sudo systemctl restart apache2.service

To test PHP settings with Apache2, create a phpinfo.php file in Apache2 root directory by running the commands below

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

Then type the content below and save the file.


Save the file.. then browse to your server hostname followed by phpinfo.php http://localhost/phpinfo.php

You can check out the PHP default test page now.

Congratulations! You’re successfully installed the LAMP Stack on Ubuntu 16.04 / 18.04 / 18.10…

If you’ve still any confusions regarding Lamp installation on Ubuntu, feel free to contact us now, Vnnovate can definitely help you!