坏蛋格鲁坏蛋格鲁

【Environment】Windows 搭建 MySQL + Nginx + PHP 开发环境


1. 安装 MySQL

  • 下载 MySQL
  • 按流程安装

2. 安装 Nginx

  • 下载 Nginx
  • 解压至目标路径,如 C:\Wnmp
  • 配置 nginx.conf 的 server,包括自定义项目根目录、添加独立项目
// C:\Wnmp\nginx-1.28.0\conf\nginx.conf

   http {
       ...
       
        # localhost
        server {
            listen       80;
            server_name  localhost;
            
            location / {
                root   D:/www;
                try_files $uri $uri/ /index.php?$query_string;
                index index.php index.html;
            }
            
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   D:/www;
            }
            
            location ~ \.php(.*)$ {
                root   D:/www;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO  $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                include        fastcgi_params;
            }
        }
       

        # myphp.test
        server {
            listen       80;
            server_name  myphp.test;
    
            location / {
                root   D:/www/phptest;
                try_files $uri $uri/ /index.php?$query_string;
                index index.php index.html;
            }
            
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   D:/www/phptest;
            }
            
            location ~ \.php(.*)$ {
                root   D:/www/phptest;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO  $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                include        fastcgi_params;
            }
        }
       
       ...
   } 
  • 添加 bat 处理文件,用于启动、停止 Nginx,可以存放在任何目录下,如 C:\Program Files\Mybat
REM C:\Program Files\Mybat\nginx.bat

@echo off
echo Nginx is Started

if "%1"=="h" goto begin 
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit 
:begin

C:\Wnmp\nginx-1.28.0\nginx.exe -p C:\Wnmp\nginx-1.28.0\

pause
REM C:\Program Files\Mybat\nginx-stop.bat

@echo off
echo Nginx is Stoped

if "%1"=="h" goto begin 
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit 
:begin

C:\Wnmp\nginx-1.28.0\nginx.exe -p C:\Wnmp\nginx-1.28.0\ -s quit

pause

3. 安装 PHP

  • 下载并安装 Microsoft Visual C++
  • 下载 PHP
  • 解压至目标路径,如 C:\Wnmp
  • 在解压后的 PHP 根目录下,将 php.ini-development 文件复制一份改为 php.ini,作为 PHP 的配置文件
  • 配置 PHP,根据项目实际需求修改 php.ini,如设置扩展目录、开启相关扩展
# C:\Wnmp\php-8.4.12\php.ini

# 设置时区
date.timezone=Asia/Shanghai
 
# 扩展目录
extension_dir ="ext"
 
# 开启扩展
extension=curl
extension=mysqli
extension=pdo_mysql
  • 配置环境变量:计算机 -> 右键选择属性 -> 高级系统设置 -> 环境变量 -> 系统变量 选择path,将 php 解压的路径 C:\Wnmp\php-8.4.12 添加到 path 当中,然后在终端中输入 php -v,如果正常显示出了 PHP 版本信息,则说明安装 OK
  • 添加 bat 处理文件,结合 Nginx,一次性启动、关闭 PHP + Nginx 环境,可以存放在任何目录下,如 C:\Program Files\Mybat

注意:此处 bat 文件里用到了 RunHiddenConsole.exe ,作用是在执行完命令行脚本后可以自动关闭脚本,而从脚本中开启的进程不被关闭。简单来说就是在终端窗口运行的程序不被关闭的情况下隐藏终端窗口,特别是对一些会挂住必须显示命令窗体的命令很有用,如 Tomcat、Php、Nginx 等

下载 RunHiddenConsole.exe,存放于 bat 文件的同级目录内,如 C:\Program Files\Mybat

REM C:\Program Files\Mybat\php-start.bat

@echo off
echo PHP is Started

if "%1"=="h" goto begin 
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit 
:begin

set PHP_FCGI_MAX_REQUESTS = 1000

RunHiddenConsole.exe C:\Wnmp\php-8.4.12\php-cgi.exe -b 127.0.0.1:9000 -c C:\Wnmp\php-8.4.12\php.ini 

C:\Wnmp\nginx-1.28.0\nginx.exe -p C:\Wnmp\nginx-1.28.0\

pause
REM C:\Program Files\Mybat\php-stop.bat

@echo off
taskkill /F /IM nginx.exe > nul
taskkill /F /IM php-cgi.exe > nul
echo PHP is Stopped
exit
本原创文章未经允许不得转载 | 当前页面:坏蛋格鲁 » 【Environment】Windows 搭建 MySQL + Nginx + PHP 开发环境

评论