Home > Article > Backend Development > Windows nginx installation php
When building a web server, Nginx is a high-performance web server, and PHP is a commonly used web development language. When we want to integrate Nginx with PHP, we need to install and install them. configuration. This article will introduce the detailed process of installing and configuring Nginx and PHP in a Windows environment.
Install Nginx
First, we need to go to the official website of Nginx (https://nginx.org/en/download.html ), download the latest Windows version, choose the 32-bit or 64-bit version, and extract it to a custom directory. For example, you can unzip Nginx to C:
ginx.
Next, we need to create a conf directory in the Nginx directory, and then create an nginx.conf file in the conf directory. In the nginx.conf file, enter the following content:
worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } 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; } } }
In the above configuration file, the main configuration of Nginx is under http and server. The meanings of the server configurations are as follows:
listen: the listening port number.
server_name: Server name, which can be an IP address or domain name.
root: Web root directory.
index: Default homepage file.
location /: Configuration when the URL path is /.
try_files: nginx searches for files in the path requested by the user. If found, it returns directly. If not found, the request is forwarded to FastCGI for processing.
error_page: Define error page.
location ~ .php$: Forward php requests to FastCGI for processing.
After completing the configuration, we can start Nginx, enter the Nginx directory, and double-click the nginx.exe file to start. Or, enter the Nginx directory on the command line and enter the following command to start:
nginx.exe -c conf/nginx.conf
Install PHP
First, we You need to download the latest Windows version from PHP's official website (https://windows.php.net/download/), select the 32-bit or 64-bit version, and then extract it to a custom directory. For example, PHP can be extracted to C:php.
In order to integrate PHP with Nginx, we need to add the FastCGI module to Nginx so that Nginx can forward php requests to PHP. To do this, we need to add the following configuration to nginx.conf in the conf directory of Nginx:
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; }
The above configuration forwards php requests to FastCGI for processing. In order for Nginx to find PHP, we need to find the php.ini-development file in the PHP directory, copy it to the same directory and name it php.ini. Then, we need to modify the following configuration of the php.ini file:
cgi.fix_pathinfo = 1
This configuration means that PHP can obtain incomplete file paths from URLs. This is so that Nginx can load the correct PHP script based on the URL information. If this option is not turned on, a 404 error will occur.
Next, we need to find php-cgi.exe in the PHP directory, right-click the file and select "Open with Notepad". Copy the following command to Notepad and save it:
php-cgi.exe -b 127.0.0.1:9000 -c C:phpphp.ini
Among them, the -b parameter indicates the IP address and port number that FastCGI listens to, and the -c parameter indicates the PHP configuration file path. Copy the saved command, open the command line window, enter the PHP directory, and enter the command to start PHP.
After completing the above settings, we can test whether PHP is working properly. Open the directory where Nginx is located, open the conf and html folders in sequence, and copy a test.php file to the folder. Open the browser and enter localhost/test.php in the address bar. If some PHP information can be returned normally, it means that we have successfully integrated PHP into Nginx.
Summary
Through the above steps, we have successfully installed and integrated Nginx and PHP. If it is a new installation, you need to pay attention to promptly query, review and solve problems when encountering problems during the configuration process. These two tools can create a complete web server for us, thereby providing a better web development experience. This is a great option for expanding our development capabilities.
The above is the detailed content of Windows nginx installation php. For more information, please follow other related articles on the PHP Chinese website!