Step 1: Install the Nginx Web Server



NGINX is a high performance webserver and also the second most used webserver software in the world. The usage of NGINX is recommended by vpsrv.com due to the fact, that websites served over NGINX are simply faster, more reliable and also don't need additional config to have a basic level of security.



So first we will update our repositories:



sudo apt update

Then we install NGINX:

sudo apt install nginx

NGINX will start on boot if you install it over the standard Ubuntu repositories (like you did above).

To verify if NGINX installed successfully, simply open the IP Address of your Server in a Browser and you should see a simple HTML page saying:



Welcome to nginx!

Step 2: Install MariaDB



MariaDB is the leading enterprise open source database with features previously only available in costly proprietary databases



You can install MariaDB only if MySQL is installed already. So let's install mysql:



sudo apt install mysql-server

Once asked, enter the password you want your root user to have for MySQL connections. After the installation, you will still need to do some configuration, but this is really simple and takes no more than two minutes:

mysql_secure_installation

First the script will ask you for your MySQL root password. Enter the password you used to during the installation of mysql-server. We recommend to also delete anonymous users (the script will ask you), reload privilege tables and also to disable mysql login remotely if you don't explicitly need it.



Then we can start with the installation of MariaDB:



sudo apt install mariadb-server

That's it basicly. MariaDB is now installed and uses the configuration you made for MySQL.

Step 3: Install PHP for Processing



Now we will install php-fpm and php-mysql. Those two packages are needed to process PHP over NGINX and to have connections over PHP to your MariaDB/MySQL-Server.

sudo apt install php-fpm php-mysql

Configure PHP for use with NGINX

Before we start enabling PHP for NGINX, we will first change a setting in the PHP config file in order to secure our installation.

Open the php-fpm configuration file with root privileges:

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





Now we look for cgi.fix_pathinfo (you can search in nano by pressing ctrl + w) and comment it out (we remove the semi-colon) and set the value from 1 to 0. cgi.fix_pathinfo does the following:

It tells your PHP processor to execute the closest PHP file it can locate if the PHP file requested by the visitor cannot be found. This could cause unwanted behaviour in your application.

After that, we enable our configuration by entering:



service php7.0-fpm restart

Now to enable PHP processing for NGINX we open and edit the default configuration for our site, which can be found in:

sudo nano /etc/nginx/sites-available/default

The default server block found in this file should look like the following:

/etc/nginx/sites-available/default

server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
try_files $uri $uri/ =404;
}
}



We need to make some changes to this file for our site.

  • We change the index directive to include index.php. We recommend the following modification:

index index.html index.htm index.nginx-debian.html;

to

index index.php index.html index.htm index.nginx-debian.html;

 

  • We change the server_name directive to our domain or IP Address.

  • We enable PHP processing by uncomenting some lines in the location ~ \.php$ block to match the following:

 

 

location ~ \.php$ {

include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

 

  • You may also uncomment the block that disables access to .htaccess, as those are only made to work with apache:

 

location ~ /\.ht {
deny all;
}

 

Now you can close and save the file. Do so by pressing ctrl + x and then pressing y and enter.

 

Everytime you do changes to any NGINX config file, you should run a configuration test by entering:

 

sudo nginx -t

Once that is done and no errors are found/left, you should reload the NGINX webserver. We do not recommend to restart the NGINX webserver in production environment.

service nginx reload

Your setup should now be done and you can test it by putting accessing a phpinfo file on your IP/Domain. Create one by doing the following:



  • nano /var/www/html/info.php

  • Add the following code to it:

<?php

phpinfo();

?>

  • Save and close the file by pressing ctrl + x and y and then enter.

  • Then access your IP/Domain over a browser and add /info.php to it.

You should now see a PHP Informational page, telling you about your configuration and several variables. You should then remove access to this file, as it could disclose sensitive information.



Do so by entering the following command:



rm /var/www/html/info.php

Was this answer helpful? 2403 Users Found This Useful (9696 Votes)