How to Install LAMP on Ubuntu 16.04

Install LAMP on Ubuntu 16.04

In this tutorial we’ll learn how to install LAMP on Ubuntu 16.04. We will also install and configure its prerequisites. LAMP is a simple software bundle made of 4 components, Linux, Apache, MySQL or MariaDB and PHP.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 16.04 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install LAMP on Ubuntu 16.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y && apt-get upgrade -y

Step 2. Installing Apache.

Install Apache by running the following command:

apt-get install apache2 libapache2-mod-php

After the installation is complete, you should enable Apache to start automatically upon system boot. You can do that with the following command:

systemctl start apache2
systemctl enable apache2

To verify that Apache is running, open your web browser and enter your server IP address, (e.g. http://192.168.77.1). If Apache is successfully installed you should see a message saying “It works!”.

Step 3. Installing and configuring MySQL.

Now that we have a web server, we need to install MySQL, a database management system, to store and manage the data for our site. You can install this easily by running the following:

apt-get install mysql-server

Start MySQL service using the following command:

systemctl start mysql

MySQL needs some security modifications to help protect your databases from unauthorized access. Run the following command:

mysql_secure_installation

Follow the prompts as below:

  • Would you like to setup VALIDATE PASSWORD plugin? press Enter here for No
  • Please set the password for root here. New password: Type a secure password here then press Enter
  • Re-enter new password: Re-type the secure password here then press Enter
  • Remove anonymous users? (Press y|Y for Yes, any other key for No) : press y and then Enter here
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : press y and then Enter here
  • Remove test database and access to it? (Press y|Y for Yes, any other key for No) : press y and then Enter here
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : press y and then Enter here

Step 4. Installing and configuring PHP 7.

Install PHP 7 with the following command:

apt-get install php php-mysql

Now we can test if PHP is working correctly by placing a file called info.php inside your web server root directory:

nano /var/www/html/info.php

Enter the following inside the file and save it:

<?php
phpinfo();
?>

Finally, restart the Apache server by typing:

systemctl restart apache2

Now, if you navigate to this page: http://your_server_ip_address/info.php in your web browser, you will see the following page showing your current PHP configuration:

Install LAMP on Ubuntu 16.04

Congratulation’s! You have successfully configured LAMP Stack on your Ubuntu 16.04 LTS server.

Save

Save