Home >Operation and Maintenance >Nginx >How to solve nginx not parsing php files

How to solve nginx not parsing php files

PHPz
PHPzforward
2023-05-21 10:46:461695browse

  1. PHP is not installed or not configured

First, please make sure that your server has PHP installed and configured correctly nginx to make it work properly. To check if PHP has been installed correctly, open a terminal and run the following command:

php -v

This will display the version of PHP currently installed on your server. If the PHP version is not displayed, consider installing PHP.

To ensure that PHP works with nginx, edit the nginx configuration file and add the following line:

location ~ \.php$ {
  fastcgi_pass  unix:/run/php/php7.4-fpm.sock;
  fastcgi_index index.php;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Here, we want to specify nginx’s PHP file processing location and other parameters. Please confirm that the code block has been added to your nginx configuration file and that the path to the sock file matches your PHP configuration file.

  1. index.php file not set

If your web application's home page is index.php, but it won't be in nginx Automatically processed, then you need to add index.php in the "index" directive of the nginx configuration file as follows:

index index.php index.html;

Now, when you open the homepage, nginx will automatically find index.php and process it correctly it.

  1. PHP file permissions

Another main reason why nginx cannot parse PHP files is incorrect permissions. Make sure the following:

  • The permissions of the PHP file are 644

  • The permissions of the directory where the PHP file is located are 755

Also make sure that the nginx user has ownership of all PHP files, and the ownership of the directory where the PHP files are located is also set to the nginx group. This can be achieved by using the following command:

sudo chown -R nginx:nginx /var/www/html/

Here we assign the ownership of the /var/www/html/ directory to the nginx user and group.

  1. PHP module is not enabled

If your nginx cannot parse the PHP file and does not display any error message, please make sure that the PHP module has been enabled . To enable it, edit nginx's compilation options, add the following line:

--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_degradation_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_spdy_module \
--with-http_auth_request_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-ipv6 \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-threads \
--with-debug \
--add-module=/path/to/php-src/sapi/nginx/

Here, we added --add-module=/path/to/php-src/sapi/nginx/ to enable PHP module.

  1. PHP Error Log

If nginx cannot parse the PHP file but does not display any error message, you can look for information in the PHP error log More information about the error. Open the php.ini file and uncomment the following lines to enable PHP error logging

error_log = /var/log/php/error.log
log_errors = On

We specify /var/log/php/error.log as the PHP error log and enable the error logging function. Please make sure the folder has been created and has the appropriate permissions.

The above is the detailed content of How to solve nginx not parsing php files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete