Home  >  Article  >  Backend Development  >  How to install nginx and php under windows

How to install nginx and php under windows

PHPz
PHPzOriginal
2023-04-11 10:42:582927browse

Nginx is a high-performance web server with very good compatibility. It can not only act as a web server, but also act as a reverse proxy, load balancer and other roles. It is one of the indispensable components in the Internet infrastructure. Under Windows, we can easily install and configure Nginx to meet our needs.

This article will introduce how to install Nginx PHP in a Windows environment to provide Web services, and elaborate on possible problems and solutions during the configuration process.

1. Install Nginx

1. Download Nginx

Download the Windows version of Nginx from the Nginx official website (http://nginx.org/) and extract it to the specified file Table of contents.

2. Configure Nginx

(1) Edit the nginx.conf file, which is the main configuration file of Nginx and has many configuration items. Modify the following items:

worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    #实体文件最长有效时间(缓存)
    sendfile        on;
    #针对sendfile文件的缓存,尽可能大,使用磁盘缓存
    sendfile_max_chunk 1m;
    #启用tcp_nopush使数据尽快地发送到客户端,默认关闭。
    tcp_nopush      on;
    #tcp_nodelay应置于“on”,即对于目的地或来自浏览器的nginx客户端,应该避免一些发送消息的延迟。
    tcp_nodelay      on;
    #将缓冲区从磁盘io操作改为内存操作
    aio              on;
    #不更新文件上次访问时间,可减少磁盘io操作
    open_file_cache off;
    #gzip压缩开启,compression_ratio是压缩的比率
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types       text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    include servers/*;
}

(2) Create a new servers folder and create a server.conf file under the folder. This file serves as the configuration file for a specific website, for example:

server {
    listen        80;         //端口号
    server_name   localhost;  //虚拟主机名
    index         index.html index.php;  //默认首页
    root E:/htdocs;            //根目录
    autoindex on;              //自动索引
    client_max_body_size  1m;  //客户端上传文件大小
    fastcgi_connect_timeout 300; //fastcgi超时时间
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;  //fastcgi缓冲区大小
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;  //php-fpm的监听地址
        fastcgi_index index.php;
        include fastcgi_params;       //fastcgi常用参数
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

(3) Start Nginx, enter the Nginx installation directory, and execute the command:

start nginx

(4) Check whether Nginx starts successfully. Enter localhost directly in the browser. If the Nginx welcome page can be displayed normally, Then Nginx has been successfully installed.

2. Install PHP

1. Download PHP

Download the Windows version of PHP from the PHP official website (http://windows.php.net/download/) and unzip it to the specified directory.

2. Configure PHP

(1) Edit the php.ini configuration file and modify the following items:

;php的扩展(Linux格式分隔符);表示分隔符
extension=php_openssl.dll;
extension=php_pdo_mysql.dll;
extension=php_mbstring.dll;
extension=php_curl.dll;
extension=php_fileinfo.dll;
extension=php_mysqli.dll;
extension=php_pdo_mysql.dll;
extension=php_pdo_sqlite.dll;

(2) Start PHP-FPM and enter the PHP installation directory , execute the command:

php-cgi.exe -b 127.0.0.1:9000

3. Integrate Nginx and PHP

1. Modify the fastcgi_pass item in the Nginx configuration file and change it to:

#修改前
fastcgi_pass   127.0.0.1:9000;
#修改后
fastcgi_pass   127.0.0.1:9000;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
fastcgi_index  index.php;
include        fastcgi_params;

2. Check Nginx Is the configuration file correct:

nginx -t

3. Restart Nginx:

nginx -s reload

4. Test the PHP environment, create the test.php file, and enter the following content in the file:

<?php
phpinfo();
?>

Then enter localhost/test.php in the browser. If the PHP environment can be displayed, PHP has been successfully installed.

4. Install MySQL

In order to provide more complete Web services, we usually also need to install MySQL as a database solution under Windows.

1. Download MySQL

Download the Windows version of MySQL from the MySQL official website (https://www.mysql.com/downloads/windows/) and extract it to the specified directory.

2. Configure MySQL

(1) Initialize MySQL:

cd mysql/bin
mysqld --initialize-insecure --user=mysql

(2) Start the MySQL service:

mysqld --console

(3) Modify the MySQL root user Password:

mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

Here new_password is the new password, and the password of the MySQL root user can be successfully modified.

5. Summary

Through the above steps, we have successfully installed Nginx PHP in the Windows environment and successfully set up a Web server, providing a powerful solution for our Web applications. support. It should be noted that compatibility issues may arise between different versions of software, so you need to be very careful and patient during the installation and configuration process, and understand and master as many details of the interaction between software as possible in order to be truly efficient. , Use these software stably.

The above is the detailed content of How to install nginx and php under windows. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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