laravel 如何在 vagrant up 上自动启用 Homestead 中的 php 扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40641526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:45:24  来源:igfitidea点击:

How to automatically enable php extensions in Homestead on vagrant up

phplaravelvagranthomesteadphp-extension

提问by Wesley Smith

Im using Laravel 5.3 in Homestead with Vagrant 1.8.7 running on VirtualBox.

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

I have need to enable some php extensions.

我需要启用一些 php 扩展。

I know that I could ssh into the box and edit the php.ini to enable the extension but this seems like a very anti-vagrant way to do this.

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

I want to tell Vagrant to provision the box with specific php extensions enabled so that I can simply call vagrant up --provisionand the box will be ready to go (kinda the point of vagrant right?)

我想告诉 Vagrant 配置启用了特定 php 扩展的框,这样我就可以简单地调用vagrant up --provision,框就可以使用了(有点流浪者的观点,对吗?)

So, How can we automatically enable php extensions in Homestead on vagrant up?

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

采纳答案by Wesley Smith

After some tinkering, the below is what I came up with. I make no assurances that this is the right way to do itonly that, in my case, it seems to be working:

经过一些修补,下面是我想出的。我不保证这是正确的方法,只是在我的情况下,它似乎有效:

Find the after.shthat was generated when you installed homestead. For me, on Mac El Capitain, the file is created at ~/.homestead/after.sh, I imagine there is a .batin a similar location on windows.

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

Do notmake the mistake of editing ~/Homestead/src/stubs/after.sh, thats the template file from the homestead installation, not your actual generated copy.

不要犯编辑错误~/Homestead/src/stubs/after.sh,那是 homestead 安装中的模板文件,而不是您实际生成的副本。



Edit after.sh

编辑 after.sh

Add the below lines to after.sh(this is my whole file, only the first 5 comment lines were in the default file):

将以下几行添加到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

If you dont psychically know the exact name for the extension you need (I didnt) you can use sudo apt-cache search php7-*or similar to list the available ones

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



vagrant destroy

流浪破坏

Now, if you have homestead up, in the terminal, cdto your Homestead dir, for me cd ~/Homesteadand then run vagrant destroy

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



vagrant up

流浪起来

While inside /Homesteadrun vagrant up --provision

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



Check install

检查安装

To check that the extensions installed correctly, while inside /Homesteadrun these two commands:

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

vagrant ssh

vagrant ssh

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

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

My output (33 and 61 were added):

我的输出(添加了 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
)


Like I stated at the beginning, I cant say this is the right way, but it's working for me so far.

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

If anyone sees a flaw in this approach, feel free to tell me Im doing it all wrong :)

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

回答by Nikola Kirincic

you should first log onto Homestead server using ssh ( probably you know this already - "vagrant ssh").

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

then go to "/etc/php/7.0/fpm/" there is also for cli on this location "/etc/php/7.0/cli/" edit it with "sudo vi php.ini" ( esc and :wq to save changes ).

然后转到“/etc/php/7.0/fpm/”这个位置还有用于cli的“/etc/php/7.0/cli/”用“sudo vi php.ini”编辑它(esc和:wq以保存变化 )。

then you should restart nginx: "sudo nginx -s reload"

那么你应该重新启动nginx:“sudo nginx -s reload”

and after that, restart php-fpm: "sudo service php7.0-fpm restart"

之后,重新启动 php-fpm:“sudo service php7.0-fpm restart”

if you are not sure if it is php 5.x or 7.x on your homestead, use "find / -name php.ini" to find php.ini, you will probably get 2 or 3 results.

如果你不确定你家是 php 5.x 还是 7.x,使用“find / -name php.ini”来查找 php.ini,你可能会得到 2 或 3 个结果。

回答by Wiwwil

In case there's still a need for this :

如果仍然需要这样做:

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

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

=> Run the 3 first commands :

=> 运行前 3 个命令:

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

Or simply add to after.sh:

或者简单地添加到after.sh:

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