How to Install Apache, MySQL &
PHP on Ubuntu 20.04
Written by Rahul, Updated on April 28, 2020
Apache, lamp, mysql, PHP, Ubuntu 20.04
A combination of Linux, Apache, MySQL, and PHP is known as LAMP
stack is the popular web hosting environment for the PHP based
application. Here Linux is an operating system, Apache is the
popular web server developed by Apache Foundation, MySQL is
relational database management system used for storing data and
PHP is the widely used programming language.
This article will help you to Install Apache 2.4, MySQL 8.0 and PHP
7.4 on Ubuntu 20.04 LTS (Focal Fossa) system. Let’s begin the
installation of LAMP stack your Ubuntu machine.
Prerequisites
You must have root or sudo privileged user access to your Ubuntu
20.04 system. Login to your Ubuntu system using GUI for Desktop
and SSH for the server edition.
ssh ubuntu@remote.host
Now upgrade current packages to latest version.
sudo apt update
sudo apt upgrade
Follow the initial server setup instruction to get ready your system
for working.
Step 1 – Installing PHP
PHP 7.4 packages are available under the default repositories on
Ubuntu 20.04 LTS. To install PHP on your system, update apt index
and then install it on your system.
sudo apt update
sudo apt install -y php7.4
Also install additional PHP modules required for your application.
sudo apt install php7.4-curl php7.4-gd php7.4-json php7.4-mbstring
php7.4-xml
Step 2 – Installing Apache2
Next, you need to install Apache2 package on your system. The
Apache packages are are available under the default apt
repositories. You also need to install libapache2-mod-php module to
work PHP with Apache2.
Type below commands to install it:
sudo apt install apache2 libapache2-mod-php7.4
To create additional virtual hosts visit this tutorial.
Step 3 – Installing MySQL Server
The default Ubuntu 20.04 apt repositories contains MySQL server
8.0. Finally, install mysql-server packages for the MySQL database.
Also, install the php-mysql package to use MySQL support using
PHP. Use the following command to install it.
sudo apt install mysql-server php7.4-mysql
The installer will prompt for the root password, This password will
work for your MySQL root user. After installing MySQL execute the
following command for initial settings of MySQL server. You will see
that script will prompt for more settings than earlier MySQL versions
like password validation policy etc.
sudo mysql_secure_installation
Press y|Y for Yes, any other key for No: y
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
New password:
Re-enter new 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
Step 4 – Installing phpMyAdmin (optional)
phpMyAdmin provides a user friendly web interface to manage
MySQL database server. You can install phpMyAdmin on Ubuntu
20.04 by executing the following command:
sudo apt install phpmyadmin
The installation process will prompt to select web server to
configure. Select “Apache” as web server to run phpMyAdmin.
Next, this will prompt to create database for phpMyAdmin and
prompt for the administrative user access details. Complete all steps
to finish phpMyAdmin installation.
Step 5 – Manage Services
We have done with the installation of LAMP stack on Ubuntu 20.04
LTS system. The below commands will help you to start/stop or
restart Apache and MySQL services running with systemd.
To restart Apache and MySQL services, type:
sudo systemctl restart apache2
sudo systemctl restart mysql
To start Apache and MySQL services, type:
sudo systemctl start apache2
sudo systemctl start mysql
To stop Apache and MySQL services, type:
sudo systemctl stop apache2
sudo systemctl stop mysql
Step 6 – Adjusting Firewall Rules
You can directly provide a service name like “http” or “https” to
allow. The firewalld uses /etc/services file to determine the
corresponding port of the service.
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 7 – Test Setup
After completing all setup. Let’s create a info.php file website
document root with following content.
sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Now access this file in web browser. You will see the screen like
below with all details of PHP on server.
Also access the phpMyAdmin
Congratulation’s! You have successfully configured web server on
your Ubuntu 20.04 LTS system. Read our next article Installing
LAMP Stack on RHEL based systems