Ubuntu: install a web server – NGINX / PHP / MariaDB

In this tutorial, we will see how to install and configure a server in Ubuntu with the following components:

  • Nginx
  • PHP
  • MariaDB

I’ll also explain how to put phpmyadmin to administer the MariaDB server and how to configure vhosts.

In this tutorial, we will not use the official repositories of the distribution, but the repositories of the various components in order to install the latest versions.


Have a freshly installed and up to date Ubuntu server with root or sudo access and Internet access.

Installation of the various components

In this part, we are going to install the various components of the Web server and check their correct functioning.

Install Nginx

Before installing it, we will add the Nginx office repository.

Enter the following command to create a list file where we will fill in the urls.

In the nano editor, enter the lines below which are the configuration of the repository:

Press the Crtl + X keyboard keys and save the changes.

You have to adapt the sources of the repository to the version of Ubuntu, in the tutorial I’m on version 18.04.X, so I’m using bionic.

Add the signatures of the deposit using the following commands:

Update packages with the command:

On the screenshot below, we can see that the nginx repository has been queried:

Install nginx with the following command:

The -y option allows you to not have a confirmation request for the installation

Nginx is now installed, we will now do some checks, configuration and see some commands before moving on to installing PHP.

Check the configuration of nginx:

This command is very useful, especially when making a configuration change, it allows you to check that it is correct before reloading it.

Start the nginx service:

If all goes well the command returns nothing

Configure the automatic start of the service:

sudo systemctl enable nginx

Check the nginx service:

systemctl status nginx

This command allows you to check the status of the service and since when it has been running.

Show version of nginx

Configure the nginx user

This configuration step is important, especially if you want to use php (php-fpm) on the web server.

When installing nginx from the official repository, the service is configured to run with the nginx user and php the www-data user. It is necessary to modify the nginx configuration to use the same user as php.

Edit the /etc/nginx/nginx.conf file:


Replace with :

Save the config file.

Reload the configuration:

Test the Nginx server

From a computer, open an Internet browser and enter the IP address or the name of the machine and verify that the Welcome to nginx! is displayed.

Install PHP

Now that the web server is in place, we will move on to installing PHP, as for nginx, we will add the ppa: ondrej / php repository which allows to have the latest versions of PHP and to have several versions simultaneously .

In this tutorial, we will install version 7.3 and 7.4 of PHP and we will see how subsequently to configure a vhost to use a specific version of PHP.

Add the repository

Enter the following commands:

Confirmation is required when executing the command: sudo add-apt-repository ppa: ondrej / php

Refresh the packages using the command:

View available PHP versions

Enter the command apt install php7. and do a tab.

You can see all the PHP packages available on the repository.

Install a version of PHP

For an installation on nginx, it is important not to enter the command sudo apt install php7.X because this command will install dependencies including apache2.

To use PHP with NGINX, you must install PHP in FPM mode.

To install PHP enter the following command (adapt according to the desired version):

Once the installation is complete enter the following command to test the correct operation:

PHP in FPM mode, works as a service, it is possible to check its status using the command below by adapting the version of php installed:

systemclt status php7.3-fpm

To install another version of PHP simultaneously, just repeat the operation by changing the command with the desired version.

Configure NGINX and PHP

Now we will configure nginx to use PHP and test that it works.

Edit the default vhost file:

Add before final} the following lines adapting to the version of PHP available:

Check the config using the command: sudo nginx -t

Reload the config: sudo systemctl reload nginx

To test how php works, we will create a phpinfo.php file:

Enter the PHP code below:

Save file.

From an Internet browser, call the page http: //ip_server/phpinfo.php

If the phpinfo.php page is all good.

Install MariaDB

Our Web server is almost complete, we now have to install MariaDB to have a database.

Add the repository

Once again, we need to add the repository in order to have the latest version. Go to the following url: Download MariaDB Server – MariaDB.org and select the distribution and the version of MariaDB in order to have the url of the sources.

Enter the following commands to add the repository:

Update the list of packages with the command:

MariaDB installation

As for PHP enter the command apt install mariadb- + tab to display the available packages.

For the example, we will install version 10.5, enter the command below to perform the installation:

Once the installation is complete, enter the following command to check the correct functioning of the MariaDB server:

Creation of an administration account

By default, the root account has no password and is only accessible by the console, to avoid modifying this account, we will create an administration account that we can use with another tool like phpmyadmin.

Enter the following command to switch to mysql mode

Enter the following lines (requests) to create an adminsql account, change the password before placing orders.


This step is optional, the phpmyadmin installation allows you to administer MariaDB.

To make my work easier, I used WinSCP for the transfer to the server in SSH.

Download the latest version of phpmyadmin.

Unzip the archive.

On the web server create a pma folder at the location of the vhost by default: / usr / share / nginx / html

Send the contents of the archive to the pma folder:

From a browser go to the address: http: //ip_server/pma/index.php

Enter the information of the account created previously (adminsql / password) and click on Execute to connect.

You can administer the MariaDB server with a web interface.

Configure vhosts

Now that the Web server is ready, we will see how to configure vhost also called virtual host which allows you to host several sites on the same server.

For the tutorial, I will create two vhost (site-a.lab.priv and site-b.lab.priv) which will both use a different version of php, site-a will be under php7.3 and site -b will be with php7.4

Beforehand I created the two DNS records and it points to the server

Before starting, you have to create the different folders, in the / home folder I will create a www-data folder and then in this folder I will create the sites, site-a and site-b subfolders which will each have two subfolders -log and public_html folders.

Now that the files are ready, we will move on to the configuration files. In order to have an operation similar to Apache, we will create two folders in the / etc / nginx folder.

  • sites-available : which will contain the virtual host config files
  • sites-enabled : which will contain symbolic links to the virtual host configuration files which will be active.

Create the folders with the following commands:

You must now modify the nginx configuration file to tell it to load the configuration files that will be in the site-enabled folder.

Search :

Add after:

Save the configuration file.

Test the nginx config:

Reload the configuration:

We go to the configuration of the virtual host for site-a, we will use the default file as a base, we will copy the default.conf file in the site-available folder by renaming it site-a.conf

You must now modify the file to make it correspond to the site configuration.

Here is the config file:

Changed lines:

Ligne 3 (server_name) : enter url
Ligne 6 : lLogs folder
Ligne 9 : site root directory
Ligne 10 : add index.php
Ligne 46 : site root directory

You must now activate the site with symbolic link:

Test the nginx config

Reload the config

To test the correct operation I added an index.php file in the site’s public_html folder with the following code:

From a browser enter the site url:

For site-b, I reproduced the same configuration by changing the server_name, locations and configuration of php to use php7.4.

the config file:

Here is the result :


The tutorial ends here, now you know how to install and configure a web server with Nginx / PHP / MariaDB with Ubuntu using different PHP versions and virtual hosts.

Other tutorials will come like installing WordPress and GLPI on an NGINX web server.

Related Posts

PHP on IIS: Installation and Configuration

Table Of ContentsPresentation PrerequisitesPHP installationInstalling PHP Manager for IISPHP configurationEnable a PHP extensionUse different versions of PHP depending on the sites Presentation In thi

Log root SSH on Ubuntu/ Debian

By default, the connection with the root account is not possible because it does not have a password and the SSH server is configured so as not to allow connection with the root account. It happens th