Save 50% for your first year of web hosting!

How to Install Nginx and PHP 8 on Debian 10

PHP 8 is the latest PHP version released. This tutorial will show you how you can install Nginx mainline version on Debian 10 for optimal performance as well as the newest version of PHP, currently 8.0.

Step 1: Install Nginx

Firstly we need to install the prerequisites.

sudo apt install curl gnupg2 ca-certificates lsb-release

Then we need to add the Nginx mainline package to our repository so that when we run apt install nginx, we will download the mainline version instead of the old stable version.

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

Optionally, if you would rather want to use the older and slower stable version of Nginx you can do so by running: (Remember that you should only run one of the following)

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

Next we need to download the signing key so that we can verify its authenticity

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

If it prints out OK! then you are good to go!

Now that we have downloaded & verified its authnicity lets install it!

sudo apt update
sudo apt install nginx

That’s it! You have now installed the latest release of Nginx on Debian 10. You should now start it!

sudo systemctl start nginx.service

Don’t forget to make it automaticly start on system boot as well.

sudo systemctl enable nginx.service

Visit your server’s IP address in your webbrowser. You should now see something along these lines if it is working correctly.

welcome to nginx, default index.html
Default welcome screen on Nginx

Step 2: Install PHP 8

To add the repositories for PHP:

sudo apt-get install software-properties-common

Since PHP 7.4 didn’t come with Debian 10 it is required to add the following repository

sudo add-apt-repository ppa:ondrej/php

Run this to download your added repositories:

sudo apt update

We can now install PHP now that we have all of the required repositories.

sudo apt install 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

It is recommended to raise the memory limit to improve the overall performance. Your PHP configuration is located in/etc/php/8.9/fpm/php.ini.

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

Press CTRL + W and search for memory_limit.

Replace it with memory_limit = 256

Save & exit by pressing CTRL + X followed by Y

Step 3: Configure Nginx

Add nginx to www-data group

sudo usermod -a -G www-data nginx

Change owner of directory to www-data

sudo chown -R www-data /usr/share/nginx/html

Next up we need to replace our existing default.conf file for Nginx. To do this easily run the following commands:

sudo rm /etc/nginx/conf.d/default.conf
sudo touch /etc/nginx/conf.d/default.conf

Go into your default.conf file

sudo nano /etc/nginx/conf.d/default.conf

Paste the following in the configuration file

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;

   location / {
    if ($request_uri ~ ^/(.*)\.html$) {
        return 302 /$1;
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass   unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

Save & exit by pressing CTRL + X followed by Y

Now restart your Nginx:

 service nginx restart

That’s it! However, you may now test to see if php works

nano /usr/share/nginx/html/phpinfo.php

Add the following lines

<?php

phpinfo();

?>

Save & exit by pressing CTRL + X followed by Y

Now go to YOURSERVERIP/phpinfo.php in your web browser. You should see something along these lines

phpinfo example page that shows that PHP is installed

That’s it! Now you know how to install Nginx and PHP 7.4 on Debian 10.