How can PHP files be specified in nginx?
To specify PHP files in nginx, you need to make corresponding settings in the nginx configuration file. Here is a simple example:
- Open the configuration file of nginx, typically located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default.
- Locate the server block in the configuration file, usually the section that starts with server { }.
- Add the following content in the server block to specify how PHP files should be handled:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
The above configuration will route all requests ending in .php to the PHP interpreter for processing.
- Restart the nginx service using sudo systemctl reload in the terminal.
Now, you can create PHP files in a specified directory and access them in a browser, where nginx will pass the request to the PHP interpreter for processing.