To set up Nginx to serve a PHP-based website on a subdomain. A basic example to get you started, assuming you want to create a subdomain called "subdomain.example.com" and host a PHP website on it.
Step 1:
Assuming you have your PHP files in the directory /var/www/subdomain.
Note to replace /var/www/subdomain with the actual path to your PHP website files and adjust the fastcgi_pass directive in the
configuration to match your PHP-FPM version and configuration.
Step 2:
Configure Nginx for the Subdomain, you need to create a new Nginx configuration file for the subdomain. Typically, Nginx stores its configuration files in the /etc/nginx/sites-available/ dir.
Create a new configuration file, for example, /etc/nginx/sites-available/subdomain.example.com
server {
listen 80;
server_name subdomain.example.com;
root /var/www/subdomain;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; # Adjust this path to your PHP-FPM version and configuration
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
Step 3:
Enable Subdomain Configuration to do this create a symbolic link to enable the subdomain configuration:
sudo ln -s /etc/nginx/sites-available/subdomain.example.com /etc/nginx/sites-enabled/
Step 4:
Before applying the changes test the Nginx configuration, it's essential to check if the Nginx configuration is valid:
sudo nginx -t
If all is good it will says "syntax is okay" and "test is successful," you can proceed.
Step 5:
To apply the changes, reload Nginx:
sudo systemctl reload nginx
Step 6:
Update DNS settings for your subdomain (subdomain.example.com) to point to your server's IP address.
You may need to consider additional security and performance measures. It's also recommended to use HTTPS with a valid SSL certificate to secure your website's
For further information check DigitalOcean's PHP Websites Securely With Nginx And Php-fpm or Setup PHP on Nginx with fastCGI (PHP-FPM)