Home > Article > Backend Development > What should I do if mac nginx cannot parse php files?
It is a common operation to use Nginx to build a web server on Mac, but sometimes you encounter the problem that Nginx cannot parse PHP files. In this case, when accessing the PHP file, it will be downloaded directly instead of parsed and executed, which brings inconvenience to web development.
This article will introduce how to solve the problem that Nginx cannot parse PHP files when using Nginx to build a web server on Mac. It mainly includes the following aspects:
Nginx does not support PHP parsing by default. PHP-FPM needs to be used to implement PHP parsing. After PHP-FPM is started, it establishes a Socket connection with Nginx and forwards the request to the PHP-FPM process for processing. PHP-FPM then returns the processing results to Nginx, and Nginx finally returns the results to the client.
Therefore, the reasons why Nginx cannot parse PHP files may be as follows:
For the above reasons, take the following methods to solve them.
2.1 Install PHP-FPM
First you need to install PHP-FPM, you can use Homebrew to install:
brew install php-fpm
After the installation is complete, you can use the following command to check whether the installation is successful:
php-fpm -v
If the PHP version information is displayed, it means the installation is successful.
2.2 Start PHP-FPM
After installing PHP-FPM, you need to start the PHP-FPM process:
sudo php-fpm
After starting, you can use the following command to check whether PHP-FPM is started. Success:
ps aux | grep php-fpm
If information similar to the following is displayed, it means that PHP-FPM started successfully:
_www 49202 0.0 0.7 5871400 11664 ?? S 11:42上午 0:00.03 php-fpm: pool www
2.3 Configure Nginx
Add PHP parsing configuration in the Nginx configuration file, you can Add the following content in the server section:
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
Parsing instructions:
location ~ \.php$
: Indicates matching all requests ending with .phpfastcgi_pass 127.0.0.1:9000;
: Indicates that the request will be forwarded to the PHP-FPM process for processing. The port number here should be consistent with the port number used when the PHP-FPM process is started.fastcgi_index index.php;
: Indicates that when there is no specified file in the request directory, index.php will be used as the entry file by default fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
: Indicates that the full path of the requested file is passed to the PHP-FPM process, where $document_root represents the Web root directory configured by Nginx, and $fastcgi_script_name represents the full path of the request (excluding domain name and parameters) include fastcgi_params;
: Indicates the introduction of the FastCGI parameter configuration file, which contains some parameter configurations related to FastCGI. After the configuration is completed, you can use the following command to reload the Nginx configuration:
sudo nginx -s reload
The above is the method to solve the problem that Nginx cannot parse PHP files when using Nginx to build a web server on Mac. Hope this article is helpful to you.
The above is the detailed content of What should I do if mac nginx cannot parse php files?. For more information, please follow other related articles on the PHP Chinese website!