How to install Nginx and PHP on Debian 10

Linux dominates the field of servers that make up the Internet. And only they, but even organizations at all levels, use Linux to enable servers to provide services within the network. Specifically, in Linux, there are many web servers, and if we talked about Apache before, now it is time to install Nginx and PHP on Debian 10.

What is Nginx?

Nginx is an open-source web server for Unix systems. It is Apache’s main competitor and has the main advantage of high performance in high traffic.

It is both the success and flexibility that Nginx offers, and is also used as a reverse proxy, HTTP cache, and load balancer.

Nginx was designed to be very efficient, so it often outperforms other web servers in performance tests. Many of them in situations where there is a high level of traffic or requests.

With this in mind, you can understand why many giant companies use it for their websites or internal web applications.

Nginx vs Apache webserver

First of all, both are two web-server very good and efficient. However, in terms of ease of use and number of modules available Apache is superior.

On the other hand, in performance Nginx has no comparison and this is the main reason why almost half of the sites on the Internet use it. Especially those that are expected to be in high demand

So, although both do their job very well, if you plan to have a web application that will be used by many users, it is a good idea to use Nginx.

Also, both can be configured to work with PHP, and that is precisely what we are going to do today.

Install Nginx and PHP on Debian 10

Now it is time for work.

Usually, there is only one web server in a system. This is not mandatory but it is common. In this guide, we will assume that the server is ready and has not suffered many changes. This means that there is no webserver running on it.

Since Nginx is so popular, it is not surprising that it is included in the official Debian 10 repositories. Since it is such a critical application, it is convenient to sacrifice a little bit of novelty for stability.

To check if Nginx is available, just run the following command:

:~$ sudo apt list nginx

You will get a screen output similar to this one:

List the Nginx package

That is, we will install version 1.14.2 which is a fairly stable and robust version.

To install it, just run the following command:

:~$ sudo apt install nginx

Install Nginx on Debian 10

Enter your password and the installation will begin. In the end, the service will be active and enabled to start with the system.

If you want to stop it just use the command systemctl.

:~$ sudo systemctl stop nginx

In case you start it:

:~$ sudo systemctl start nginx

So you can handle Nginx as a systemctl service.

If you use a firewall such as ufw, then make sure to open ports 80 and 443. Or add the service to the firewall as follows:

:~$ sudo ufw allow 'Nginx HTTP'

To find out if everything went well, open your web browser and go to your server.

http://IP-ADDRESS or http://domain-name

You should see an image like this that indicates that Nginx is working properly.

Now you have to install PHP. This is also in the Debian 10 repositories. To install PHP and some of its main modules just run the following command:

:~$ sudo apt install php7.3-fpm php7.3-common php7.3-mysql php7.3-gmp php7.3-curl php7.3-intl php7.3-mbstring php7.3-xmlrpc php7.3-gd php7.3-xml php7.3-cli php7.3-zip php7.3-soap php7.3-imap

Getting PHP from the terminal

Adding PHP support to Nginx

Although at this point both Nginx and PHP are properly installed, you need to configure Nginx to be able to interpret PHP files.

First, in Debian, the root directory where files and websites will be processed is /var/www/html/. Therefore, you have to change the owner and permissions to this directory.

:~$ sudo chmod 755 -R /var/www/html/
:~$ sudo chown www-data:www-data -R /var/www/html/

This will save you problems with running web sites and applications.

Next, you need to make some changes to the default Nginx configuration file. This file configures the pages and websites that are in the default root directory. So if you add a virtual host you have to make a new configuration file.

So, edit it.

:~$ sudo nano /etc/nginx/sites-available/default

Find the location section, the file is not very large so you will find it quickly. So, let it be like this:

location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

Configuring Nginx to support PHP

Save the changes and close the editor.

Now, to test that PHP is interpreted by Nginx, you have to create a new file containing some PHP code.

So, in the Nginx root directory, create a new one with the phpinfo method.

:~$ sudo nano /var/www/html/test.php
<?php
phpinfo();
?>

Test the PHP support

Likewise, save the changes and close the file.

For all changes in Nginx to take effect, the service must be restarted.

:~$ sudo systemctl restart nginx

And you can check the service status with the following command:

:~$ sudo systemctl status nginx

Nginx service status after the changes

Now, open your web browser again and open the file:

http://IP-ADDRESS/test.php or http://domain-name/test.php

Nginx and PHP working on Debian 10

So, Nginx and PHP have correctly installed on Debian 10 Everything went well.

Conclusion

With Nginx, we can conclude that it is a very efficient web server and above all willing to give the maximum possible performance in a lot of traffic. Also, to make it work with PHP, it requires a little more configuration than Apache, but it is not complicated either and it’s worth it.

On the other hand, Nginx is a pretty popular program with a lot of documentation to read and study.

Now it is your turn, do you like Nginx? or do you prefer Apache?

Share This: