Home Backend Development PHP Problem Windows nginx installation php

Windows nginx installation php

May 28, 2023 pm 08:11 PM

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

  1. Download 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.

  1. Configuring Nginx

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

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;

   }

 }

}

Copy after login

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.

  1. Start Nginx

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:

1

nginx.exe -c conf/nginx.conf

Copy after login

Install PHP

  1. Download 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.

  1. Configuring 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:

1

2

3

4

5

6

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;

}

Copy after login

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:

1

cgi.fix_pathinfo = 1

Copy after login

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:

1

php-cgi.exe -b 127.0.0.1:9000 -c C:phpphp.ini

Copy after login

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.

  1. Test 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

PHP API Rate Limiting: Implementation strategies. PHP API Rate Limiting: Implementation strategies. Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Input Validation: Best practices. PHP Input Validation: Best practices. Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

See all articles