How to install Apache, MySQL, PHP (LAMP stack) on Ubuntu

The LAMP stack is a well-known and widely used web-development stack whose primary goal is to help you to build an open source web hosting environment. This stack consists of the following components: Linux (base operating system), Apache (web server), MySQL (relational database) and PHP (multi-platform scripting language). And here’s where LAMP comes from. You may have heard about some well-known projects which are heavily relying on this stack: Facebook, Wikipedia, Tumblr, and WordPress. In this article we’ll show you how you can install it on Ubuntu server.

Prerequisites

You must have root privileges or have an ability to use sudo to install required packages to your system.

Installation process

Installation process is easy and straightforward.

First, we’ll install Apache web server, then MySQL DB and then finalize the process with PHP scripting language.

Installing Apache

Before installing a new package, you need to update your packages list:

sudo apt update
1. How to install LAMP on Ubuntu - apt-get update

Now, let’s install Apache web-server:

sudo apt install -y apache2
2. How to install LAMP on Ubuntu - apt-get install apache2

Make sure that Apache server is up and running: 

sudo systemctl status apache2
3. How to install LAMP on Ubuntu - systemctl status apache2

Now, check that you can open your server’s default web page:

  • http://you-server-hostname

The default Apache web page would appear on the screen:

4. How to install LAMP on Ubuntu - Default apache webpage

Your web-server configuration files are stored at /etc/apache2 directory.

Default web page and other website content should be stored at /var/www/html directory.

It is not required, but if you’d like to change this webpage right now, use nano editor to make any changes:

sudo nano /var/www/html/index.html
5. How to install LAMP on Ubuntu - nano index

To save a file, press “CTRL+X”, Y, and “Enter”.

Installing MySQL

Update your system packages before installing the MySQL:

sudo apt update
1. How to install LAMP on Ubuntu - apt-get update

Now, the installation command:

sudo apt install -y mysql-server
6. How to install LAMP on Ubuntu - apt-get install mysql-server

Check that the MySQL service is up and running:

sudo service mysql status
7. How to install LAMP on Ubuntu - systemctl mysql status

MySQL initial configuration

At this stage we need to configure MySQL default security settings:  

sudo mysql_secure_installation
8. How to install LAMP on Ubuntu - mysql_secure_installation - 1

During this setup you’ll be asked about:

  • Password complexity validation settings
  • Password for the root user
  • Deletion of anonymous user
  • Preventing remote connection to DB using root account
  • Removing test database 
9. How to install LAMP on Ubuntu - mysql_secure_installation - 2

To make your MySQL database as secure as possible, you definitely need to consider answer Y to all those questions.

10. How to install LAMP on Ubuntu - mysql_secure_installation - 3

As soon as you complete all steps to protect your MySQL, you’ll see a message “All done”.

Let’s check if we can connect to the database.

Testing MySQL connection

You can start MySQL client and connect to the database using the following command:

sudo mysql
11. How to install LAMP on Ubuntu - sudo mysql

Type “exit” to exit the MySQL client (or press Ctrl+D).

Installing PHP

The PHP is the last component of the LAMP stack installation.

As usual, update the software packages list first:

sudo apt update
1. How to install LAMP on Ubuntu - apt-get update

Now, you need to install two additional packages:

  • php – scripting language interpreter
  • libapache2-mod-php – PHP module for Apache web-server
  • php-mysql – MySQL client for PHP

Use the following command:

sudo apt install -y php libapache2-mod-php php-mysql
12. How to install LAMP on Ubuntu - apt-get install php libapache2-mod-php php-mysql

Now, all packages have been successfully installed.

Verifying PHP

Now, use the following command to check the version of PHP interpreter:

php --version
13. How to install LAMP on Ubuntu - php version

Now, let’s verify the integration of PHP with the Apache web-server.

Create a file named test.php in /var/www/html/test.php directory:

sudo nano /var/www/html/test.php

Put the following boilerplate test PHP function:

 <?php
 phpinfo();
 ?>
14. How to install LAMP on Ubuntu - phpinfo

To Save the file, press “CTRL+X”, Y and hit “Enter”.

Check the PHP integration with Apache web-server by accessing the following URL:

  • http://you-server-hostname/test.php
15. How to install LAMP on Ubuntu - php setting page

It’s important to delete test.php once you’ve ensured that PHP is working, because it contains sensitive server information.

Execute the following command in your terminal:

sudo rm /var/www/html/test.php
16. How to install LAMP on Ubuntu - delete test

Free SSL certificate installation (Let’s Encrypt)

As a small bonus, let’s install a free Let’s Encrypt SSL certificate to your web-server.

Install Certbot

First you need to install Certbot (a script, which will renew your SSL certificate automatically).

Update the Ubuntu packages list:

sudo apt-get update
1. How to install LAMP on Ubuntu - apt-get update

Now, you can install Certbot:

sudo apt-get install -y certbot python3-certbot-apache
17. How to install LAMP on Ubuntu - apt-get install certbot python3-certbot-apache

Make required changes in your DNS control panel to set up a DNS name for your server.

Now, use the following command to create an SSL certificate:

sudo certbot --apache --redirect -d demo.hands-on.cloud -m no-reply@hands-on.cloud --agree-tos

Replace demo.hands-on.cloud with your own server DNS name.

Now, you can check that your Apache web-server has a valid free SSL certificate.

19. How to install LAMP on Ubuntu - Test Lets Encrypt SSL certificate

Configure automatic SSL certificate renewal

Let’s Encrypt certificates are valid for 90 days.

The Certbot updates the systemd timers and crontab to automatically renew your certificate.

Check that the systemd timer is active.

sudo systemctl list-timers | grep 'certbot\|ACTIVATES'
20. How to install LAMP on Ubuntu - Validate certbot systemd timers

Now, let’s check that the crontab entry is in place.

sudo ls -l /etc/cron.d/certbot
21. How to install LAMP on Ubuntu - Validate certbot crontab

Finally, let’s verify that the renewal process works without issues:

sudo certbot renew --dry-run
22. How to install LAMP on Ubuntu - certbot renew

Now you’re ready to deploy your PHP application.

WordPress, right? 😉

Summary

In this article we’ve installed the LAMP stack including Apache2 web server, MySQL database and PHP 7.4.3 in Ubuntu Linux. In addition to that we’ve configured a free SSL certificate and prepared your server for hosting your PHP applications.

We hope that this article was useful. If so, please, help us to spread it to the world!

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment