Home > Article > Operation and Maintenance > How to disable PHP execution in Nginx
1. Edit the Nginx configuration file
You need to edit the Nginx configuration file to disable the execution of PHP scripts. Try entering the following command in the terminal, if you don’t know where the Nginx configuration file is
$ locate nginx.conf
Depending on your operating system, the Nginx configuration file may be located in different locations.
Edit Nginx's configuration file and find a line similar to the following:
location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
This block defines how Nginx handles PHP scripts. Therefore, we need to disable this block to disable Nginx from executing PHP scripts. You can comment out the entire block like this:
#location ~ \.php$ { # try_files $uri =404; # fastcgi_pass unix:/var/run/php5-fpm.sock; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; #}
Save changes and exit the editor.
2. Reload Nginx
Now you need to reload Nginx for the changes to take effect. You can use the command from your system's init script like this:
$ sudo service nginx reload
This will reload Nginx and apply the new configuration file to the server.
3. Test prohibiting PHP execution
Now, you can test whether prohibiting PHP scripts takes effect. To do this, you can try to access a PHP script on your web server, for example:
http://your-server.com/test.php
If everything is working fine, you should see a 404 error page telling you that the page does not exist.
This completes the task of prohibiting Nginx from executing PHP scripts.
The above is the detailed content of How to disable PHP execution in Nginx. For more information, please follow other related articles on the PHP Chinese website!