Turn: Installation and configuration of Nginx + PHP5 on Windows

Source: Internet
Author: User
Turn: Installation and configuration of Windows Nginx + PHP5

turn: Installation and configuration of Windows Nginx + PHP5

Nginx is a lightweight, high-performance Http WebServer, written in event-driven fashion, so nginx is more stable, better performing, simpler to configure and less resource-intensive than Apache. Here are my steps for Nginx and PHP5.2 in Windows 7 installation.

Installing PHP5

First, from the http://www.php.net/downloads.php? Download the latest version of Php5.2.9-2 Windows, unzip to C:\PHP5, and rename the php.ini-recommended in the Tarball to PHP.ini, and then open the Modify several options:

1            2            3            4            5            6            7            8            9            ten            13            
error_reporting =E_all            display_errors = on            Extension_dir = "C:\php5\ext"?dynamic expansion, you can remove the extension in front of the comments as needed;            , such as loading PDO, MySQL            extension=Php_pdo.dll            extension=Php_pdo_mysql.dll?; CGI SettingsCgi.force_redirect=1Cgi.fix_pathinfo=1Cgi.rfc2616_headers=1

?

PHP load extensions need to be aware of dependencies, such as Php_exif.dll needs Php_mbstring.dll, you have to put Php_mbstring.dll in front of the Php_exif.dll to load successfully. Some extensions rely on additional DLL files, such as PHP 5.0+, Php_mysqli.dll relies on Libmysql.dll, and Php_oci8.dll, you need to install Oracle 8 client. If you are not familiar with these dependencies, refer to the Install.txt file in the installation package.

The search order of dependent files: First is the directory where the Php.exe is located, if it is ISAPI mode, the startup location of the Web Server is searched, such as the Apache bin directory, followed by the directory in the Windows PATH environment variable. do not copy any files to the Windows directory, if necessary, you can add C:\PHP5 to PATH, in favor of later PHP upgrade.

Installing Nginx

Starting with v0.7.52, Nginx launches the Windows version of Nginx, which you can download on its official website:

Http://nginx.net

If you need an older version of Nginx for Windows, can you Look for Kevin Worthington's website.

I am using 0.7.55, download good later, unzip release file to C:\nginx.

So how can I configure Nginx so that it works in conjunction with PHP?

Configure PHP FastCGI

Nginx needs to work with FastCGI server in order to process requests, there are two ways to run PHP FastCGI server, one is to use PHP built-in FastCGI Manager:

1                
C:/php5/-b 127.0.0.1:9000 C:/php5/php.ini

Another way is to use third-party tools, such as PHP-FPM, cgi-fcgi, and so on. Obviously! It's extremely painful to use these tools in Windows, and you might need to Cygwin something like that, and someone did, though I think it was trouble.

Next, start to modify Nginx configuration file C:\nginx\conf\nginx.conf, find the relevant parts of PHP, modified as follows:

1                    2                    3)                    4                    5                    
# Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000                     Location ~ \.php$ {                    root    d:/public_html;                    include php.conf;                    }

Root is $document _root refers to your PHP scripts root directory, which is set to the root directory of your Web site. Under Windows, you need to be aware of the root path, it is best to use "/" as the path delimiter, rather than Windows default "\", otherwise prone to problems, such as this path: D:\public_html\test, will not work, Nginx will throw 500 Error, because \ t is parsed as a tab in \test. Of course, plus a backslash escape is also possible, such as: D:\\public_html\\test.

Php.conf is the file I created to save the PHP configuration, in fact there are only 3 lines of command:

1                        2                        3                        4                        5                        6                        
# Connect to the native 9000 port, where the port refers to the port that PHP FastCGI Server opens,                        # Please be consistent                        with Php-cgi.exe open ports # when Nginx receives a PHP file request, it is automatically forwarded to PHP FastCGI Server                        Fastcgi_pass    127.0.0.1:9000;                        Fastcgi_index   index.php;                        include         Fastcgi_params;

To create a standalone php.conf save configuration in order to streamline nginx.conf, when configuring multiple virtual hosts in Nginx, each virtual host needs to be configured with PHP, then the master configuration file will become redundant and bloated.

In addition, you need to modify the C:\nginx\conf\fastcgi_params file, add a line:

1                            
Fastcgi_param  script_filename    $document _root$fastcgi_script_name;

and modify php.ini, set cgi.fix_pathinfo = 1, which is very important, otherwise PHP will not be able to find the PHP script to be processed.

Some other settings for the master server:

1                                2                                3                                4                                5                                6                                7                                8                                9                                ten                                14                                
# Number of processes opened by defaultWorker_processes1;                                ? Error_log Logs/error.log;#error_log logs/error.log notice;                                #error_log logs/error.log info;?#pid logs/nginx.pid;? Events {# Maximum number of connections processed by a process,                                # Local development, do not need the default 1024, this is changed toWorker_connections -; }

When a directory does not exist under the default index.php index.html, such as the home page file, Nginx will throw 403 ERROR, if you need to list this directory, you can add the following command in the HTTP {...}:

1                                    2                                    3                                    
on;                                    on;                                    on;

OK, integrate together

Create Start_nginx.bat to start PHP FastCGI and Nginx at the same time:

1                                        2                                        3                                        4                                        5                                        6                                        
@ Echo off                                        echo starting PHP FastCGI                                        ... Runhiddenconsole c:/php5/php-cgi.exe-b 127.0.0.1:9000 -c C:/php5/php.ini                                        ?                                        Echo Starting Nginx                                        ... C:/nginx/nginx.exe

RunHiddenConsole.exe is a small program used to hide DOS windows, which can be downloaded here.

After the Start_nginx.bat is turned on, there will also be a DOS window, but it can be safely turned off, and will not close Nginx and php-cgi.exe.

Same stop_nginx.bat, used to close:

1                                            2                                            3                                            4                                            5                                            6                                            
@ Echo off                                            echo stopping Nginx                                            ... nul                                            Echo Stopping PHP FastCGI                                            ... nul                                            Exit

The basic configuration is complete here.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.