menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

Windows下借助WSL搭建PHP开发环境

作者:Chancel Yang, 创建:2019-05-29, 字数:6209, 已阅:1047, 最后更新:2024-03-10

在Windows下调试PHP一直使用WAMPWNMP套件,本文记录一次实践手动在Windows下安装PHP开发环境的过程

这次开发的项目是基于Nginx+Yii的PHP项目,Nginx是需要安装Nchan插件的,在Windows下Nginx编译与插件安装少嫌麻烦,好在微软推出了WSL/WSL2

这次在Windows下借助WSL+Xdebug来完成开发环境的安装,实现的效果是Visual Studio Code借助XdebugWSL开发Yii项目

Visual Studio Code 以下简称 VSCode,方便描述

1. WSL

1.1. 说明

Nchan是一个基于Nginx的插件,用于实现高性能、可扩展的实时消息传递和流媒体功能。它提供了多种功能,包括发布-订阅消息传递、长轮询、HTTP流、WebSocket等,使得构建实时应用程序和实时通信变得更加简单和高效。

WSL(Windows Subsystem for Linux)是Windows操作系统中的一个功能,它允许用户在Windows系统上运行Linux环境和应用程序。WSL提供了一个兼容层,使得Linux二进制文件能够在Windows上运行,而无需虚拟机或双启动系统。

系统环境

  • Windows 10:VSCode + Xdebug远程调试 + PHP源码
  • WSL(Ubuntu子系统):Nginx + MySQL + PHP-FPM7.2 + PHP源码

大致步骤

  1. WSL下编译安装Nginx(nginx插件Nchan)
  2. WSL下PHP7.2的安装与配置
  3. Windows下PHP7.2的安装与配置
  4. Windows下调试PHP

1.2. 开启WSL

要开启WSL(Windows Subsystem for Linux),请按照以下步骤进行操作:

  1. 打开“控制面板”:在Windows操作系统中,点击任务栏上的“开始”按钮,然后搜索并打开“控制面板”
  2. 进入“程序”设置:在控制面板中,找到并点击“程序”选项
  3. 启用“适用于Linux的Windows子系统”功能:在“程序”设置页面中,点击“启用或关闭Windows功能”链接
  4. 勾选“适用于Linux的Windows子系统”选项:在“Windows功能”对话框中,找到并勾选“适用于Linux的Windows子系统”选项
  5. 点击“确定”并等待安装完成:点击“确定”按钮后,Windows将开始安装WSL组件,安装完成后重启系统
  6. 安装Linux发行版:重新启动后,你需要从Microsoft Store,这里选择Ubuntu的子系统
  7. 安装完成后,在开始菜单中找到并点击已安装的Linux发行版的图标。这将启动WSL,并为你提供一个Linux shell终端窗口。在该终端中,你可以执行Linux命令和运行Linux应用程序

在成功进入子系统Ubuntu后,开始安装Nginx,Nginx官方提供deb包直接安装

  1. nginx-common.ubuntu.deb
  2. nginx-extras.ubuntu.deb

第一个是Nginx,第二个是nchan包,下载后直接安装

Bash
sudo dpkg -i nginx-common.ubuntu.deb
sudo dpkg -i nginx-extras.ubuntu.deb

编辑/etc/nginx/conf.d/dev.conf,内容如下

TEXT
server{
    charset      utf-8;
    server_name 127.0.0.1;

    root  /var/www/nginx;
    index index.php index.html;

    access_log  /var/log/nginx/dev-access.log ;
    error_log   /var/log/nginx/dev-error.log;

    # 以下是本地开发Nchan插件所需配置,无需理会
    location = /sub {
        nchan_subscriber;
        nchan_channel_id $arg_id;
        nchan_message_timeout 5s;
    }
    location = /pub {
        nchan_publisher;
        nchan_channel_id $arg_id;
        nchan_message_timeout 5s;
    }
}

1.3. PHP7.2

直接安装php7.2

Bash
# 安装源
sudo apt -y install software-properties-common python-software-properties
# 更新源
sudo add-apt-repository ppa:ondrej/php && sudo apt-get update
# 安装php7.2
sudo apt-get -y install php7.2
# 安装常用扩展(根据需要自选)
sudo apt-get -y install php7.2-fpm php7.2-mysql php7.2-curl php7.2-json php7.2-mbstring php7.2-xml  php7.2-intl 
# 可供参考的扩展包(根据需要自选)
sudo apt-get install php7.2-gd
sudo apt-get install php7.2-soap
sudo apt-get install php7.2-gmp    
sudo apt-get install php7.2-odbc       
sudo apt-get install php7.2-pspell     
sudo apt-get install php7.2-bcmath   
sudo apt-get install php7.2-enchant    
sudo apt-get install php7.2-imap       
sudo apt-get install php7.2-ldap       
sudo apt-get install php7.2-opcache
sudo apt-get install php7.2-readline   
sudo apt-get install php7.2-sqlite3    
sudo apt-get install php7.2-xmlrpc
sudo apt-get install php7.2-bz2
sudo apt-get install php7.2-interbase
sudo apt-get install php7.2-pgsql      
sudo apt-get install php7.2-recode     
sudo apt-get install php7.2-sybase     
sudo apt-get install php7.2-xsl
sudo apt-get install php7.2-cgi        
sudo apt-get install php7.2-dba 
sudo apt-get install php7.2-phpdbg     
sudo apt-get install php7.2-snmp       
sudo apt-get install php7.2-tidy       
sudo apt-get install php7.2-zip

编辑/etc/php/7.2/fpm/pool.d/www.conf文件,内容如下

TEXT
...
; 用户修改为本机用户(注意:Nginx要和php-fpm的用户一致,同时也要对代码目录有读写权限)
user = chancel
group = chancel
...

; 本地的php的端口号(与Nginx通信的端口号)
listen = 127.0.0.1:9000

启动PHP7.2

Bash
sudo service php7.2-fpm start

配置XDebg,首先检查php版本

Bash
sudo apt-get install php-xdebug

编译/etc/php/7.2/php.ini,加载Xdebug

TEXT
; 这里根据Xdebug.so 进行填写
zend_extension=xdebug.so 
[XDebug]
; 启用远程调试
xdebug.remote_enable = on
xdebug.remote_autostart = 1
; WSL与Windows共享网关,此处填127.0.0.1,如果是远程虚拟机则填写调试机器的IP
xdebug.remote_host = 127.0.0.1
; 远程XDebug配置的端口号,这个端口号在后面Windows10里配置,注意不要填9000,因为WSL里面的php-fpm已经使用这个端口了
xdebug.remote_port = 9001
xdebug.remote_connect_back = 1
xdebug.auto_trace = 1
xdebug.collect_includes = 1
xdebug.collect_params = 1
; 日志的位置
xdebug.remote_log = /tmp/xdebug.log

1.4. MariaDB

安装启动如下

Bash
sudo apt install mariadb-server
sudo service mysql start
sudo mysql_secure_installation

到这里,WSL里面的PHP/XDdebug/Nginx/MySQL基本就配置完了,其中的Nginx连接PHP部分需填写配置,这里给出一份参考

TEXT
location ~ \.php(.*)$ {
           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;
}

2. Windows

2.1. PHP

php7.2安装配置

  1. 下载PHP7.2
  2. 解压PHP7.2到指定目录,并添加到Path中
  3. cmd中运行php -i查看php是否安装成功

Xdebug的安装参考官方:https://xdebug.org/docs/install#windows

2.2. VSCode

下载安装VBC:下载安装VSCode

打开后在左侧寻找插件菜单,安装PHP Debug插件

紧接着按下ctrl+shift+p,输入user setting,选择用户设置,搜索php,选择编辑json,添加下面的内容

JSON
{
    "phpformatter.composer": true,
    "php.validate.executablePath": "C:\\dev\\php7.2\\php.exe",
    "php.suggest.basic": false,
    "php.executablePath": "C:\\dev\\php7.2\\php.exe",
    "php-cs-fixer.executablePath": "${extensionPath}\\php-cs-fixer.phar",
    "[php]": {
        "editor.defaultFormatter": "kokororin.vscode-phpfmt"
    },
    "php-cs-fixer.lastDownload": 1558509222941,
}

使用VSCode打开php源码,编辑.vscode/launch.json,内容如下

JSON
{
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "stopOnEntry":false,
    "localSourceRoot": "${workspaceRoot}",
    "serverSourceRoot": "/home/wwwroot/weiphp.dev",
    "port": 9001
},
{
    "name": "Launch currently open script",
    "type": "php",
    "request": "launch",
    "program": "${file}",
    "cwd": "${fileDirname}",
    "port": 9000
}

按F5选择PHP环境调试,打好断点即可

3. 尾声

参考资料

  • https://nchan.io
  • https://xdebug.org

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]