Shell-Script

Homestead 以 root 身份執行命令

  • September 13, 2018

在我的 Homestead 配置中,我嘗試讓我的after.sh腳本自動配置 xdebug,以便通過框更新或重新創建能夠為它實施我的配置,而無需一直手動重做。

腳本如下:

#!/bin/sh

echo "Configuring Xdebug"
ip=$(netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10)
xdebug_config="/etc/php/$(php -v | head -n 1 | awk '{print $2}'|cut -c 1-3)/mods-available/xdebug.ini"

echo "IP for the xdebug to connect back: ${ip}"
echo "Xdebug Configuration path: ${xdebug_config}"
echo "Port for the Xdebug to connect back: ${XDEBUG_PORT}"
echo "Optimize for ${IDE} ide"
first_line=$(head -n1 ${xdebug_config})

if [ $IDE=='atom' ]; then
 echo "Configuring xdebug for ATOM ide"
 sudo cat <<EOL >${xdebug_config}
${first_line}
xdebug.remote_enable = 1
xdebug.remote_host=${ip}
xdebug.remote_port = ${XDEBUG_PORT}
xdebug.max_nesting_level = 1000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true
xdebug.remote_log=xdebug.log
EOL
fi

Homestead.yml的如下:

ip: 192.168.10.10
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
timeout: 120

keys:
   - ~/.ssh/id_rsa
folders:
   -
       map: /home/pcmagas/Kwdikas/php/apps/ellakcy_member_app/
       to: /home/vagrant/code
sites:
   -
       map: homestead.test
       to: /home/vagrant/code/web
       type: symfony

databases:
   - homestead
   - homestead-test

variables:
 - key: database_host
   value: 127.0.0.1
 - key: database_port
   value: 3306
 - key: database_name
   value: homestead
 - key: database_user
   value: homestead
 - key: database_password
   value: secret
 - key: smtp_host
   value: localhost
 - key: smtp_port
   value: 1025
 - key: smtp_user
   value: no-reply@example.com
 - key: IDE
   value: atom
 - key: XDEBUG_PORT
   value: 9091

name: ellakcy-member-app
hostname: ellakcy-member-app

我已經設置了以下額外的環境變數:

 - key: IDE
   value: atom
 - key: XDEBUG_PORT
   value: 9091

所以我可以為 xdebug 提供一個細粒度的配置。

但是當我執行Iget 時vagrant provision出現以下錯誤(為了節省空間我做了 nbot 把整個輸出):

ellakcy-member-app:/tmp/vagrant-shell:37:/tmp/vagrant-shell:無法創建/etc/php/7.2/mods-available/xdebug.ini:權限被拒絕

這是由他的命令引起的:

sudo cat <<EOL >${xdebug_config}
${first_line}
xdebug.remote_enable = 1
xdebug.remote_host=${ip}
xdebug.remote_port = ${XDEBUG_PORT}
xdebug.max_nesting_level = 1000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true
xdebug.remote_log=xdebug.log
EOL

所以我想知道如何自動配置 Homestead Vagrant box 的設置?(例如 xdebug 配置一)

現在可以選擇以 root 身份執行整個腳本。通過將以下選項更改為Vagrantfile

   config.vm.provision "shell", path: afterScriptPath, privileged: **false**, keep_color: true

   config.vm.provision "shell", path: afterScriptPath, privileged: **true**, keep_color: true

但它將無法從Homestead.yml.

引用自:https://unix.stackexchange.com/questions/468638