How to setup Linux, Nginx & PHP 7.4/8.0 (LEMP) on Ubuntu 20.04

Created by Christian van Os at 16-04-2021 10:17:10 +0200

The LEMP stack is a combination of several open-source software packages. In this guide, we will go through all the steps of installing these packages, namely: Linux, Nginx, MySQL and PHP. Together they enable your server to host dynamic web applications or host databases. In this tutorial we'll be going through the installation process step by step.

Before we start, we need a Linux Ubuntu installation. We'll skip that step in this tutorial since you can easily get a default linux installation on one of the containers on the my.cloudcontainers.net site.

This tutorial is based on the cloudcontainers created on the my.cloudcontainers.net page. This means that you are by default the root user, so all of the commands below don't make use of sudo. If however, you're not the root user on your system, you'll have to run "sudo -i" at the start of this tutorial, so that you become the root user.

Install Nginx

Since we're using a clean Virtual Machine to install Nginx on, we are going to want to update our packages to prevent conflicts from emerging later on.

apt-get update;

apt-get upgrade -y;

apt-get dist-upgrade -y;

Now we can install nginx, and we also install some standard usefull software.

apt-get install software-properties-common nginx -y;

To test if Nginx was installed correctly, navigate to your server's domain/IP address in your browser.

If Nginx was successfully installed, you should see this page:


Install MySQL

We use the package manager to install MySQL:

apt-get install mysql-server -y

You'll now be prompted for an administrative password to the MySQL root user, fill in a password and press OK.


Next, we're going to run a security script to modify some default settings:

mysql_secure_installation

You'll be prompted the MySQL root password.

Next, you'll be asked for a couple configurations. You can answer yes to all of them, however, the VALIDATE PASSWORD PLUGIN could be considered annoying since it will return errors if your password is too weak. This is especially problematic when using software that automatically generate MySQL user credentials. You can safely leave the VALIDATE PASSWORD PLUGIN disabled, but know it's important to choose strong passwords.


MySQL is now installed.


Install and configure PHP

Since Nginx does not by default contain PHP processing, In addition to PHP we're also going to install a plugin that makes us able to use multiple php versions on the same virtual machine. For this, use the following commands:

add-apt-repository ppa:ondrej/php;

apt-get update

apt-get install -y php7.4 php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl

apt-get install -y php8.0 php8.0-fpm php8.0-mysql php8.0-mbstring php8.0-xml php8.0-gd php8.0-curl

Now we have installed the PHP processor and Nginx, we still have to configure Nginx to actually use it. To do this, open the following file:

vim /etc/nginx/sites-available/site1.example.com.conf

vim /etc/nginx/sites-available/site2.example.com.conf

Now that we made our nginx config, we want to enable it. This can be done by running this commands:

ln -s /etc/nginx/sites-available/site1.example.com.conf /etc/nginx/sites-enabled/

ln -s /etc/nginx/sites-available/site2.example.com.conf /etc/nginx/sites-enabled/


To test if the setup is working correctly, we make a php file that returns all the info about php, and the server.

This will be done by running these commands:

mkdir -p /var/www/site1.example.com/ && echo"" > /var/www/site1.example.com/index.php

mkdir -p /var/www/site2.example.com/ &&echo "" > /var/www/site2.example.com/index.php


You can test your configuration for errors by typing the following command:

nginx -t

If there are no errors, we can reload Nginx to finalize the changes in our configuration:

systemctl enable nginx && systemctl restart nginx;

  

Now check the content of the website on site1.example.com and site2.example.com, if you see php7.4 on site1.example.com and php8.0 site2.example.com then it went OK.


For now, we'll empty these index.php file; because it could give away information concerning our server we don't want unauthorized users to have acces to.

echo "" > /var/www/site1.example.com/index.php

echo "" > /var/www/site2.example.com/index.php


Congratulations, you've now installed and tested LEMP on Ubuntu 16.04!


Comments

Comments are turned off.