Save up to 81% on your first year of web hosting!

How to Install Nginx and PHP 7.4 on Debian 10

So PHP 7.4 has now been released for quite a while for Debian 10 buster. 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 7.4.

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 7.4

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 php7.4-fpm php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip php7.4-soap php7.4-imap

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

sudo nano /etc/php/7.4/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

Go into your default.conf file

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

Replace your existing configuration file with the one below

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/php7.4-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.

Don’t forget to try out our VPS too! Our servers are located in stockholm, so if you’re from Sweden feel free to check out our Swedish site too, and grab yourself a Stockholm VPS.