How to server PHP  sites from Nginx

How to server PHP sites from Nginx

By default, Nginx doesn’t know how to serve PHP files. we need a way to do.

Create a Nginx site

Create a bash script from the below content into the terminal and make sure to give it executable permission with chmod +x filename.sh , when you run this script provide the name of the site for which you want to configure PHP

#!/bin/bash

# Ask for the site name
read -p "Enter the site name (e.g., sitename.com): " site_name

# Create the directory and index.html file
sudo mkdir -p /var/www/$site_name

sudo bash -c "cat > /var/www/$site_name/index.html" <<EOF
<html>
<head>
    <title>Welcome to '$site_name'!</title>
</head>
<body>
    <h1 style="color: green;">The '$site_name' is working!</h1>
</body>
</html>
EOF


# Create Nginx virtual host configuration
sudo bash -c "cat > /etc/nginx/sites-available/$site_name" <<EOF
server {
    listen 80;
    listen [::]:80;

    root /var/www/$site_name;
    index index.html index.htm;

    server_name $site_name www.$site_name;

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

# Create symbolic link to enable the site
sudo ln -s /etc/nginx/sites-available/$site_name /etc/nginx/sites-enabled/

# Reload Nginx to apply changes
sudo systemctl reload nginx

echo "Site $site_name has been created and configured."

Add Php info file to the site

echo "<?php phpinfo(); ?>" | sudo tee /var/www/sitename.com/info.php

now access the sitename.com/info.php , you will notice instead of displaying content it is downloading the file because by default, Nginx doesn’t know how to serve PHP files. we need a way to do.

To handle the PHP scripts, we need PHP module, such as PHP-FPM. PHP-FPM operates independently of the NGINX environment by establishing its separate processes.

when a user makes a request for a PHP page, the NGINX server delegates the request to the PHP-FPM service through FastCGI. The installation process of PHP-FPM on Ubuntu depends upon the version of PHP in your Ubuntu server. we will be installing PHP 7.4 version and php7.4-fpm, adjust the below command according to your

# Install essential packages for adding repositories and managing certificates
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https

# Set the LC_ALL environment variable to ensure proper locale settings
LC_ALL=C.UTF-8

# Add the PHP repository maintained by Ondřej Surý
sudo add-apt-repository ppa:ondrej/php

# Update the package list to include the new repository
# and install PHP version 7.4 along with the PHP-FPM package
sudo apt update
sudo apt install php7.4 php7.4-fpm

Edit Site configuration

We created the configuration file with bash script which basically creates a site configuration file at /etc/nginx/sites-available/sitename.com

we need to add this block to the configuration file and please make sure the PHP version.

  # pass any file with php to php-fpm
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
  }

Your nginx configuration of sitename.com will look like this

access the php.info file at the browser sitename.com/info.php

Next we will see how we can control the cache of static content with nginx ,

Did you find this article valuable?

Support Muhammad Usama by becoming a sponsor. Any amount is appreciated!