【环境搭建】WordPress环境搭建

文章目录
概述

Windows 环境搭建 WordPress。PHP 调试环境搭建

相关参考

[toc]

下载安装

建议先下载 WordPress,再根据 WordPress 文件中的 readme.html 下载对应版本的 PHP 和 MySQL。另外的需要下载 Nginx 实现 Web 服务,这个可以单独下载。

  1. WordPress

    下载链接:Download – WordPress.org

    下载完成后打开 readme.html 文件,查看最低系统需求或者根据系统推荐下载 PHP 和 MYSQL

  2. 下载 MYSQL

    下载链接:MySQL :: Download MySQL Community Server (Archived Versions)

    下载完成后直接安装就行,设置用户名和密码,进入到 mysql 环境后创建 wordpress 数据库

    1
    create database wordpress;
  3. 下载 PHP

    下载链接:windows.php.net - /downloads/releases/archives/

配置环境

配置环境主要是修改配置文件

nginx.conf

文件路径:\wordpress-4.9.4-zh_CN\nginx-1.28.0\conf\nginx.conf

修改点 Line10\Line11\Line32

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
40
41
42
43
44
45
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root E:/wordpress-4.9.4-zh_CN/wordpress; # 这里修改为 wordpress 的目录
index index.html index.htm index.php; # 这里添加 index.php
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root E:/wordpress-4.9.4-zh_CN/wordpress; # 这里同样修改为 WordPress 所在目录
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

php.ini

文件路径: 下载的根目录

修改点如下所示:

  1. extension_dir 设置到 php 目录下的 ext

    1
    2
    3
    4
    5
    ; Directory in which the loadable extensions (modules) reside.
    ; https://php.net/extension-dir
    ;extension_dir = "./"
    ; On windows:
    extension_dir = "E:\wordpress-4.9.4-zh_CN\php-8.5.1-Win32-vs17-x64\ext"
  2. 开启 cgi.fix_pathino

    1
    2
    3
    4
    5
    6
    7
    8
    ; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
    ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
    ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
    ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
    ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
    ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
    ; https://php.net/cgi.fix-pathinfo
    cgi.fix_pathinfo=1
  3. 启动 php_mysqli.dll 插件

    1
    extension=php_mysqli.dll #新版本中可能省略了后缀.dll

Web-Config.php

在 WordPress 的下载目录有一份 Web-Config-Sample.php 的文件,拷贝一份为 Web-Config.php,然后修改对应的数据库字段即可。

启动

启动主要分两个步骤:

  1. 启动 nginx
  2. 启动 php-cgi

手动启动步骤如下所示:

1
2
3
4
5
# 启动 nginx.exe
nginx.exe

# 启动 PHP
%PHP_PATH%\php-cgi.exe -b 127.0.0.1:9000 -c %PHP_PATH%\php.ini

大致的脚本如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@echo off

set PHP_FCGI_MAX_REQUESTS = 1000

echo Starting PHP FastCGI...

rem 分别为php-cgi.exe和php.ini的路径 ,-b,-c等参数必须保留且注意前后空格

RunHiddenConsole %PHP_PATH%\php-cgi.exe -b 127.0.0.1:9000 -c %PHP_PATH%\php.ini

start E:\wordpress-4.9.4-zh_CN\nginx-1.28.0\nginx.exe

echo Starting nginx...

rem 填写nginx.exe实际位置

RunHiddenConsole %NGINX_PATH%\nginx.exe

pause

对应的关闭程序脚本

1
2
3
4
5
6
@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
pause

WordPress 调试环境搭建

环境说明

原理很简单,就是让 php 在启动的时候可以加载 xdebug 的动态库,然后就可以通过 VSCode 的 PHP Debug 插件进行调试。

  • PHP版本和上文部署的一致
  • Visual Studio
  • PHP XDebug

调试环境配置:

推荐文章中有使用 PHP Study 的,鉴于我这里是为了调试 WordPress 源码,因此还是自己琢磨下,不使用 PHPStudy。

安装插件 PHP Debug

1767088968721

XDebug

然后就是下载对应的 XDebug 版本了,运行 phpinfo() 获取版本信息,然后粘贴到 Xdebug: Support — Tailored Installation Instructions 这个页面,就可以自动获取要安装的版本了。

由于 PHP 5.5.6 版本不支持,要考虑使用别的方案了

下载完成对应的 XDebug 文件后,在 php.ini 中添加如下所示的配置:

1
2
3
4
5
6
7
8
[PHP]
zend_extension=E:\\wordpress-4.9.4-zh_CN\\php_xdebug-2.5.5-5.5-vc11-x86_64.dll # xdebug dong'tai'k
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9003 #配置端口 9003 与 launch.json 配置保持一致
xdebug.log = "E:\\wordpress-4.9.4-zh_CN\\xdebug.log"

然后就是修改 VSCode 的配置文件,设置 php 的调试路径,在 VSCode 的 settings.json 中添加如下配置:

1
2
"php.validate.executablePath": "E:\\wordpress-4.9.4-zh_CN\\php-5.5.6-Win32-VC11-x64\\php.exe",
"php.debug.executablePath": "E:\\wordpress-4.9.4-zh_CN\\php-5.5.6-Win32-VC11-x64\\php.exe",

最后就是在将 WordPress 的源代码目录打开为 VSCode 的工作区并添加 launch.json 调试配置文件:

主要的调试配置如下所示:

1
2
3
4
5
6
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
}

一切配置完成后就可以启动 Wordpress 调试了。

WordPress 源码断点命中截图:

PHP 源码编译

环境说明

这里还是以 PHP 5.5.6 为例,但是这个版本就会比较麻烦,需要老版本的SDK和WDK才行。用新版本的只需要安装官方文档的教程执行即可。

完成的编译步骤可以参考官方文章:

  1. microsoft/php-sdk-binary-tools: Tool kit for building PHP under Windows
  2. PHP: internals:windows:stepbystepbuild

关于要编译的版本与对应的 Visual C++ 版本对比可以在这篇文章看到:PHP: internals:windows:stepbystepbuild_sdk_2

相关参考文章:

  1. 在windows平台上构建自己的PHP(php5.3+) - driftcloudy - 博客园

下载 PHP 源码

  1. 可以从 PHP 的 Github 页面下载历史版本

    php/php-src: The PHP Interpreter

  2. 也可以在 Windows.php.net 页面下载

    windows.php.net - /downloads/releases/archives/

下载 PHP 编译工具

microsoft/php-sdk-binary-tools: Tool kit for building PHP under Windows

上述链接也包括 PHP 的编译工具 php-sdk-binary。可以帮助配置 PHP 的编译环境

搭建环境编译

剩下的就是按照官方文档的步骤执行了:

  • git clone https://github.com/Microsoft/php-sdk-binary-tools.git c:\php-sdk
  • cd c:\php-sdk
  • git checkout php-sdk-2.1.9 or later
  • invoke phpsdk-vc15-x64.bat
  • phpsdk_buildtree phpmaster
  • git clone https://github.com/php/php-src.git && cd php-src, or fetch a zipball
  • phpsdk_deps --update --branch master, use phpsdk_deps --update --branch X.Y for a non master branch
  • do the build, eg. buildconf && configure --enable-cli && nmake

一些遇到的问题

  1. PHP7 编译报错常量异常 (涉及到的版本 7.0.2)

    **问题原因:**文件编码问题导致的,可以参考这篇文章修改下相关文件编码PHP :: Bug #52974 :: jewish.c: compile error under Windows with GBK charset

  2. 编译替换相关问题

    1. ThreadSafety 问题

      如果你下载运行的 Thread Safe 版本,那么在编译时也要统一为 ts 版本

    2. 本地编译的替换问题

      首先是插件,如果你在配置文件中启用了某些插件,那么在本地编译时ye’xu’yao