Automate everything! – The power of puPHPet

As you readers probably know, I can’t stand XAMPP and MAMP, being two steaming piles of crap, and have long advocated that you set up VirtualBox & Vagrant, then head over to, fill in the forms to configure your VM,  generate the config.yaml, and then unzip it and run ‘vagrant up’ to install it. Brilliant so far.

Yesterday I had a total downer of a day, trying to run an old legacy PHP 5.3 app. PuPHPet doesn’t have the EOL PHP 5.3, so at first I settled as a one off for MAMP, but it was slow and horrible.

Then I thought, wait! If I don’t configure Apache or PHP in puphpet, I could get a box up and install 5.3 myself. That’s when I discovered the awesomeness of the puphpet/files folder.

The only thing I used in there was the ssh keys. But there are empty folders waiting for .sh files (shell scripts) to be dropped in.

So for this box, I created exec-once/ which contained the following:

yum -y install httpd php
yum -y install php-mysql php-devel php-gd php-pecl-memcache php-pspell php-snmp php-xmlrpc php-xml

Then upon running vagrant provision, it not only looked for changes in config.yaml, but it checks for changes in these files too!

I then made, and, which look like these:

echo "
Adding vhosts to /etc/httpd/conf/httpd.conf
echo "
<VirtualHost *:80>

   DocumentRoot /var/www/fife/web
   ServerName fife
   ErrorLog /var/www/fife/log/error.log

   <Directory "/var/www/fife">
      Options -Indexes +FollowSymLinks
      Order allow,deny
      Allow from all
      AllowOverride All

" >> /etc/httpd/conf/httpd.conf

And …

mysql -u root --password=123 --database=fortdev < /var/www/fife/data/sql_scripts/symf_fortdev.sql

I take it by now you get the idea! So now you can totally destroy your VM, and put any customisations in these shell scripts, so your full setup can be back up in 5 minutes flat with a vagrant up and vagrant provision!!!

You can then also start thinking about using puPHPet for deploying your setup to your production server 🙂 There’s a vagrant plugin called Vagrant Managed Servers, which will take care of that for you. . I haven’t looked at it yet, but of course you can expect a blog post on it here when I figure it all out!!

Installing Ansible

Ansible is a bit like puPHPet, in that it provisions Vagrant boxes etc. My latest project is using PPI Framework 2, and provisions with Ansible, so I had to get everything installed.

Essentially we just clone from the Github repo and install a few python things:

$ git clone git:// --recursive
$ cd ansible
$ source hacking/env-setup
$ sudo easy_install pip
$ sudo pip install paramiko PyYAML Jinja2 httplib2 six

And that should be you. Run ansible from the terminal in ay folder now and you should get all the available options etc. Have fun!

Debug Codeception from a remote Vagrant box in PHPStorm

Configuring this REALLY did my head in,but in reality there are only a few steps to hook up your puPHPet Vagrant box (however miss something and you’ll be left scratching your head)  so here’s the quick vibe:

First up create a file in your site root called debugrun.php or whatever:

require_once 'vendor/codeception/codeception/autoload.php';

use Symfony\Component\Console\Application;

$app = new Application('Codeception', Codeception\Codecept::VERSION);
$app->add(new Codeception\Command\Run('run'));


Then goto Edit configurations in the debug menu.

Screen Shot 2015-09-28 at 23.44.41
replacing ‘yoursite’ with a valid path of course. Ok that and then go into your preferences. Click on deployment and connect with SFTP using your ssh key  puphpet/files/dot/ssh/id_rsa :
Set the path mappings tab to /var/www/yoursite or whatever.

Screen Shot 2015-09-28 at 23.47.31

Goto Languages > PHP and add a remote interpreter:

Screen Shot 2015-09-28 at 23.50.33

Lastly go into Tools SSH Terminal and choose default remote interpreter. Now hit the debug button and codeception should launch! Dont ask me how to change the terminal colours!

puPHPet Box MySQL backup

As you all know by now, I love puPHPet. And I like tinkering with things. Which of course leads to the occasional breaking of things. And then I realised, okay, your sites files are being mounted into this virtual machine, what about the databases though?

The great thing about puPHPet and Vagrant is you can build a full server up from scratch in minutes. The one downside is that it cant be smart enough to uninstall things. If you add lines to your config.yaml it will install something, but removing those lines means puPHPet won’t even know it was there, so how would it know to uninstall it? The exceptions of course being the ones which have their own config.yaml entry, with an install: ‘1’ or install: ‘0’

Anyway, I generated me a new config and was about to blitz my old VM when the usual gut instinct checks kicked in, and the DB sprung to mind immediately. So I made a quick shell script (courtesy of a StackOverflow post) which makes individual sql files for each of your DB’s. I decided I would keep the script in my mounted sites folder in a bin directory. So hence it lives in /var/www/bin/



databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $USER -p$PASSWORD --databases $db > `date +%Y%m%d`.$db.sql

To back everything up, vagrant ssh into the machine, then (you’ll probably need to switch user to www-data) do the following:

sudo su www-data
cd /var/www/bin

Now you’ll find all your DB’s SQL files date stamped in the folder! You can continue to break, tweak, fix, and run your VM to your hearts content 😀

Fix Zend_Mail not sending on a puPHPet Vagrant box with Mailcatcher

