Home >Backend Development >PHP Problem >What to do if nginx does not parse php files
nginx is a high-performance web server that is often used with PHP to render dynamic content in web applications. However, sometimes nginx may not parse the PHP file correctly, causing the application to fail to run properly. In this article, we will explore some common issues that cause nginx to fail to parse PHP files and provide solutions.
First, make sure PHP is installed on your server and nginx is properly configured to work. 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 specify the location and other parameters that nginx will use to process the PHP files. Please make sure this snippet is in your nginx configuration file and the sock file in it matches your PHP configuration file.
If your web application's homepage is index.php, but it doesn't get automatically handled in nginx, 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 look for index.php and handle it correctly.
Another main reason why nginx cannot parse PHP files is incorrect permissions. Ensure the following:
Also, please ensure the ownership of the PHP file Set to nginx user, and the ownership of the directory where the PHP file is located is 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 the nginx user and group.
If nginx cannot parse the PHP file and there is no error message, make sure you have the PHP module 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.
If nginx cannot parse the PHP file but does not display any error message, you can find more information about the error in the PHP error log. To enable PHP error logging, open the php.ini file and uncomment the following line:
error_log = /var/log/php/error.log log_errors = On
Here, we specify the PHP error log as /var/log/php/error.log and enable errors Record. Please make sure the folder has been created and has the appropriate permissions.
Conclusion
In this article, we introduced some reasons why nginx cannot parse PHP files and provided solutions. When you face such issues, follow the above steps and properly configure nginx to handle PHP files.
The above is the detailed content of What to do if nginx does not parse php files. For more information, please follow other related articles on the PHP Chinese website!