Anyone who knows a little bit about the Linux and UNIX platforms, he/she must be heard about the LAMP Stack. LAMP Stack is the group of services that work together in order to serve the dynamic websites and web apps. The LAMP stack can be installed and configured on almost all Linux variants such as Amazon Linux, Ubuntu, Red Hat, and Fedora. In this tutorials, we will explain how to install LAMP on Ubuntu 16.04 EC2 instance. Generally, LAMP stack consists of the following components:
- L= Linux as the operating system platform.
- A= Apache as the Web server
- M= MySQL as the database server
- P= PHP as the supportive language
If you will be able to install LAMP on Ubuntu server successfully, there will be slight differences to install LAMP on other Linux variants. But the basic concept and commands would remain almost the same. In order to install and configure LAMP on Ubuntu server, running as an EC2 instance, you need to perform the following steps.
- Install and configure Apache web server
- Install and configure MySQL database server
- Install and configure PHP components
- Test the LAMP configuration
If you are not too much familiar with AWS cloud platform or eager to learn more about AWS Cloud articles, here we have few articles for you:
- Creating Amazon EC2 instances
- Connecting Amazon EC2 Linux Instances
- Enabling Multi-Factor Authentication for IAM users
Since we are going to perform this exercise on an EC2 instance but it will not matter too much whether you are using an EC2 instance, physical server, or virtual machine. The same steps can be used to install LAMP on Ubuntu server running on a physical host or virtual machine that runs outside the AWS cloud.
Installing Apache Web Server on Ubuntu
To setup LAMP stack on your Ubuntu machine, the first step is installing and configuring Apache Web server. To do so, you need to perform the following steps:
Execute the following commands to update the existing packages and install the Apache packages.
sudo apt-get update -y sudo apt-get install apache2 -y
Ensure that apache service is running properly. If not, start it manually by executing the following commands:
sudo service apache2 status sudo service apache2 restart
Once you have installed the Apache server and tested that the service is running properly, type the following URL on a Web browser to test that Web server is functioning properly.
The default page of Apache web server should be displayed as shown in the following figure.
You also need to make sure that ports 80 and 443 are allowed publically through the firewall.
Install and configuring MySQL database server
After configuring the Apache web server, next you need to install and configure MySQL to setup LAMP stack properly. MySQL is a database service that organizes and provides access to databases where your website can store information. To install MySQL on Ubuntu machine, use the following steps.
sudo apt-get install mysql-server mysql-client mysql-common php-mysql
The MySQL installation will begin and you would be asked to set the MySQL root password as shown in the following figure. Set the desired MySQL root password and complete the installation.
After completing the MySQL installation, execute the following command to create database directory structure where MySQL will store its information.
sudo mysqld --initialize
The default installation of MySQL has some security holes. For the testing purpose, you may leave the SQL installation with default settings. But for the production network, use the following command to enhance MySQL database security.
Follow the on-screen instructions, read each of the displayed settings options and understand what impact these settings will have.
Now your Apache web server and MySQL database server are ready for the LAMP Stack.
Installing PHP Components
PHP stands for Personal Home Page is a scripting language used in web designing. Here, we assuming that you know the basics of PHP and its uses. We will just cover the installation of PHP. To install PHP and its required packages on Ubuntu 16.04, use the following command:
sudo apt-get install phpmyadmin php-mbstring php-gettext
On the Web server selection screen, select Apache2 as the web server and complete the installation.
On the Configuring phpmyadmin screen, click Yes and specify a PHPMyAdmin password.
Now, execute the following commands to adjust the PHP settings:
sudo phpenmod mcrypt sudo phpenmod mbstring
Finally, use the following command to restart the Apache server.
sudo service apache2 restart.
Now, if you test your default web server page, you will get the content of the index.html file. However, we need to change it as we want to serve dynamic web pages using PHP components. To achieve this, you need to modify the dir.conf file using the following command.
sudo vi /etc/apache2/mods-enabled/dir.conf
In the dir.conf file, change the order of index directory by placing index.php before of all the other index files as shown in the following figure.
In order to enhance functionalities of PHP, you may need to install some additional PHP components. To install additional PHP components, depending on your choice and need, you can use sudo apt-cache show command to list all the optional PHP components:
To know more about any of the specific option package, you the following command:
apt-cache show package_name
Now, your (L)inux (A)pache (M)ySQL and (P)HP = LAMP stack is installed and configured successfully.
Testing LAMP Configuration
To test and validate that your Apache, MySQL, and PHP services are working properly, you can create a PHP test script under the web server directory that is /var/www/html in Ubuntu Linux. Use the following command to create the PHP test script and write the script code as given below.
sudo vi /var/www/html/test.php <?php phpinfo(); ?>
Save the test.php file and restart the apache service.
sudo service apache2 restart
To confirm that all your configuration is done successfully, test each of LAMP service using the following URLs.
- Web server test URL: http://server-ip-address
- PHP script test URL: http://server-ip-address/test.php
- PHPMyAdmin test URL: http://server-ip-address/phpmyadmin
The following figure show the default login page of PhpMyAdmin
For the security perspective, we recommend you to remove the test script file you created earlier.
That’s all you need to install LAMP on Ubuntu 16.04 server. The same steps can also be used to install LAMP on Ubuntu 14.04. If you have any further suggestions or doubts, please write to us in the comment box we will try to address them.