零、
在搭建开发环境的时候,为了让编程环境与调试环境独立开来,经常需要独立搭建一个远程的调试环境,本文基于此需求,将采用Xdebug在我的oVirt服务器上搭建一个远程调试环境。
一、需求与准备
- Centos环境,这里采用 Centos7 1804 minimal 镜像
- PHP7
- 与PHP版本对应的Xdebug作为远程调试器
- Nginx
- vsftp 用来同步代码文件
- JetBrains PhpStorm 用来进行PHP开发
二、关闭 SELinux与firewalld
SELinux是提供访问控制安全策略机制的安全模块,而firewall则是centos7开始采用的防火墙模块,在这里为了省事选择直接关闭它们。
对于firewall可以通过以下两条命令关闭:
[root@Debug ~]# chkconfig firewalld off
Note: Forwarding request to 'systemctl disable firewalld.service'.
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@Debug ~]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
而对于SELinux,首先通过sestatus命令查看SELinux的开启状态,当然在Centos7下它默认是开启的:
[root@Debug ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 31
我们只需要修改 /etc/selinux/config,将其中的SELINUX=enforcing修改为disabled,然后reboot即可:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
重启后我们通过sestatus再次查看SELinux的状态,可以看到SELinux已经被关闭了:
[root@Debug ~]# sestatus
SELinux status: disabled
三、安装PHP7
依照(Centos官方wiki)[https://wiki.centos.org/HowTos/php7],我们开始安装PHP7环境:
[root@Debug ~]# yum -y install centos-release-scl.noarch
[root@Debug ~]# yum -y install rh-php70 rh-php70-php rh-php70-php-fpm httpd
[root@Debug ~]# yum -y install rh-php70-php-devel
四、配置Xdebug
安装xdebug模块:
[root@Debug ~]# yum -y install sclo-php70-php-pecl-xdebug
定位xdebug.so的位置:
[root@Debug ~]# find / -name "xdebug.so"
/opt/rh/rh-php70/root/usr/lib64/php/modules/xdebug.so
然后依照 Jetbrains提供的文档,我们开始配置Xdebug扩展:
[root@Debug php.d]# vim /etc/opt/rh/rh-php70/php.d/xdebug.ini
[Xdebug]
zend_extension=/opt/rh/rh-php70/root/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.remote_port=9010
xdebug.remote_host=192.168.2.1
remote_host指定的是开发机的ip地址,同时为了避免Xdebug的端口与php-fpm冲突,我们将默认的9000端口改成9010端口,在Xdebug配置完成后,我们启动php-fpm服务:
[root@Debug ~]# service rh-php70-php-fpm start
Redirecting to /bin/systemctl start rh-php70-php-fpm.service
如果Xdebug被成功配置的话,通过php –version可以看到Xdebug标识:
[root@Debug rh-php70]# /opt/rh/rh-php70/root/usr/bin/php --version
Cannot load Xdebug - it was already loaded
PHP 7.0.27 (cli) (built: Apr 4 2018 13:48:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans
五、安装Nginx
依照Nginx官方文档,我们开始安装Nginx服务器引擎:
[root@Debug yum.repos.d]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
yum 安装Nginx:
[root@Debug yum.repos.d]# yum -y install nginx
六、测试网站配置
在Nginx+PHP7+Xdebug环境配置好后,我们需要先为Nginx添加第一个测试站点,用来打印出php信息,以确保环境正常,我们将通过在/etc/nginx/conf.d下创建一个test.conf配置文件来创建一个测试站点:
[root@Debug ~]# cd /etc/nginx/conf.d
[root@Debug conf.d]# ls
default.conf
[root@Debug conf.d]# cp default.conf default.conf.bak
[root@Debug conf.d]# mv default.conf test.conf
[root@Debug conf.d]# vim test.conf
server {
listen 80;
server_name 192.168.2.206;
location / {
root /www/php/test;
index index.php;
}
location ~ \.php$ {
root /www/php/test;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/php/test$fastcgi_script_name;
include fastcgi_params;
}
}
接下来我们创建/www/php/test/index.php,并赋予相应权限:
[root@Debug conf.d]# mkdir -p /www/php/test
[root@Debug conf.d]# chmod -R 777 /www/php/test
[root@Debug conf.d]# echo "<?PHP phpinfo(); ?>" > /www/php/test/index.php
创建完index.php后,我们启动nginx服务:
[root@Debug conf.d]# service nginx start
Redirecting to /bin/systemctl start nginx.service
如果一切正常,这个时候访问服务器,phpinfo()将会打印出php信息,而Xdebug被加载,也会有区块显示Xdebug的信息,如图:
七、配置vsftpd
为了利用ftp将本地代码上传到调试机上,我们将依照Centos利用vsftpd搭建ftp服务安装并配置vsftpd来实现ftp服务器功能。
我们创建一个名为phpdebug的用户,并在/www/php/test下为其分配一个空间。
[root@Debug ~]# useradd phpdebug
[root@Debug ~]# usermod -g apache phpdebug
[root@Debug ~]# passwd phpdebug
Changing password for user phpdebug.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@Debug ~]# mkdir -p /www/php/test
[root@Debug ~]# chmod -R 775 /www/php/test
[root@Debug ~]# chown -R phpdebug:apache /www/php/test
[root@Debug www]# mount --bind /www/php/test /home/phpdebug
并为其添加一个配置文件:
# vsftpd per-user basis config file (override of any config option specified
# in the vsftpd server config file)
#
# TEMPLATE
#
# User test - Description for user test
#
# Set local root
local_root=/home/phpdebug
# Disable any form of FTP write command.
# Allowed values: YES/NO
write_enable=YES
allow_writeable_chroot=YES
最后重启vsftpd服务。
八、配置PhpStorm
新建完一个项目之后,我们按ctrl+alt+s进入Settings,定位到Languages&Frameworks-PHP-Debug 选项,在右边将Xdebug项的Debug port修改为我们为Xdebug配置的9010,勾上“Can accept external connections” 选项:
然后点击Debug下面的Servers,添加调试服务器:
然后为项目创建一个运行/调试配置,点击IDE右上角的Add Configuration,在弹出的 Run/Debug Configurations 页面中,点击“+”号,添加一个PHP Web Page,我们指定要被调试的主页是test.php:
在调试或运行这个配置前,我们还需要将本地的代码变更同步到调试服务器上,因而我们需要点击下方的“+”号添加一个上传文件的动作:
在Upload to Remote Host页面,因为目前没有配置同步方式,所以页面为空:
点击Server右边的“…”按钮,添加并配置为调试服务器的ftp:
点击OK后,回到Upload to Remote Host页面,选中配置好的ftp服务器,同时点击右侧“+”号添加本地要上传到ftp的目录,即项目所在目录:
确定好后应用保存该运行配置,新建一个test.php并下断点,如下:
shift+f9执行调试:
支持常用的调试操作,如单步步入,单步步过,查看变量,计算表达式等,网页成功执行页面如下: