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 http://www.puphpet.com, 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/install-stuff.sh which contained the following:

#!/bin/bash
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 set-vhosts.sh, and import-database.sh, which look like these:

#!/bin/bash
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
  </Directory>

</VirtualHost>
" >> /etc/httpd/conf/httpd.conf

And …

#!/bin/bash
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. https://github.com/tknerr/vagrant-managed-servers . 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!!

Advertisements

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://github.com/ansible/ansible.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'));

$app->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 backupdbs.sh 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/backupdbs.sh

#!/bin/bash

USER="root"
PASSWORD="YOURPASSWORDHERE"

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
    fi
done

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
./backupdbs.sh

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 http://yoursitehere.dev:1080 it should be sitting in your mailcatcher inbox! Have a nice day!

Http Basic Auth with Apigility on a puPHPet Vagrant box

If you aren’t using the built in php server for development, and like me you are using a vagrant box configured by puPHPet, this will save you a lot of wasted time wondering why you get 401s and 403s when you aren’t expecting them.

In your vhost section , under the setenv option, we add a new setenvif option:

setenvif:           
     - 'Authorization "(.*)" HTTP_AUTHORIZATION=$1'

Without this option, the Authorization header is being stripped! Run vagrant provision, and suddenly everything should be working correctly. Now get on with building that API!

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!