Windows 10 手动配置 PHP 开发环境
目录结构
WebServer
├── logs
│ ├── nginx-service
│ └── php-service
├── mariadb
│ └── data
├── nginx
│ ├── nginxservice.exe
│ ├── nginxservice.xml
│ └── conf
│ ├── conf.d
│ │ └── default.conf
│ └── nginx.conf
├── php
│ ├── php.ini
│ ├── phpservice.exe
│ ├── phpservice.xml
│ └── php-stop.cmd
└── wwwroot
上面的结构仅展示比较重要的文件或者目录。
- logs 目录存放 WinSW 记录和错误报告。
- mariadb 目录中的 data 目录存放数据库信息。
- wwwroot 目录为 default.conf 配置中的站点根目录。
配置安装
配置 Nginx
- 访问 Nginx(Nginx 目前有两种比较流行的衍生版本:Tengine 和 OpenResty)下载 Windows 版的软件包。Nginx 中文件显示为 nginx/Windows-x.x.x,Tengine 中文件显示为 Tengine-x.x.x.tar.gz,OpenResty 中文件显示为 openresty-x.x.x.x-win64.zip(本文以 OpenResty 为例,撰写时最新的版本为 1.15.8.3)。
- 将压缩包内的文件提取到 D:\\WebServer\\nginx 目录。
- 打开 D:\\WebServer\\nginx\\conf\\nginx.conf 文件,替换文件内容如下:
worker_processes auto;
worker_rlimit_nofile 51200;
user www www;
events {
multi_accept on;
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
client_body_buffer_size 10m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_intercept_errors on;
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\\.(?!.*SV1)";
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
include "conf.d/*.conf";
}
- 在 D:\\WebServer\\nginx\\conf 目录新建名为 conf.d 的文件夹,并在 conf.d 文件夹中新建一个名为 default.conf 的配置文件,添加文件内容如下:
server {
listen 80;
server_name _;
root "D:\\WebServer\\wwwroot";
index index.html index.htm index.php;
location / {
autoindex on;
autoindex_localtime on;
}
access_log on;
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;
}
location ~ /\\.ht {
deny all;
}
}
- 在环境变量的管理页面中找到 Path 变量,新增以下路径:
D:\\WebServer\\nginx
配置 PHP
- 访问 PHP For Windows 下载 NTS 版本的 PHP(撰写时最新的 PHP 版本为 7.4.4)。因为本文是与 Nginx 配合使用,所以推荐下载 Non Thread Safe 版本,如果是与 IIS 配合使用,那么推荐下载 Thread Safe 版本。
- 将压缩包内的文件提取到 D:\\WebServer\\php 目录。
- 在 D:\\WebServer\\php 目录,将 php.ini-production 重命名为 php.ini 并打开,找到下面的参数去掉注释(删掉每行前的 ; 符号):
extension_dir = "ext"
extension=bz2
extension=curl
extension=gd2
extension=gettext
extension=imap
extension=mbstring
extension=exif
extension=mysqli
extension=openssl
extension=sqlite3
- 在 D:\\WebServer\\php\\php.ini 文件中找到
post_max_size
参数,并将值修改为 300M,找到upload_max_filesize
参数,并将值修改为 300M,找到date.timezone
参数,并将值修改为 Asia/Shanghai。 - 在 D:\\WebServer\\php 目录,创建一个名为 phpservice.xml 的空文件,添加文件内容如下:
taskkill /f /IM php-cgi.exe
- 在环境变量的管理页面中找到 Path 变量,新增以下路径:
D:\\WebServer\\php
配置 MariaDB
- 访问 MariaDB 下载最新版本软件包(撰写时最新的版本为 10.5.2),根据搭建环境所使用的平台,按需下载 x86 或 x64 版本的 zip 压缩包。
- 将压缩包内的文件提取到 D:\\WebServer\\mariadb 目录。
- 在环境变量的管理页面中找到 Path 变量,新增以下路径:
D:\\WebServer\\mariadb\\bin
配置 WinSW
WinSW 是可执行的二进制文件,可用于包装和管理 Windows 服务的自定义进程。
- 访问 WinSW 下载最新版本的二进制文件(WinSW.NET461.exe)。
- 将 WinSW.NET461.exe 复制到 D:\\WebServer\\nginx 目录,并重命名为 nginxservice.exe,然后新建一个名为 nginxservice.xml 的配置文件,添加文件内容如下:
<service>
<id>Nginx</id>
<name>Nginx</name>
<description>Nginx</description>
<executable>D:\\WebServer\\nginx\\nginx.exe</executable>
<logpath>D:\\WebServer\\logs\\nginx-service</logpath>
<logmode>roll</logmode>
<depend></depend>
<startargument>-p</startargument>
<startargument>D:\\WebServer\\nginx</startargument>
<stopexecutable>D:\\WebServer\\nginx\\nginx.exe</stopexecutable>
<stopargument>-p</stopargument>
<stopargument>D:\\WebServer\\nginx</stopargument>
<stopargument>-s</stopargument>
<stopargument>stop</stopargument>
</service>
- 将 WinSW.NET461.exe 复制到 D:\\WebServer\\php 目录,并重命名为 phpservice.exe,然后新建一个名为 phpservice.xml 的配置文件,添加文件内容如下:
<service>
<id>PHP</id>
<name>PHP</name>
<description>PHP</description>
<executable>D:\\WebServer\\php\\php-cgi.exe</executable>
<stopexecutable>D:\\WebServer\\php\\php-stop.cmd</stopexecutable>
<env name="PHPRC" value="D:\\WebServer\\php" />
<logpath>D:\\WebServer\\logs\\php-service</logpath>
<logmode>roll</logmode>
<startargument>-b9000</startargument>
<startargument>-cD:\\WebServer\\php\\php.ini</startargument>
</service>
安装 Nginx 服务
以管理员模式启动 Windows 命令提示符,并运行以下命令
D:\\WebServer\\nginx\\nginxservice.exe install
安装 PHP 服务
以管理员模式启动 Windows 命令提示符,并运行以下命令
D:\\WebServer\\php\\phpservice.exe install
安装 MariaDB 服务
以管理员模式启动 Windows 命令提示符,并运行以下命令(--password 参数为 root 账号默认密码,命令中设置密码为 Vtrois):
D:\\WebServer\\mariadb\\bin\\mysql_install_db.exe --datadir=D:\\WebServer\\mariadb\\data --service=MariaDB --password=Vtrois
操作命令
命令 |
描述 |
---|---|
net start/stop nginx |
启动/停止 Nginx 服务 |
net start/stop php |
启动/停止 PHP 服务 |
net start/stop mariadb |
启动/停止 Mariadb 服务 |
卸载删除
- 停止 Nginx 、PHP 以及 Mariadb 服务,执行下面的命令:
sc stop nginx
sc stop php
sc stop mariadb
- 卸载 Nginx 、PHP 以及 Mariadb 服务,执行下面的命令:
sc delete nginx
sc delete php
sc delete mariadb
- 在环境变量的管理页面中找到 Path 变量,删除下面的路径:
D:\\WebServer\\nginx
D:\\WebServer\\php
D:\\WebServer\\mariadb\\bin
- 注意备份 wwwroot 以及 mariadb\\data 目录中的重要数据,然后删除 WebServer 文件夹。