In this guide, we will go through the steps to install Nginx and PHP 8.0 on the Debian 10 operating system. We will also cover how to install PHP for Nginx.

 

What is Nginx?

Nginx is a web and proxy server that essentially enables the functionality of websites. Nginx is highly versatile as a web server and can handle large amounts of website visitors very effectively. Nginx is a good alternative to the Apache web server.

 

What is PHP?

PHP, which stands for Hypertext Preprocessor, is a backend language that enables the development of complex web applications. PHP is easy to learn and relatively simple to install. Currently, the majority of web applications are built with PHP. For example, even this website runs on PHP. However, in modern times, PHP is starting to become somewhat of an older language, even though it is still being updated. PHP is being replaced by JavaScript-based backend applications such as Node.js.

 

Nginxin installation.

To get our website and PHP working, we need a web server. Let's start by installing all the necessary applications, starting with what Nginx requires. If you are not using the root user, remember to prepend "sudo" at the beginning of each command.

$ sudo apt-get install curl gnupg2 ca-certificates lsb-release 

$ sudo apt-get update

 

After this we will install Nginx

$ sudo apt-get install nginx

If it fails try this:

You may need to add the necessary sources depending on the applications required by Nginx.

To add the Nginx mainline repository to the apt source list located at /etc/apt/sources.list

$ echo "deb http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

We still need the key for this repository.

$ curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

Then try to update and install Nginx

$ sudo apt-get update

$ sudo apt-get install nginx

If you have installed it correctly, you should see the following page when you visit your server's address online.

If you don't need PHP for your website, you can proceed with regular usage.

You can find your website files in the following location: /var/www/html

Replace the existing index.nginx-debian.html file with your own index.html file at that location.

PHP 8.0 installation

Please note that in each command, we are using the PHP 8.0 version. If you want to install a different version like PHP 7.3, replace "php8.0" with "php7.3".

First, we add the PHP GPG keys and repositories.

$ sudo apt-get install wget apt-transport-https

$ wget https://packages.sury.org/php/apt.gpg

$ sudo apt-key add apt.gpg

$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

$ sudo apt-get update

Afterwards, install all the necessary PHP extensions and PHP itself:

$ sudo apt-get install php8.0 php8.0-fpm php8.0-common php8.0-mysql php8.0-gmp php8.0-curl php8.0-intl php8.0-mbstring php8.0-xmlrpc php8.0-gd php8.0-xml php8.0-cli php8.0-zip php8.0-soap php8.0-imap

How to configure php8.0 for nginx

Please note that in each command, we are using the PHP 8.0 version. If you want to install a different version like PHP 7.3, replace "php8.0" with "php7.3".

 

Now that we have installed Nginx and PHP, we can move on to the next step.

The Nginx website configurations can be found in the /etc/nginx/site-avaible/ directory, and the enabled configurations are symlinked in the /etc/nginx/sites-enabled/ directory. Typically, there is one file in these directories named default. You can edit this same file.

Add a new location definition above the existing location:

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

Once you have added this, you can include index.php in the index field.

After adding both sections, your default file should look like this (without comments):

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

        root /var/www/html;

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

        server_name _;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php/php8.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

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

After saving the file, test for any configuration errors in Nginx by using the command nginx -t. If there are any errors, correct them, and then restart Nginx using the command service nginx restart or systemctl restart nginx.

To test if PHP is working, you can replace the index.nginx-debian.html file with your own index.php file that looks like this:

<?php

To print PHP information on the webpage:
phpinfo();

?>

If PHP is functioning correctly, the webpage should display PHP information.

Was this answer helpful? 5 Users Found This Useful (9 Votes)

Powered by WHMCompleteSolution