HostUp
Image of how to install Nginx & php on debian 10

How to Install Nginx and PHP 7.3 on Debian 10

Just recently on July 6th, 2019, Debian 10 buster released, and with it came a lot of features. This tutorial will show you how you can install Nginx mainline version on Debian 10 for optimal performance.

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.3

To add the repositories for PHP:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php -y

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.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

It is recommended to raise the memory limit and disable cgi.fix_pathinfo. Your PHP configuration is located in/etc/php/7.3/cli/php.ini.

sudo nano /etc/php/7.3/cli/php.ini

Press CTRL + W and search for memory_limit.

Replace it with memory_limit = 256

Press CTRL + W and search for ;cgi.fix_pathinfo=1

Replace it with cgi.fix_pathinfo=0

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.3-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

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

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

David

Want Ubuntu tutorials that actually work? Then you have found the right person

Leave a Reply

avatar
  Subscribe  
Notify of