How To Install Linux, Apache, MySQL and PHP (LAMP Stack) On Ubuntu 20.04?

June 30th, 2022
How To Install Linux, Apache, MySQL and PHP (LAMP Stack) On Ubuntu 20.04?

How To Install Linux, Apache, MySQL and PHP (LAMP Stack) On Ubuntu 20.04?

The LAMP (Linux, Apache, MySQL and PHP) stack is one of the most common approaches to building dynamic websites. A LAMP stack uses mature and reliable opensource software as building blocks and has been the cornerstone for a variety of projects including WordPress, Facebook, and Wikipedia. Understanding how to install and configure a LAMP stack is an important skill for Linux administrators and web developers alike.

To help you get hands on with LAMP stacks, in this article we'll provide you with a step-by-step tutorial on how to install and configure a basic LAMP stack on Ubuntu 20.04. As a bonus, we'll review how Tasksel can help you speed up the process.

What Is A LAMP Stack?

A LAMP stack is a set of four popular opensource components for web development.
Each of the letters in the LAMP acronym stands for one of the components:

  • Linux is the operating system in the stack.
  • Apache is the web server in the stack.
  • MySQL is the database in the stack.
  • PHP is the programming language in the stack.

With these components, web developers have all the components they need to build web applications that can serve dynamic content. Linux acts as the base layer where the other components are installed, Apache serves HTTP(S) pages, MySQL allows data persistence, and PHP allows developers to tie the web server and database layers together and create dynamic sites. For example, PHP can take data from a webform and perform CRUD (create, read, update, delete) operations on a MySQL database.

LAMP is prescriptive in which web server, database, and programming languages a developer should use, but there is flexibility in the Linux distribution. Ubuntu, Debian, RHEL, and CentOS/Rocky Linux are some of the most common distros used in LAMP stacks, but it's mostly a matter of preference.

Additionally, there are plenty of alternatives to the traditional LAMP stack. For example, some developers use Python or Perl instead of PHP in their stacks. Similarly, some use nginx (pronounced "engine x") instead of Apache to make a LEMP stack or replace MySQL with a different database or data persistence layer like PostgreSQL. There are also more fundamentally different LAMP alternatives like MEAN (MongoDB, Express.js, Angular.js, Node.js).

How To Install A LAMP Stack Quickly With Tasksel

We'll walk through the process of installing each individual LAMP stack component below, and we recommend following that process if it's your first time configuring a LAMP stack. But, if you're looking for a quick way to hit the ground running on Ubuntu or Debian, consider using Tasksel. Tasksel is a tool that helps Linux administrators install multiple related packages at the same time, and it installs the Apache, MySQL, and PHP components you need for a LAMP stack with just one command.

To use Tasksel to install the Apache, MySQL, and PHP components for your LAMP stack, follow this process:

  1. Install Tasksel
sudo apt install tasksel -y
  1. Use Tasksel to install lamp-server
sudo tasksel install lamp-server

Lamp Server Install With Tasksel

After the lamp-server install, you should see that Apache2, MySQL, and PHP are all installed on your server.
Post install LAMP components

How To Install Linux, Apache, MySQL and PHP (LAMP Stack) On Ubuntu 20.04

Ubuntu's LTS (long term support) releases are some of the most common Linux operating systems used in LAMP stacks. In this process, we'll use Ubuntu 20.04 LTS to install and configure a working LAMP stack.

Prerequisites:

Before we begin, you'll need:

  • Access to the terminal of an Ubuntu 20.04 system. If you don't have one, you can spin up a virtual private server on Cherry Servers.
  • sudo/root privileges

Run Apt Update

First, let's update our apt package lists and repositories with this command:

sudo apt update

You should see output similar to this:
Running apt update

Install Apache

Next, use apt to install the Apache webserver with this command:

sudo apt install apache2 -y

installing Apache

Install MySQL

Next, we'll install MySQL on the server. For a deep dive on the MySQL installation and configuration process — including information on hardening your installation — check out How to Install and Configure MySQL on Ubuntu 20.04.

For this step of our LAMP stack installation process, run this command to install the MySQL Server package:

apt install mysql-server -y

You can verify your MySQL installation by running this command:

mysql -V

Output should look similar to:
MySQL version check

Install PHP

Now we can install the scripting language of our LAMP stack. Specifically, we will install 3 packages:

  • php is the default package for the PHP scripting language
  • libapache2-mod-php is the PHP module for Apache
  • php-mysql is the PHP module for MySQL

To install all 3 packages, run this command:

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

You can verify your PHP installation by running this command:

php -v

Output should look similar to:
PHP version check

[Optional] Configure Firewall

By default, Ubuntu's uncomplicated firewall a.k.a. ufw is disabled. You can enable ufw to implement firewall policies and help secure your system. If you choose to leave ufw disabled, you can skip this step.

If you're not sure if ufw is enabled on your system, you can check its status with this command:

sudo ufw status

If ufw is enabled, there are three available applications (listed in the output of sudo ufw app list) related to Apache you can allow through the firewall:

  • Apache will allow traffic on TCP port 80 (default HTTP) only
  • Apache Secure will allow traffic on TCP port 443 (default HTTPS) only
  • Apache Full will allow traffic on both TCP ports 80 and 443

For our example, will allow inbound traffic to Apache on TCP ports 80 and 443 with this command:

sudo ufw allow in "Apache Full"

You should see output similar to:

Rule added
Rule added (v6)

Check The Apache Test Page

Use a web browser to access http://<your server's IP> (where <your server's IP> is the IP address of your Ubuntu machine).
You should see the default Apache2 Ubuntu Default Page.
Apache Test Page

Configure And Enable Your Virtual Host

With all the LAMP stack components installed, you could now move on to testing PHP processing and MySQL connectivity in the subsequent steps. By default, Apache will serve web pages in the /var/www/html, and that may be enough for simple sites.

However, Apache Virtual Hosts let you create multiple sites on a single server and are an important concept in Apache web server administration. So, let's create a Virtual Host for our site by following these steps:

Note: We'll name our site cherryservers.local. However, you can replace cherryservers.local with any other Apache virtual host name.

  1. Disable the default virtual host:
sudo a2dissite 000-default.conf
  1. Copy the 000-default.confApache configuration file to a new /etc/apache2/sites-available/cherryservers.local.conf file:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/cherryservers.local.conf
  1. Use a text editor like nano or vim to edit the etc/apache2/sites-available/cherryservers.local.conf to contain these settings:
<Directory /var/www/html/cherryservers.local/public>
        Require all granted
</Directory>
<VirtualHost *:80>
        ServerName cherryservers.local
        ServerAlias www.cherryservers.local
        ServerAdmin pepperandegg@localhost
        DocumentRoot /var/www/html/cherryservers.local/public
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  1. Create the DocumentRoot directory from the cherryservers.local.conf file:
sudo mkdir -p /var/www/html/cherryservers.local/public
  1. Make the www-data user and group owner of the directory and its content:
sudo chown -R www-data:www-data /var/www/html/cherryservers.local/public 
  1. Give the www-data user read, write, and execute permissions on the directory and its contents, and give everyone else read and execute permissions:
sudo chmod -R 755 /var/www/html/cherryservers.local/public
  1. Link the new site to your Apache sites-enabled:
sudo a2ensite cherryservers.local
  1. Reload the Apache webserver:
sudo systemctl reload apache2

Serve A Test.php Page

Next, let's serve a simple test.php page that demonstrates PHP is working on our site.

  1. Use a text editor like nano or vim to create a new /var/www/html/cherryservers.local/public/test.php file with this content:
<?php
phpinfo();
  1. Use a web browser to access http://<your server's IP>/test.php (where <your server's IP> is the IP address of your Ubuntu machine). You should see a page that displays PHP information similar to this:
    PHP test page

Configure MySQL Database Access For PHP

Now, let's create a test MySQL database and configure a user for PHP to access it with.

  1. Launch MySQL:
sudo mysql
  1. At the mysql> prompt, create a testDB database:
CREATE DATABASE testDB;
  1. Create a MySQL user named phpUser with a password of password (replace password with a strong password):
CREATE USER 'phpUser'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

4.Grant the phpUser user full privileges on the testDB database:

GRANT ALL ON testDB.* TO 'phpUser'@'%';
  1. Create a breakfastMenu table in the testDB
CREATE TABLE testDB.breakfastMenu(food VARCHAR(50), description VARCHAR(255));
  1. Add some test data to the breakfastMenu table:
INSERT INTO testDB.breakfastMenu(food, description) VALUES ('Pepper and Egg With Giardiniera', 'Great Way To Start The Day');
  1. Exit MySQL:
exit

Test PHP And MySQL

Finally, we can create a PHP script to display data from our MySQL database.

  1. Use a text editor to create a new PHP script at /var/www/html/cherryservers.local/public/cherry.php with this content (be sure to change your "password" to match the MySQL password you configured earlier):
<?php
$DBconnect=mysqli_connect("localhost","phpUser","password","testDB");
$result = mysqli_query($DBconnect,"SELECT * FROM breakfastMenu");
while($row = mysqli_fetch_array($result))
    print_r($row['food']);
mysqli_close($DBconnect);
?>

  1. Use a web browser to access http://<your server's IP>/cherry.php (where <your server's IP> is the IP address of your Ubuntu machine). You should see a page that displays the food value we inserted into the database previously, similar to this:
    Test PHP and MySQL

Conclusion

That's it! Now you have a working LAMP stack on an Ubuntu 20.04 system. From here, you can delete our test .php scripts and move on to further hardening your system — for example, by configuring Apache to use HTTPS — and creating dynamic content for your site.

Helping engineers learn 💡 about new technologies and ingenious IT automation use cases to build better systems 💻

Join Cherry Servers Community

Get monthly practical guides about building more secure, efficient and easier to scale systems on an open cloud ecosystem.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.