In this article we will see how to easily install PHP7.4, Apache and Nginx servers and MySQL on Ubuntu 20.04 server. This environment is useful for Laravel project as well as any PHP projects. Let's dive right in.

Prerequisites

This article requires that you have an ubuntu 20.04 setup with non-root user with sudo privileges and a basic firewall setup. You can follow up this digital ocean setup guide for ubuntu 20.04 to perform this configuration.

Getting started

Install the latest packages in your ubuntu environment by running:

sudo apt update
sudo apt upgrade

This will install the latest packages and update your package index.

Install Apache

Apache is one of the most popular web servers. It has a very broad community, well documented and is used by multitude of websites, which makes a great choice for usage for web application deployment.

To install the apache server run in the terminal:

sudo apt install apache2 -y

If this is the first time you are using sudo, you will be prompted to enter your password to confirm you privileges to use apt  package. Then you simply have to add apache2 to your firewall list to allow it to operate.

To open port 80 run:

sudo ufw allow in "Apache"

To open port 443 for SSL/TSL encrypted traffic run:

sudo ufw allow in "Apache Secure"

To open both 80 and 443 ports run:

sudo ufw allow in "Apache Full"

Now if you run:

sudo ufw status

You will get (For port 80):

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                                
Apache                     ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)                    
Apache (v6)                ALLOW       Anywhere (v6) 

Now port 80 is open for traffic. And if you go to your public IP address you will get the default apache2 page.

Install Nginx

Alternatively we can use the Nginx server. Nginx is best preferred for serving static files but can also be employed to serve dynamic files. Like Apache has a very wide community and is used in many websites.

sudo apt install nginx

You will prompted to install, press Y  then the installation will continue as usual. Then Nginx will be active and running on your machine. If you have firewall active we will have to allow Nginx to run through the firewall.

To enable only port 80 run:

sudo ufw allow 'Nginx HTTP'

To enable only port 443 for SSL/TLS run:

sudo ufw allow 'Nginx HTTPS'

To enable both ports run:

sudo ufw allow 'Nginx Full'

Now if you run:

sudo ufw status

You will get(for Nginx port 80):

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Now if you go to your public IP you will be greeted with the default Nginx page.

Nginx default page

Install PHP7.4

To install PHP7.4 first we need to add the ondrej/php  apt repository which has PHP 7.4 package and other required PHP extensions for successful execution of your PHP scripts.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Once the apt repository is added we can install PHP.

sudo apt instal php7.4

You can confirm that PHP is installed by running the command:

php -v

You will get an output similar to:

Output
PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Then install the rest of the cocktail of extensions that may be required. The -y option is to answer yes to all the confirmation messages that will come up.

sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y

Configure PHP7.4

Now depending on the requirements of your application you can modify the settings from the php.ini file.

For Nginx this file is available at:

sudo nano /etc/php/7.4/fpm/php.ini

For Apache this file is available at:

sudo nano /etc/php/7.4/apache2/php.ini

Once you have modified the configuration you have to restart apache sudo service apache2 restart or php7.4-fpm sudo service php7.4-fpm restart to make sure the changes have been persisted.

Finally Install MySQL

Now that your server is up and running you need to install a database. The most popular in this category is MySQL database. It is most used with PHP applications hence its presence in the LAMP/LEMP stack.

Run the following command to install the server. When prompted press Y  followed by ENTER  to confirm the installation.

sudo apt install mysql-server

When the installation is completed successfully you have to secure the database with secure script that comes pre-installed with MySQL. Start the interactive script by running the following command. This will ask you to configure the VALIDATE PASSWORD PLUGIN .

sudo mysql_secure_installation

The script will raise several questions, answer Y  for yes to continue or anything else as per questions.

Now you will be able to login into your database using the credentials preset. But also you will be able to enter the password using sudo mysql or just mysql when logged in as a root user. This is because mysql_secure_installation is using unix_socket. You can configure it to use password, Learn How To.

Even though this might look like a security concern at first, it makes the database server more secure because the only users allowed to log in as the root MySQL user are the system users with sudo privileges connecting from the console or through an application running with the same privileges. In practical terms, that means you won’t be able to use the administrative database root user to connect from your PHP application. Setting a password for the root MySQL account works as a safeguard, in case the default authentication method is changed from unix_socket to password.  - Digital ocean tutorial

Conclusion

From this article you have a stable foundation upon which you can start learning how to deploy your PHP applications. The fundamental environmental setup has been done.

You can later learn how to setup SSL/TLS using Let's Encrypt certificates to secure your server connections.