-3

I know nothing about Nginx. Could you please provide me with some guidance that I can copy and modify slightly to ensure everything works correctly?

Additionally, if someone could explain how to host non-static (PHP) websites on Nginx, I would greatly appreciate it.

Thank you!

  • Whilst you happen to have received an answer from an extremely generous person in this instance, this question shows no effort whatsoever on your part before asking for the free time of others, and is too broad (two questions at once, for one thing), and as such is really not in the community / voluntary spirit of Stackoverflow at all. You may not receive a favourable response to such low-quality posts in future. We are not a tutorial service, in general. Take the [tour] and read [ask] (and linked articles) to learn how to do it properly, and get the best out of the site. Thanks. – ADyson Aug 01 '23 at 23:37

1 Answers1

1

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)

RodneyH
  • 11
  • 2