Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Last active April 7, 2021 14:27
Show Gist options
  • Save webmechanicx/4ac925ef405f76d2d798e88e9d60b385 to your computer and use it in GitHub Desktop.
Save webmechanicx/4ac925ef405f76d2d798e88e9d60b385 to your computer and use it in GitHub Desktop.
How To Install LAMP On Ubuntu 16.04 or 18.04 – EC2 Instance

How To Install LAMP On Ubuntu 16.04 – EC2 Instance

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.

  1. Install and configure Apache web server
  2. Install and configure MySQL database server
  3. Install and configure PHP components
  4. 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:

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

Install LAMP on ubuntu 16.04

Install LAMP on ubuntu 16.04

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.

http://server-ip-address

The default page of Apache web server should be displayed as shown in the following figure.

install apache web server on Ubuntu 16.04

install apache web server on Ubuntu 16.04

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.

Install LAMP Stack on Ubuntu ec2 instance

Install LAMP Stack on Ubuntu ec2 instance

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.

sudo mysql_secure_installation

Follow the on-screen instructions, read each of the displayed settings options and understand what impact these settings will have.

mysql_secure-Installation option for Ubuntu 16.04 ec2 instance

mysql_secure-Installation option for Ubuntu 16.04 ec2 instance

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.

selecting web server for phpmyadmin

selecting web server for phpmyadmin

On the Configuring phpmyadmin screen, click Yes and specify a PHPMyAdmin password.

specifying mysql phpmyadmin password on ubuntu 16.04

specifying mysql phpmyadmin password on ubuntu 16.04

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.

modifying dir.conf file on ubuntu linux ec2 instance

modifying dir.conf file on ubuntu linux ec2 instance

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

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.

The following figure show the default login page of PhpMyAdmin

install phpmyadmin on ubuntu 16.04

install phpmyadmin on ubuntu 16.04

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.

Share this:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment