How to Install Apache, MySQL, and PHP (LAMP) Stack on Ubuntu 20.04 LTS
Introduction
A LAMP stack is a group of four open-source software components that form the foundation of building and running high-performance dynamic websites and web applications. It's an acronym for Linux, Apache, MySQL/MariaDB and PHP.
The LAMP stack is simple, stable, and powerful, and its first layer is comprised of the popular Linux Operating System with flexible configuration options and tight security. Apache is the second layer in the pile and offers you compiled module support for major scripting languages such as PHP, Perl, Python, Node.js, etc. This HTTP web server delivers web content through the internet. When it comes to the database layer, you've two options when deploying a LAMP stack. You can either use MySQL or MariaDB as an Relational Database Management System (RDMS) for your web applications. The last package in the list is PHP. This is a scripting language that you can easily embed in HTML documents to create static or dynamic websites.
The majority of modern Content Management Systems (CMS) such as WordPress, Joomla, Drupal, and Magento require a LAMP stack to work. Therefore, in this guide, you'll learn how to configure the right environment for them. The same setup will also work for any custom web application that you may intend to build with the LAMP stack technology.
Prerequisites
To follow along with this guide, you need the following:
- An Ubuntu 20.04 server
- A non-root sudo user.
1. Install Apache Webserver
SSH to your Ubuntu server as a non-root user, then update the package information index and upgrade your packages.
$ sudo apt update && sudo apt -y upgrade
Next, run the command below to install the Apache Web server.
$ sudo apt install -y apache2
Visit the URL below on a web browser and replace the 192.0.2.1
with the public IP address of your server or domain name.
You should see the default Apache web page as shown below.
After setting up the web server, you'll install a database in the next step.
2. Install a Database Server
You can either install MySQL or MariaDB server when deploying a LAMP stack. To install the MySQL server, run the command below.
$ sudo apt install -y mysql-server
To set up a MariaDB server, execute the command below.
$ sudo apt install -y mariadb-server mariadb-client
Irrespective of the database that you've chosen, run the command below to secure it.
$ sudo mysql_secure_installation
Enter the below choices and press Enter in each prompt to proceed.
MySQL Server
Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: n
Please set the password for root here.
New password: EXAMPLE_PASSWORD
Re-enter new password: EXAMPLE_PASSWORD
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No): y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
MariaDB Server
Enter current password for root (enter for none): :key_enter:
Set root password? [Y/n]: Y
New password: EXAMPLE_PASSWORD
Re-enter new password: EXAMPLE_PASSWORD
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
Once you've secure the installation, login to the RDMS as root:
$ sudo mysql -u root -p
Enter your database server root password and press Enter to proceed. Then, type the command below to create your first test_database
database.
MySQL server.
mysql> CREATE database test_database;
MariaDB server.
MariaDB [(none)]> CREATE database test_database;
Output.
Query OK, 1 row affected (0.00 sec)
Next, list the databases in the server by running the SHOW DATABASES
command.
MySQL server.
mysql> SHOW DATABASES;
MariaDB server.
MariaDB [(none)]> SHOW DATABASES;
Your test_database
should be in the list below.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_database |
+--------------------+
5 rows in set (0.01 sec)
Create a test_user
and assign full privileges to the database you've just created. You'll require the details of this user when testing database connectivity with PHP. Replace EXAMPLE_PASSWORD
with a strong value.
MySQL server.
mysql> CREATE USER 'test_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'EXAMPLE_PASSWORD';
GRANT ALL PRIVILEGES ON test_database.* TO 'test_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
MariaDB server.
MariaDB [(none)]> CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'EXAMPLE_PASSWORD';
GRANT ALL PRIVILEGES ON test_database.* TO 'test_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Your database server is now ready and you can move ahead to installing a scripting language.
3. Install PHP
In this step, you'll install the PHP package. Run the command below.
$ sudo apt install -y php
Since most web applications rely on some PHP extensions, install the most common ones using the command below.
$ sudo apt install -y php-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}
Restart the Apache webserver to load PHP.
$ sudo systemctl restart apache2
To test PHP, create an info.php
file in the root directory of your web server.
$ sudo nano /var/www/html/info.php
Then, enter the information below into the file.
<?php
phpinfo();
Save and close the file by pressing Ctrl + X, then Y and Enter. Then, in a web browser, visit the URL below and replace 192.0.2.1
with your server's correct public IP address.
You should get a detailed PHP page as shown below.
Next, test PHP connectivity with the database that you created earlier. Open a new file for editing using nano.
$ sudo nano /var/www/html/database_test.php
Then, enter the information below into the file.
<?php
$conn = new mysqli('localhost', 'test_user', 'EXAMPLE_PASSWORD', 'test_database');
if ($conn->connect_error) {
die("Database connection failed: " . $conn->connect_error);
}
echo "Database connection was successful";
Save and close the file. Then, visit the address below in a web browser and replace 192.0.2.1
with your server's correct public IP address.
You should get the output below that shows that the script has successfully connected to the database.
echo "Database connection was successful";
Conclusion
Your LAMP stack is now ready on Ubuntu 20.04, and you can now go ahead and build a dynamic website or cloud-based web application.
From here, consider reading about One-Click LAMP stack installation guides and securing your web server with Let's Encrypt.
For more information about the LAMP stack, see the official documentations: