sajad torkamani

Assuming you’ve setup Nginx and PHP-FPM, you’ll want to configure Nginx to use PHP-FPM to process PHP files.

Create sample PHP app

Create root web directory.

sudo mkdir /var/www/example-website

Create an info.php file.

vim /var/www/example-website/info.php

Paste the following code:

<?php
phpinfo();

Assign ownership to a non-root user who will be responsible for most sysadmin tasks.

sudo chown -R $USER:$USER /var/www/example-website

Create Nginx configuration

sudo vim /etc/nginx/sites-available/example-website

Add the following:

server {
    listen 80;
    # Instead of example-domain, you'll want to use a domain that you own (e.g., mycoolwebsite.com)
    server_name example-domain www.example-domain;
    root /var/www/example-website;

    index index.html index.htm index.php;

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

   location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Change as needed
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
   }

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

Activate this config by creating a symlink from the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example-website /etc/nginx/sites-enabled/

Test configuration for syntax errors:

sudo nginx -t

Activate new configuration:

sudo systemctl reload nginx

Verify configuration

Assuming you’ve setup your DNS records properly, navigate to http://server_domain_or_IP/info.php and you should see information about your PHP installation.

Tagged: Nginx

Leave a comment

Your email address will not be published. Required fields are marked *