How To Install The LAMP Stack On Ubuntu Linux


The LAMP stack is a group of open-source software that is usually installed together to enable a server to host websites and web applications. The acronym represents L for the Linux Operating System, A for Apache HTTPD Web Server, M for MySQL, and P for PHP. In this tutorial, we will cover how to install the LAMP stack on Ubuntu Linux.

Update Software Packages

Before we start the installation it is a good idea to update software packages with the following command:

sudo apt update 

Install Apache Httpd Web Server

Next, we will install Apache HTTPd Web Server:

sudo apt install -y apache2 apache2-utils

Once the installation is completed the Apache service should be automatically started. Check Apache’s status with systemctl:

sudo systemctl status apache2

The output would look something like the following:

 apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
   Active: active (running) since Sat 2018-09-08 10:46:05 UTC; 3min 37s ago
 Main PID: 1610 (apache2)
    Tasks: 55 (limit: 505)
   CGroup: /system.slice/apache2.service
           ├─1610 /usr/sbin/apache2 -k start
           ├─1612 /usr/sbin/apache2 -k start
           └─1613 /usr/sbin/apache2 -k start

If it is a case that Apache is not running then run the following command:

sudo systemctl start apache2

Also, configure Apache to start automatically when your server boots up:

sudo systemctl enable apache2

Ensure that the HTTP firewall rule is enabled:

sudo ufw allow http

Once you completed installing the Apache HTTPd webserver you can type in the server up address in your web browser to see the Apache webserver default page.

Apache HTTPd Default Web Page

Install MySQL(MariaDB)

Once the webserver is up and running we will install MySQL(MariaDB) next. MariaDB is a community-developed fork of the MySQL relational database management system. Run the following command to start installing MariaDB:

sudo apt install mariadb-server mariadb-client

Once the installation is completed you should check the status to see if MariaDB is active with the following command:

sudo systemctl status mariadb

Sample output:

mariadb.service - MariaDB 10.1.34 database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-09-08 11:13:27 UTC; 21s ago
     Docs: man:mysqld(8)
 Main PID: 3473 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 27 (limit: 505)
   CGroup: /system.slice/mariadb.service
           └─3473 /usr/sbin/mysqld

If it is not running you can start the MariaDB server with the following command:

sudo systemctl start mariadb

The next step is to enable MariaDB to start automatically at system boot:

sudo systemctl enable mariadb

Once the MariaDB service is up and running, the next step is to start the MySQL installation script:

sudo mysql_secure_installation

When it prompts you for the MariaDB root password you can go ahead and press enter and then enter y to set the root password for the MariaDB server. Next, you can press enter through the remaining steps which will remove the anonymous user, disable remote root login, and remove the test database.

Install PHP

The last step is to install PHP on your Ubuntu Linux server or instance:

sudo apt-get install php libapache2-mod-php php-mysql

Once PHP is installed create a script info.php under the webroot directory:

sudo vi /var/www/html/info.php

The script will create a blank file and then we will add the following PHP code inside the file:


Once all the above steps are completed view the info.php page in the web browser using the server IP:


The page will look similar to the one below:

PHP Info Web Page

Chad Crouch

GoLang Fanatic & FullStack Geek experienced in multiple enterprise environments. Has a huge passion for the open-source world specifically Linux and Opensource Software.