Installation guides

Installing PHP with Nginx on Debian

Step-by-Step Guide

  1. Update the Package Index
    Open a terminal and update the package index to ensure you have the latest information about available packages:
   sudo apt update
  1. Install PHP
    Install PHP along with some common PHP modules using the apt package manager:
   sudo apt install php-fpm php-mysql

This command installs PHP-FPM (FastCGI Process Manager) and the PHP MySQL extension.

  1. Verify PHP Installation
    Check the PHP version to ensure it is installed correctly:
   php -v

You should see the PHP version information displayed.

  1. Install Nginx
    Install Nginx using the apt package manager:
   sudo apt install nginx
  1. Configure PHP with Nginx
    Open the Nginx default server block configuration file for editing:
   sudo nano /etc/nginx/sites-available/default

Modify the server block to use PHP. Replace the existing content with the following configuration:

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

       root /var/www/html;
       index index.php index.html index.htm index.nginx-debian.html;

       server_name _;

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

       location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
       }

       location ~ /\.ht {
           deny all;
       }
   }

Save and close the file. Note that the fastcgi_pass directive should match the PHP-FPM socket file for your PHP version. The example uses php7.4-fpm.sock for PHP 7.4.

  1. Restart Nginx and PHP-FPM
    Restart Nginx and PHP-FPM to apply the changes:
   sudo systemctl restart nginx
   sudo systemctl restart php7.4-fpm
  1. Test PHP with Nginx
    To test PHP with Nginx, create a new PHP file in the web root directory:
   sudo nano /var/www/html/info.php

Add the following PHP code to the file:

   <?php
   phpinfo();
   ?>

Save and close the file.

  1. Access the PHP Info Page
    Open your web browser and visit http://your_server_ip/info.php. You should see the PHP information page, indicating that PHP is working correctly with Nginx.
  2. Install Additional PHP Modules (Optional)
    You can install additional PHP modules as needed. To search for available PHP modules, use:
   sudo apt-cache search php-

To install a specific PHP module, use:

   sudo apt install php-module_name

Replace module_name with the name of the module you wish to install.

  1. Configure PHP Settings (Optional)
    You can configure PHP settings by editing the php.ini file. The location of the php.ini file may vary depending on your PHP version. For PHP 7.4, the file is typically located at /etc/php/7.4/fpm/php.ini: sudo nano /etc/php/7.4/fpm/php.ini After making changes, restart PHP-FPM to apply them: sudo systemctl restart php7.4-fpm
  2. Remove the PHP Info Page
    For security reasons, it’s a good idea to remove the PHP info page after testing:
    bash sudo rm /var/www/html/info.php
Other Recent Posts