Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Install Linux, Apache, MySQL, PHP (LAMP Stack on Ubuntu 18.04

Table Of Contents

    ## Table of Contents
    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Installing Apache and Updating the Firewall](#installing-apache-and-updating-the-firewall
    – [Installing MySQL](#installing-mysql
    – [Installing PHP and Testing PHP Processing on Web Server](#installing-php-and-testing-php-processing-on-web-server
    – [Conclusion](#conclusion

    ## Introduction

    The LAMP stack is an open source software used to create websites and web applications. It consists of a Linux operating system, Apache HTTP Server, MySQL Database and PHP programming language.

    LAMP stack represents Linux operating system with the Apache HTTP server and the website data is stored in a MySQL database and PHP programming language processes dynamic content.

    ## Prerequisites

    You should have access to an Ubuntu 18.04 server and created a non-root user account with sudo privileges by following our guide, [Initial server setup with Ubuntu 18.04](

    ## Installing Apache and Updating the Firewall

    The Apache web server is well-documented and has been full usage in the history of the web and it is default choice for hosting a website.

    Installing Apache using Ubuntu’s package manager, apt:
    $ sudo apt update
    $ sudo apt install apache2
    By adding sudo command, these operations are executed with root privileges. It will ask you the regular user’s credentials to verify your intention.

    After entering the password, apt will directs which package is installed and how much disk space it will occupy. Then you have to press ‘y’ and hit enter to proceed installation.

    ## Adjust the Firewall to Allow Web Traffic

    Assume that, you have done with initial server setup and enabled the UFW Firewall and you have to make sure that firewall allows HTTP and HTTPS traffic.
    To check that UFW has an application profile for Apache, use the below command.
    $ sudo ufw app list



    In Apache full profile, It enables traffic to ports 80 and 443:
    $ sudo ufw app info Apache Full


    Use this command to allow incoming HTTP and HTTPS traffic for this profile.
    $ sudo ufw allow ‘Apache Full’
    You can check it by visiting your server’s public IP Address in your web browser by using the following command.


    You will see the default ubuntu 18.04 Apache web page. It is for information and testing purpose.

    It should look like this:


    If you get output like this, then your web server is installed correctly and accessible through the firewall.

    ## How to Find your Server’s Public IP Address

    There are many ways to find your server’s Public IP Address. First, you can use iproute2 tools to get IP Address by using below command.
    $ ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s//.*$//’
    This command will take you to three lines back. They are all correct addresses but your system will use only one of them.
    And another method is to use curl utility as follows. This is done by asking a specific server to know what your IP Address is:
    $ sudo apt install curl

    $ curl

    ## Installing MySQL

    Now the web server is running. Next, we have to install MySQL. MySQL is a database Management system, it will organize and provide access to a database where your site can store information.

    Now use apt to install this software:
    $ sudo apt install mysql-server
    This command shows the list of packages that will be installed and the amount of disc space that they will take over. To continue hit ENTER.
    When the installation is complete, run a simple script that comes as default when it is installed with MySQL, it removes some dangerous defaults and locks down access to a database system.
    Start the script by typing the below command:
    $ sudo mysql_secure_installation
    The script will ask you to give a password for further use within the MySQL system. Then, you will be asked, if you want to configure the VALIDATE PASSWORD PLUGIN.


    Press Y for yes, or any other key to continue without enabling.

    If you have enabled VALIDATE PASSWORD PLUGIN, you will be asked to select a level of password validation.


    Select any one among three levels of password validation, enter your new password. Then, you will be shown the strength of the password you have entered. Press Y if you are satisfied with the password.


    Press Y for the rest of the questions and hit ENTER at each prompt. This will disable some of the anonymous users and remote root logins, test the database and load these new rules so that MySQL will immediately respect the changes we have made.

    While running MySQL 5.7 in Ubuntu systems, the root MySQL user by default is set to authenticate by using the auth_socket rather than with a password. This gives better security and usability in most of the cases but, it complicates things when you want to allow an external program to access the user.

    If you want to connect to MySQL as a root using a password, you will need to switch the authentication method from auth_socket to mysql_native_password. To do so, open MySQL prompt using the below command.
    $ sudo mysql
    Then, use the below command to check which authentication method each of your MySQL user accounts is using:
    mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
    The output looks like this:


    Run the below command to reload the grant tables and make your changes effective.
    Now, check the authentication method employed by root to confirm that it is no longer authenticates using the auth_socket plugin. Do it by using the below command.
    mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
    Then you will get an output as follows:


    After confirming this, you can exit the MySQL shell using the below command.
    mysql> exit
    ## Installing PHP and Testing PHP Processing on Web Server

    PHP is component of your setup that will process code to display dynamic content. It can run scripts, connect your MySQL databases to get information.

    Once again update the apt system to install php.In addition to this, including some extra packages so that PHP code can run under Apache server and interact with MYSQL database.
    $ sudo apt install php libapache2-mod-php php-mysql
    Now PHP is installed.

    Now a user requests a file from the server, Apache will first look for the file called index.html. Now we have to tell the web server to take PHP files first so that Apache will look into index.php file first

    To do that, type the following command to open dir.conf file in a text editor with root privileges.
    $ sudo nano /etc/apache2/mods-enabled/dir.conf
    /etc/apache2/mods-enabled/dir.conf file looks like this:

    DirectoryIndex index.html index.cgi index.php index.xhtml index.htm

    Move the PHP index file to the first position after the DirectoryIndex specification like this:

    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

    When you finished it save and close the file by entering ctrl+X. Confirming the save by pressing Y and hit ENTER button to verify the save location.
    After this restart the Apache server, in order for your changes to be recognized.
    To do this enter the following command.
    $ sudo systemctl restart apache2
    You can also check the status of the status of the Apache2 by entering the below command.
    $ sudo systemctl status apache2
    Then you will get output like this:


    Hit q to exit from the status output.

    To enhance the functionality of PHP, enter the following command.
    $ apt search php- | less
    Use the arrow keys to scroll up and down. Press q to exit from the status output.

    bandwidthd-pgsql/bionic 2.0.1+cvs20090917-10ubuntu1 amd64
    Tracks usage of TCP/IP and builds HTML files with graphs

    bluefish/bionic 2.2.10-1 amd64
    advanced Gtk+ text editor for web and software development

    cacti/bionic 1.1.38+ds1-1 all
    web interface for graphing of monitoring systems

    ganglia-webfrontend/bionic 3.6.1-3 all
    cluster monitoring toolkit – web front-end

    golang-github-unknwon-cae-dev/bionic 0.0~git20160715.0.c6aac99-4 all
    PHP-like Compression and Archive Extensions in Go

    haserl/bionic 0.9.35-2 amd64
    CGI scripting program for embedded environments

    kdevelop-php-docs/bionic 5.2.1-1ubuntu2 all
    transitional package for kdevelop-php

    kdevelop-php-docs-l10n/bionic 5.2.1-1ubuntu2 all
    transitional package for kdevelop-php-l10n

    To know the long description of the package, type the following command:
    $ apt show package_name
    To find what the php-cli does, use the below command.
    $ apt show php-cli
    Along with large information, you would look like this.


    If you decided php-cli is looking something that you want, then type this:
    $ sudo apt install php-cli
    To test whether the system is configured correctly for PHP, create a very basic PHP script called info.php. For Apache to find this file and server it correctly. It must be saved to particular directory called web root.

    In Ubuntu 18.04 , this directory is located at /VAR/WWW/HTML/. Create the file at that location by typing:
    $ sudo nano /var/www/html/info.php
    Then you get a blank file and add the below text.


    When you are finished save and close the file.

    Now you can get your web server is correctly able to print the message generated by php script.
    To try this visit the webpage. For this, you need server’s public IP address.

    The address you have to visit is:


    The page you get looks like this:


    This page provides some basic info about server from the PHP. It is useful for debugging. If you see the page as above then your PHP is working correctly.

    If you want to remove the file, then use the following command:

    $ sudo rm /var/www/html/info.php

    You can recreate this page if you want the information later.

    ## Conclusion

    Now LAMP stack is installed. So that you have a platform that allows you to install many kind of websites and web servers on your system. And the next step is you have to ensure that the connections to your system are secured, by providing them via HTTPS.