【问题标题】:How to automatically enable php extensions in Homestead on vagrant up如何在 vagrant up 上自动启用 Homestead 中的 php 扩展
【发布时间】:2017-03-31 04:56:30
【问题描述】:

我在 Homestead 中使用 Laravel 5.3,在 VirtualBox 上运行 Vagrant 1.8.7。

我需要启用一些 php 扩展。

我知道我可以通过 ssh 进入该框并编辑 php.ini 以启用扩展,但这似乎是一种非常反流浪的方式。

我想告诉 Vagrant 为盒子配置启用了特定的 php 扩展,这样我就可以简单地调用 vagrant up --provision,盒子就可以使用了(有点 vagrant 的意思吧?)

那么,如何在 vagrant up 上自动启用 Homestead 中的 php 扩展?

【问题讨论】:

    标签: php laravel vagrant homestead php-extension


    【解决方案1】:

    经过一番修改,下面是我想出的。我不保证这是正确的方法,就我而言,它似乎有效:

    找到installed homestead 时生成的after.sh。对我来说,在 Mac El Capitain 上,该文件是在 ~/.homestead/after.sh 创建的,我想在 windows 上的类似位置有一个 .bat。

    不要错误地编辑~/Homestead/src/stubs/after.sh,这是来自宅基地安装的模板文件,而不是您实际生成的副本。


    编辑after.sh

    将以下行添加到after.sh(这是我的整个文件,默认文件中只有前 5 行注释):

    #!/bin/sh
    
    # If you would like to do some extra provisioning you may
    # add any commands you wish to this file and they will
    # be run after the Homestead machine is provisioned.
    
    # in the below --assume-yes is to avoid confirms [y/N]
    # DEBIAN_FRONTEND=noninteractive is to avoid a big menu asking if it's ok to 
    # overwrite the php.ini file, may make --assume-yes redundant, not sure
    
    # run apt-get update first, without it I was getting errors not finding the extensions 
    sudo DEBIAN_FRONTEND=noninteractive apt-get --assume-yes update
    
    # load any extensions you like here 
    sudo DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install php-xdebug 
    sudo DEBIAN_FRONTEND=noninteractive apt-get --assume-yes install php7.0-ldap # update to php7.2-ldap if using php 7.2 etc...
    
    # enable xdebug via cli
    sudo phpenmod -s cli xdebug
    
    # restart php and nginx
    sudo service php7.3-fpm restart && sudo service nginx restart
    

    如果您不知道所需扩展的确切名称(我不知道),您可以使用 sudo apt-cache search php7-* 或类似名称列出可用的扩展


    流浪破坏

    现在,如果你有宅基地,在终端,cd 到你的宅基地目录,我是cd ~/Homestead,然后运行vagrant destroy


    流浪

    在/Homestead 内部运行vagrant up --provision


    检查安装

    要检查扩展是否正确安装,在 /Homestead 中运行以下两个命令:

    vagrant ssh

    php -r "print_r(get_loaded_extensions());"

    我的输出(添加了 33 和 61):

    DoDSoftware:Homestead DOoDSoftware$ vagrant ssh
    Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-22-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com/
    vagrant@homestead:~$ php -r "print_r(get_loaded_extensions());"
    Array
    (
        [0] => Core
        [1] => date
        [2] => libxml
        [3] => openssl
        [4] => pcre
        [5] => zlib
        [6] => filter
        [7] => hash
        [8] => pcntl
        [9] => Reflection
        [10] => SPL
        [11] => session
        [12] => standard
        [13] => mysqlnd
        [14] => PDO
        [15] => xml
        [16] => apcu
        [17] => apc
        [18] => bcmath
        [19] => calendar
        [20] => ctype
        [21] => curl
        [22] => dom
        [23] => mbstring
        [24] => fileinfo
        [25] => ftp
        [26] => gd
        [27] => gettext
        [28] => iconv
        [29] => igbinary
        [30] => imap
        [31] => intl
        [32] => json
        [33] => ldap
        [34] => exif
        [35] => mcrypt
        [36] => msgpack
        [37] => mysqli
        [38] => pdo_mysql
        [39] => pdo_pgsql
        [40] => pdo_sqlite
        [41] => pgsql
        [42] => Phar
        [43] => posix
        [44] => readline
        [45] => shmop
        [46] => SimpleXML
        [47] => soap
        [48] => sockets
        [49] => sqlite3
        [50] => sysvmsg
        [51] => sysvsem
        [52] => sysvshm
        [53] => tokenizer
        [54] => wddx
        [55] => xmlreader
        [56] => xmlwriter
        [57] => xsl
        [58] => zip
        [59] => memcached
        [60] => blackfire
        [61] => Zend OPcache
        [62] => xdebug
    )
    

    就像我在开头所说的那样,我不能说这是正确的方法,但到目前为止它对我有用。

    如果有人发现这种方法有缺陷,请随时告诉我我做错了:)

    【讨论】:

    • 太好了,谢谢!我唯一改变的是使用vagrant reload --provision 而不是destroy 和up,以避免丢失我的数据库。
    【解决方案2】:

    如果仍然需要这个:

    => https://guides.wp-bullet.com/install-apcu-object-cache-for-php7-for-wordpress-ubuntu-16-04/

    => 运行前 3 个命令:

    sudo apt-get update
    sudo apt-get install php7.0-apcu -y
    sudo service php7.0-fpm restart
    

    或者干脆添加到after.sh:

    sudo apt-get install php7.x-apcu -y
    

    【讨论】:

    • 虽然准确,但这种做法没有抓住重点。最初的需求是在 vagrant up 上自动启用 Homestead 中的 php 扩展。而这只是手动安装。
    • 是的。我将其理解为“在我启动 vagrant 时使其可用”。不像“安装时准备好”。
    【解决方案3】:

    您应该首先使用 ssh 登录 Homestead 服务器(可能您已经知道这一点 - “vagrant ssh”)。

    然后转到“/etc/php/7.0/fpm/” 在这个位置也有 cli “/etc/php/7.0/cli/” 使用“sudo vi php.ini”编辑它(esc 和 :wq 保存更改)。

    那么你应该重启nginx:

    sudo nginx -s reload"
    

    然后重启php-fpm:

    sudo service php7.0-fpm restart" 
    

    如果您不确定您的家园是 PHP 5.x 还是 7.x,请使用

    find / -name php.ini
    

    找到 php.ini——你可能会得到 2 或 3 个结果。

    【讨论】:

    • 感谢您的宝贵时间。我知道如何手动启用扩展。但是,我不想每次都这样做vagrant up。我需要能够在配置过程中自动添加扩展,以便在执行 vagrant up --provision 时正确设置
    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 2017-04-25
    • 2020-02-22
    • 2015-05-14
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多