How to install Nginx and PHP on Debian 10

Linux dominates the field of servers that make up the Internet. Apache, Nginx, Lighttpd, OpenLiteSpeed are some of the web servers available for Linux. In this article, we are going to discuss Nginx and see how to install it along with PHP on Debian 10.

What is Nginx?

Nginx is an open-source web server for Windows, Linux, Unix, and Unix-alike systems. It is Apache’s main competitor. The main advantage of Nginx is high performance in high traffic. You can also use it as a reverse proxy, HTTP cache, load balancer, and proxy server for email (IMAP, POP3, and SMTP).

The efficient design of Nginx, makes it outperform other web servers in performance tests. It is a perfect fit and excels in situations where there is a high level of traffic or requests. That is the reason many giant companies use it for their websites or internal web applications.

Install Nginx on Debian

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. Login to your Debian system and run the below command in sequence to install Nginx.

1. Check if Nginx available in the Debian repository

As mentioned above, Nginx should be available in the Debian repository. Run the below command to check it.

:~$ 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. The version may be slightly older. But you should not worry as it is fairly stable and robust.

2. Install Nginx

We are going to use the apt command to install. This command takes care of all dependencies during installation.

:~$ sudo apt install nginx

Install Nginx on Debian 10

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

Use the below command to start and stop the Nginx service using systemctl  command

:~$ sudo systemctl stop nginx
:~$ sudo systemctl start nginx

3. Configure the firewall

You need to configure the firewall so that your server can accept the connection. Open the port 80 and 443 using the command given below

:~$ sudo ufw allow 'Nginx HTTP'

You can check the status using the command given below

:~$ sudo ufw status

4. Test your server

Open the web browser and type http://IP-ADDRESS or http://domain-name to find out if everything went well

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

Install PHP on Debian

PHP is a widely used open-source scripting language. It is also available in Debian 10 default repository. So you do not have to add any third-party PPA.

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

Now, both Nginx and PHP are properly installed, you need to configure Nginx to be able to interpret PHP files. Let’s see how to do that.

Adding PHP support to Nginx

Go to /var/www/html/ directory where files and website are processed in Debian and change the owner, 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 websites 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.

Test PHP with Nginx

Now, to test that PHP is interpreted by Nginx, let’s 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?

Scroll to Top