How to Setup a Basic LAMP on Ubuntu 16

Try it in our public cloud & Get $50 Credit
CLAIM NOW

This guide will explain how to setup a basic LAMP on Ubuntu. LAMP is a bundle of Linux (an operating system similar to Unix), Apache Server, MySQL, and PHP. This bundle allows users to setup many open source software packages such as WordPress, Magento for Ecommerce, and Drupal for Content Management System. Ubuntu 16.04 LTS (long-term support) is the latest version of Ubuntu: a Debian-based Linux operating system that can be used as the Linux part of LAMP.

On Ubuntu 16.04 LTS, PHP7 will be use by default and should be a dropped-in replacement for PHP5. PHP7 is a newer version of PHP that provides higher performance, better speed than PHP5 and many more features. Even if PHP7 is designed to be retro compatible with your code, we would recommend that you test it thoroughly before going into production.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Servers) with Ubuntu 16.04 LTS installed.
• All commands should be run as the root user

If you don't want to go through the whole process of setting up a basic LAMP for your Ubuntu 16.04 server, you can always try our One-Click Apps and get a fresh LAMP in seconds.

Tutorial

Step 1: Install Apache

apt-get update && apt-get -y install apache2

apt-get is the Debian/Ubuntu package manager and the above command will install Apache.

Afterwards, you can verify that Apache is working fine by reaching http://your_server_IP_address in your web browser.

Step 2: Install MySQL

While installing the packages, you will be prompted to set a root password for MySQL. It is recommended to set a strong password. Keep the password safe, as you will need it later.

apt-get -y install mysql-server php-mysql

This apt-get will install PHP and MySql.

MySQL secure installation: The following step is not mandatory, but it is highly recommended for servers in production use. It will provide better security to your MySQL installation.

mysql_secure_installation

This interactive script will apply security tweaks to MySQL. You will be asked to enter the MySQL root password that you have set earlier. It is then recommended to remove anonymous users, disallow remote root login, remove the test database and reload the privileges table.
At this point, your MySQL database service is now ready to use.

Step 3: Install PHP

This command will install PHP with the default common/core modules. If you need specific modules, you can add them right away to the following command line.
apt-get -y install php libapache2-mod-php php-mcrypt
This apt-get will install modules of php that you will need.

You can add required modules at the end of the command line : php-MyRequiredModuleName Use the following command to list available PHP modules and libraries :
apt-cache search php-

Step 4 : Configure Apache to handle PHP

Edit the following file using your favorite text editor : /etc/apache2/mods-enabled/dir.conf

 <ifmodule mod_dir.c="">
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</ifmodule>

Here, you need to move “index.php” to the front of the DirectoryIndex list, as displayed above. This way, Apache will prefer PHP files over the rest. You must restart Apache in order to apply the new configuration, use this command :

service apache2 restart

You can now test Apache+PHP by creating a basic index.php file in the default website root folder which will display the PHPInfo.

Create test file /var/www/html/index.php on Ubuntu 16

 <?php
phpinfo();
?>

Once the file is created, http://your_server_IP_address should now display the PHPInfo page. If the PHPInfo loads without error, it means Apache and PHP are working well together. We recommend that you remove the PHPinfo after you have tested your installation for security reasons.

Conclusion

At this point, your basic LAMP stack is installed, configured and ready to use. Enjoy! If this guide was helpful to you, kindly share it with others who may also be interested.