How to install LAMP stack web server on Ubuntu 20.04
LAMP stack is a simple yet popular suite of widely supported web server software. It commonly refers to the combination of Linux, Apache, MySQL and PHP. LAMP stack makes a great starting point for users who want to get a web server running quickly and easily on their new cloud server.
In this guide, we’ll walk you through the steps required to install a basic web server using Ubuntu 20.04, Apache2, MariaDB and PHP. These together form a reliable LAMP stack and the groundworks for building your websites! The steps in this guide are written for use with a newly deployed Ubuntu 20.04 UpCloud Cloud Server. The steps and installed software may vary on other platforms.
Step 1: Preparing your Ubuntu server
To begin with, you need a cloud server to run the LAMP stack software. If you are new to UpCloud, have a look at our quick started guide for deploying your first cloud server and how to connect to it.
Once you have your cloud server up and running and connect to it using SSH, you can get started!
First of all, ensure everything is up to date on your server:
sudo apt update sudo apt upgrade
Now open ports 22 (for SSH), 80 and 443 and enable Ubuntu Firewall (ufw):
sudo ufw allow ssh sudo ufw allow 80 sudo ufw allow 443 sudo ufw enable
Step 2: Installing and testing Apache2
Install Apache using apt:
sudo apt install apache2
Confirm that Apache is now running with the following command:
sudo systemctl status apache2
You should the get an output showing that the apache2.service is running and enabled.
● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-11-03 10:32:26 UTC; 1min 6s ago Docs: https://httpd.apache.org/docs/2.4/ Main PID: 52943 (apache2) Tasks: 7 (limit: 2282) Memory: 11.9M CGroup: /system.slice/apache2.service ├─52943 /usr/sbin/apache2 -k start ├─52944 /usr/sbin/apache2 -k start ├─52945 /usr/sbin/apache2 -k start ├─52946 /usr/sbin/apache2 -k start ├─52947 /usr/sbin/apache2 -k start ├─52948 /usr/sbin/apache2 -k start └─52953 /usr/sbin/apache2 -k start
Once installed, test by accessing your server’s IP in your browser:
You should see a page with an “Apache2 Ubuntu Default” header showing that Apache2 has been installed successfully. If you do not see this, please ensure that the previous commands in this section have completed without error
Step 3: Installing and testing PHP 7.4
PHP 7.4 is the latest available right now so let’s install that along with some regularly used modules:
sudo apt install php7.4 php7.4-mysql php-common php7.4-cli php7.4-json php7.4-common php7.4-opcache libapache2-mod-php7.4
Check the installation and version:
Restart Apache for the changes to take effect:
sudo systemctl restart apache2
Create a phpinfo.php test page:
echo '<?php phpinfo(); ?>' | sudo tee -a /var/www/html/phpinfo.php > /dev/null
Test everything is working by accessing the following in your browser:
You should see a PHP Version 7.4.3 page listing all of your PHP options. If you don’t or it tries to download a file, double-check that all of the above steps have completed without error.
Once you’ve confirmed that PHP is working correctly, delete the test file.
sudo rm /var/www/html/phpinfo.php
The information displayed in the PHP info could be used to find an attack vector against your web server so best not to leave it publicly accessible.
Step 4: Installing and securing MariaDB
MariaDB is a fork of MySQL from some of the original MySQL team and is a drop-in replacement. We’ll be using this over MySQL itself in this guide!
Install the required packages:
sudo apt install mariadb-server mariadb-client
Once installed, check it’s running correctly:
sudo systemctl status mariadb
You should see an output similar to the example below.
● mariadb.service - MariaDB 10.3.25 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-11-03 10:33:12 UTC; 4s ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 53554 (mysqld) Status: "Taking your SQL requests now..." Tasks: 31 (limit: 2282) Memory: 65.9M CGroup: /system.slice/mariadb.service └─53554 /usr/sbin/mysqld
Secure your newly installed MariaDB service:
As you have no root password set for MariaDB you should simply press Enter when prompted, pressing Y on the next question to then set a root password (keep this safe and secure!) With that set, you can press Enter for the remaining questions as the defaults for each of these will help to secure your new installation.