13

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

I have need to enable some php extensions.

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.

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

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

Wesley Smith
  • 19,401
  • 22
  • 85
  • 133

3 Answers3

13

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

Find the after.sh that 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 .bat in a similar location on windows.

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


Edit 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):

#!/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


vagrant destroy

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


vagrant up

While inside /Homestead run vagrant up --provision


Check install

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

vagrant ssh

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

My output (33 and 61 were added):

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 :)

Community
  • 1
  • 1
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • 1
    Great, thanks for this! The only thing I changed was use `vagrant reload --provision` instead of `destroy` and `up`, to avoid losing my databases. – Sygmoral Feb 11 '19 at 12:12
1

you should first log onto Homestead server using ssh (probably you know this already - "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).

then you should restart nginx:

sudo nginx -s reload"

and after that, 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.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
Nikola Kirincic
  • 3,651
  • 1
  • 24
  • 28
  • 2
    Thanks for your time. Im aware of how to enable the extension manually. However, I dont want to have to do this every time I do `vagrant up`. I need to be able to automate adding the extension as part of the provisioning process so that it's all set up properly when doing `vagrant up --provision` – Wesley Smith Nov 16 '16 at 22:08
1

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/

=> Run the 3 first commands :

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

Or simply add to after.sh:

sudo apt-get install php7.x-apcu -y
befabry
  • 672
  • 5
  • 13
  • While accurate, this kind of misses the mark. The original need was specifically to _automatically enable php extensions in Homestead on vagrant up_. while this is only manually installing the same. – Wesley Smith Jun 19 '19 at 13:55
  • True. I understood it as "Make it available when I launch the vagrant". Not as "Get it ready when installing". – befabry Jun 20 '19 at 14:14