Homestead 以 root 身份运行命令

Homestead 以 root 身份运行命令

在我的 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: [email protected]
  - 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 提供细粒度的配置。

但是当我运行时,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 盒子的设置? (例如 xdebug 配置之一)

答案1

现在可以选择以 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

相关内容