用Visual Studio Code Debug世界上最好的語言(Mac篇)
首先,你要有台Macbook Pro,接着才繼續看這個教程.
PS:Windows用戶看這里用Visual Studio Code Debug世界上最好的語言
brew 環境准備
見brew.sh,或者
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
PHP7 + nginx + php-fpm + xdebug
PHP7
brew install php@7.1
安裝完了之后看下安裝路徑:
where php;
##➜ ~ where php
## /usr/local/opt/php@7.1/bin/php
## /usr/bin/php
一般php.ini在/usr/local/etc/php/7.1
ls /usr/local/etc/php/7.1
#conf.d pear.conf php-fpm.conf php-fpm.d php.ini
待會我們配置xdebug和php-fpm的時候會用到這個這些配置文件的,先跳過
xdebug安裝
本來其實一句brew install php71-xdebug --without-homebrew-php就完事的,誰知道homebrew-php最近被移除了,所以就尷尬了...
手動去下載xdebug然后配置吧.下載頁面:https://xdebug.org/files/
選擇自己要安裝的版本,我這里選了2.6.
# 創建一個你喜歡的路徑存放,我放在了~/tool/目錄下;
mkdir tool;
wget https://xdebug.org/files/xdebug-2.6.0.tgz;
# 解壓
tar xvzf xdebug-2.6.0.tgz;
cd xdebug-2.6.0;
# 初始化php模塊
phpize;
# 生成對應的so文件
# ./configure --enable-xdebug --with-php-config=PHP安裝路徑/bin/php-config;
./configure --enable-xdebug --with-php-config=/usr/local/Cellar/php@7.1/7.1.17/bin/php-config;
# 上一步正常執行完畢之后會在xdebug-2.6.0/modules/文件夾下生成xdebug.la和xdebug.so,待會我們在php.ini中配置xdebug會用到這個文件
安裝nginx
brew install nginx
配置nginx.conf
安裝完成之后開始配置nginx,首先創建一堆需要用到的文件件.
mkdir -p /usr/local/var/logs/nginx
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 777 /var/www
#作者:GQ1994
#鏈接:https://www.jianshu.com/p/a642ee8eca9a
#來源:簡書
#著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
然后vim /usr/local/etc/nginx/nginx.conf 輸入以下內容:
user root wheel; #默認的是nobody會導致403
worker_processes 1;
error_log /usr/local/var/logs/nginx/error.log debug;
pid /usr/local/var/run/nginx.pid;
events {
worker_connections 256;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/var/logs/access.log main;
sendfile on;
keepalive_timeout 65;
port_in_redirect off;
include /usr/local/etc/nginx/sites-enabled/*;
}
設置nginx php-fpm配置文件
vim /usr/local/etc/nginx/conf.d/php-fpm
修改為(沒有則創建)
#proxy the php scripts to php-fpm
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include /usr/local/etc/nginx/fastcgi.conf;
}
創建默認虛擬主機default
vim /usr/local/etc/nginx/sites-available/default輸入:
server {
listen 80;#如果80被用了可以換成別的,隨你開心
server_name www.qilipet.com admin.qilipet.com;
root /var/www/pet/public;
access_log /usr/local/var/logs/nginx/default.access.log main;
index index.php index.html index.htm;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
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;
}
}
此部分內容基本來自GQ1994:mac下配置php、nginx、mysql、redis
配置php.ini
回到我們的/usr/local/etc/php/7.1文件夾
在php.ini中加入xdebug配置
[xdebug]
;zend_extension="剛剛的xdebug路徑/modules/xdebug.so"
zend_extension="~/tool/xdebug-2.6.0/modules/xdebug.so"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
;默認的9000已經被php-fpm占用了,切記換一個端口
xdebug.remote_port = 9001
xdebug.scream = 0
xdebug.show_local_vars = 1
重啟一下php-fpm和nginx,看一下php是不是都正常跑起來了.
VS Code配置
User Settings配置PHP目錄
"php.executablePath": "/usr/local/opt/php@7.1/bin/php"
安裝php debug插件
安裝完成之后配置一下launch.json
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9001 //默認是9000已經被php-fpm占用,上一步我們配置遠程端口是9001
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9001 //默認是9000已經被php-fpm占用,上一步我們配置遠程端口是9001
}
]
}
然后就愉快debug最好的語言吧!