I’ve done this a few times but always forgot and had to go into my files etc, so again I’m making a quick post to remind myself and possibly help others!

Quite simply, it’s just a matter of adding two lines to your application.ini. Use sendmail as the transport, not smtp, and change the port number to the same number set for mailcatcher in your puPHPet config.yaml (Usually 1025).

resources.mail.transport.type = sendmail
resources.mail.transport.port = 1025

Now you’ll find that the mail will send, and if you go to it should be sitting in your mailcatcher inbox! Have a nice day!

Upgrading a PuPHPet Vagrant Boxes’ PHP to 5.6

This is remarkably simple! First, BACK UP YOUR DB.

Backed up? Good. You dont need to back your web files up if they are being mounted from a shared folder.

Open your puPHPet config.yml, scroll down to the php section, and change version from ’55’ to ’56’, and save it.

Next, type vagrant destroy, to remove the old stuff, and then vagrant up, which will re-initialise the server with your shiny new PHP version!

HTML to PDF using wkhtmltopdf

Converting web pages to PDF? Sick of various libs and their various quirks and complexity? Wkhtmltopdf is a linux program that can reliably generate pdfs from html content. It’s as easy as running:

wkhtmltopdf google.pdf

from the command line. However, we want it in our web site! First off though, you’ll need it installed. If you are using puPHPet and vagrant, add the following in your config.yaml under server: packages:

    install: '1'
        - wkhtmltopdf

Then run vagrant provision. Standard linux servers will install by using yum or apt-get:

sudo apt-get install wkhtmltopdf

You can get the PHP wrapper class here:

From within your PHP, you can say:

        use mikehaert\wkhtmlto\Pdf;

        $pdf = new Pdf(array('tmpDir' => '/optional/tmp/folder/here'));

        // Save the PDF
        // $pdf->saveAs('/tmp/new.pdf');

        // ... or send to client for inline display
        if (!$pdf->send()) {echo $pdf->getError();}
        // ... or send to client as file download
        // $pdf->send('test.pdf');

You may get an error saying it cannot connect to the X server. If you get this, you probably don’t have X installed! I didn’t, it’s a web server, not a desktop machine!

In the event that you have no X server, go into the Command.php class, you will see the following option:

     * @var bool whether to enable the built in Xvfb support (uses xvfb-run)
    public $enableXvfb = false;

Change this to true, and try again. This time it should work!

If it still doesn’t, then possibly you also need to install xvfb, again with apt-get or yum, then add a startup script for it. I didn’t have to do any of this, but for those of you who may need to, complete the following steps:

sudo nano /etc/init.d/xvfb

XVFBARGS=":0 -screen 0 1024x768x24 -ac +extension GLX +render -noreset"
case "$1" in
    echo -n "Starting virtual X frame buffer: Xvfb"
    start-stop-daemon --chuid www-data --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
    echo "."
    echo -n "Stopping virtual X frame buffer: Xvfb"
    start-stop-daemon --chuid www-data --stop --quiet --pidfile $PIDFILE
    echo "."
    $0 stop
    $0 start
        echo "Usage: /etc/init.d/xvfb {start|stop|restart}"
        exit 1

exit 0

Enable the init script:

update-rc.d xvfb defaults 10

Start it up:

/etc/init.d/xvfb start

Check its running:

ps auxU www-data | grep [X]vfb

Now try reloading your page, and with a bit of luck you should see a PDF version of the google home page! Play around with it and have fun!

chmod, chown, & chgrp – Permissions on a PuPHPet Vagrant Box

PuPHPet and Vagrant are awesome. If you develop on Windoze, you are either off your head, or have no choice in the workplace. Right now I fall into the latter category. I noticed XAMPP installed. Yeah right. No chance. Anyway, using PuPHPet and Vagrant is a far better way of doing things regardless of your OS, as it means you can move your entire development environment anywhere without having to reconfigure anything! See my post for info on getting set up and running.

Anyway, it turns out that the files and directories for your sites are all owned by www-data, and are in the www-data group. I was trying to run a PHP CLI command, however, as the logged in user is vagrant and the permissions were only executable for the owner, I tried running chmod, and chgroup, and chown, with no success. Because these are mounted folders and not actually in the VM, altering the permissions the normal way is impossible.

The solution is to go into your VagrantFile, and add the following:

config.vm.synced_folder "./mysite/scripts", "/var/www/mysite/scripts",
        owner: "www-data",
        group: "www-data",
        mount_options: ["dmode=775,fmode=775"]

The first path given is relative to your host OS directory with your site folders inside, the second path is the path on the VM. dmode I’m guessing is the directory permissions, fmode is the file permissions. I keyed this in just before the ‘end’  line at the bottom of the VagrantFile.

To make the changes, you can type vagrant up (if you havent started yet), or vagrant reload. Permissions have now been changed!

One last thing. Once I was able to execute, I ran my script and got

: No such file or directory

This again is a stupid character encoding issue due to windows, and the way I sorted it was to cat the file in Putty, and select the text, nano a new file, paste it in, delete the original, and rename the new file. I didnt have to chmod anything, all files in here now default to 775.

Anyway, now that problem has been solved I can get back on with my work! Cheers! 😀


It appears that the Vagrant file has been broken up into smaller more specific parts. In a recent puPHPet Vagrant box, I had to paste it in the puphpet/vagrant/vagrantfile-local file,  right before the very last line (which just says end).