How to solve nginx without parsing php files?

10-30-2023

PHP is not installed or configured.

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

php -v

This will show 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 lines:

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 need to specify the PHP file processing location and other parameters of nginx. Please confirm that the code block has been added to your nginx configuration file, and the path of the sock file matches your PHP configuration file.

Index.php file is not set.

If the homepage of your Web application is index.php, but it will not be automatically processed in nginx, then you need to add index.php in the index directive of nginx configuration file, as shown below:

index index.php index.html ;

Now, when you open the home page, nginx will automatically find index.php and handle it correctly.

PHP file permissions

Another main reason why nginx can't parse PHP files is that the permissions are incorrect. Ensure the following:

The permission of PHP file is 644.

The permissions of the directory where PHP files are located are 755.

You also need to ensure that nginx users have ownership of all PHP files, and the ownership of the directory where PHP files are located is also set to 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 nginx users and groups.

PHP module is not enabled.

If your nginx cannot parse the PHP file and does not display any error messages, please make sure that the PHP module is enabled. To enable it, edit nginx's compilation options and 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_modu le --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_s ecure_link_module --with-http_auth_request_module --with-http_image_filter_module --with-http_geoip_module --with-http_degradation_module --with-http_x slt_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 add-add-module =/path/to/PHP-src/SAPI/nginx/to enable the PHP module.

PHP error record

If nginx cannot parse the PHP file, but no error message is displayed, you can find more information about the error in the PHP error log. Open the php.ini file and uncomment the following lines to enable PHP error logging.

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

We specify /var/log/php/error.log as the PHP error log, and turn on the error recording function. Please make sure that the folder has been created and has appropriate permissions.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us