利用WSL+Visual Studio Code调试PHP项目
在Windows下调试PHP是一件麻烦的事情,简单的PHP调试当然可以使用WAMP/WNMP等套件,不过不是很喜欢这种集成式的套件,秉着自己安装
也许更快(超慢)的想法开始了安装
背景
公司的项目是基于Nginx+YII的一套PHP项目,而且Nginx还是搭配Nchan插件的(也就是需要自编译的),基本就否定了在Windows下直接装Nginx的想法,Windows下编译Nginx太麻烦了,而且想着既然是php+nginx的环境还是Linux顺手,这时候想起了WSL系统,莫不如搭配WSL来一套PHP环境?于是开始尝试
环境
- Windows 10:Visual Studio Code + XDEBUG远程调试 + PHP源码
- WSL(Ubuntu子系统):Nginx + MySQL + PHP-FPM7.2 + PHP源码
步骤
- WSL下编译安装Nginx(nginx插件Nchan)
- WSL下PHP7.2的安装与配置
- Windows下PHP7.2的安装与配置
- Windows下调试PHP
1.WSL下编译安装Nginx(nginx插件Nchan)
参考文章 https://www.chancel.cn/#/articles/articleProgramContent/11
方法一:编译安装:WSL下编译Nginx失败了,一些so库总是有错误,最后发现Nchan有现成的插件版...
- 编译Nginx参考:Nginx的编译安装(安装自定义模块)
方法二:直接安装
- 直接下载如下两个deb
- 安装DEB
- dpkg -i nginx-common.ubuntu.deb
- dpkg -i nginx-extras.ubuntu.deb
安装Nchan部分配置参考
vim /etc/nginx/conf.d/xxx.conf
server{ charset utf-8; client_max_body_size 200M; server_name 127.0.0.1; root /mnt/d/code/company/syncthing_manage; index index.php index.html; #access_log off; access_log /var/log/nginx/sync_access.log ; error_log /var/log/nginx/sync_error.log; 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; } }
2.WSL下PHP7.2的安装与配置
这两个都是直接安装的
php7.2安装
# 安装源 sudo apt-get 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
修改PHP配置
- vim /etc/php/7.2/fpm/pool.d/www.conf
…… ; 用户修改为本机用户(注意:Nginx要和php-fpm的用户一致,同时也要对代码目录有读写权限) user = chancel group = chancel …… ; 本地的php的端口号(与Nginx通信的端口号) listen = 127.0.0.1:9000
启动PHP7.2
service php7.2-fpm start # 如果启动不了,检查php-fpm的服务名称,根据服务名称进行启动 service --status-all | grep -i fpm
配置XDebg
检查php版本
php -i
打开https://xdebug.org/wizard.php,填入 php -i 返回的结果,点击确定之后页面会出现安装XDebug的教程,这里简单翻译一下
1. 下载xdebug-2.7.2.tgz 2. xdebug依赖 php-dev php-devel automake autoconf 请确保依赖项已经安装 3. 下载完成后解压xdebug(参考指令 tar -xvzf xdebug-2.7.2.tgz) 4. 切换到xdebug解压后的目录 5. 运行 phpize,如果看到如下输出说明安装完成 Configuring for: ... Zend Module Api No: 20170718 Zend Extension Api No: 320170718 如果没有,请参考 [FAQ entry](https://xdebug.org/docs/faq#custom-phpize) 6. 运行 ./configure 7. 运行 make 8. 运行 cp modules/xdebug.so /usr/lib/php/20170718 9. 更改 /etc/php/7.2/cli/php.ini 在最后添加 zend_extension = /usr/lib/php/20170718/xdebug.so
完成XDebug的安装之后,在php.ini(位置:/etc/php/7.2/)中添加XDebug的配置
; 这里根据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
MySQL的安装与配置
安装MySQL
sudo apt install mariadb-server
启动MySQL
sudo service mysql start
配置MySQL
sudo mysql_secure_installation
配置远程账户登录(Note:这里因为WSL没有解锁ROOT账户,所以建立普通账户才能远程,或者考虑使用SSH通道来管理MySQL)
chancel@chancel-cp:/mnt/c/Users/ycs10$ sudo mysql -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 33 Server version: 10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [mysql]> grant all privileges on *.* to 'chancel'@'127.0.0.1' identified by 'wosyangshu' with grant option; Query OK, 0 rows affected (0.00 sec) MariaDB [mysql]> flush privileges; Query OK, 0 rows affected (0.00 sec)
到这里,WSL里面的PHP/XDdebug/Nginx/MySQL基本就配置完了,其中的Nginx连接PHP部分根据具体的业务需求填写配置,这里给出一份参考
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;
}
3. Windows下PHP7.2的安装与配置
php7.2安装配置
- 下载PHP7.2
- 解压PHP7.2到指定目录,并添加到Path中
- 输入 php -i 查看php是否安装成功
PHP XDEBUG配置
- 在CMD界面输入 php -i 根据输出,打开https://xdebug.org/wizard.php,填入 php -i 返回的结果,,点击确定之后页面会出现安装XDebug的教程,与WSL下安装XDEBUG相似,此处不再赘述,参考上面WSL如何配置PHP XDEBUG
Visual Studio Code安装与配置
- 下载安装Visual Studio Code
配置PHP开发环境,在VSC界面按住Ctrl+P,输入 USER SETTING 选择用户设置,搜索PHP,然后编辑配置,填好如下项目
"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,
安装插件
- PHP Debug
在Debug里选择Listen For XDebug,然后配置launch.json,注意serverSourceRoot应该是你WSL下源代码的路径,端口必须与WSL里面php.ini里面的XDebug里的php.ini的远程XDebug定义一致
{ "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 }
4. Windows下调试PHP
打开Visual Studio Code,选择你的PHP项目,并按F5选择PHP环境调试,打好断点即可