phpMyAdmin on Laravel Homestead

What is Laravel Homestead?

“Laravel Homestead is an official, pre-packaged Vagrant “box” that provides you a wonderful development environment without requiring you to install PHP, HHVM, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!” – laravel.com

That’s great!

But how can I reach my databases via phpMyAdmin?

Unfortunately it doesn’t come with phpMyAdmin out of the box. You’re forced to setup a local PMA install, or use an external application like Sequel Pro.
In this short blog post I’ll teach you how to setup phpMyAdmin on your Laravel Homestead box. Assuming you followed all the instructions on laravel.com to setup your box, start by SSH’ing into it:

homestead ssh

Install phpMyAdmin via apt-get:

sudo apt-get install phpmyadmin

(do NOT select apache2 or lighttpd. Just continue without them).

Next, make a symlink between the PMA directory and your web root:

sudo ln -s /usr/share/phpmyadmin/ /home/vagrant/Sites/phpmyadmin
cd ~/Sites && serve phpmyadmin.app /home/vagrant/Sites/phpmyadmin

Go back to your local environment and modify the hosts file like this:

127.0.0.1  phpmyadmin.app

phpMyAdmin is now reachable via http://phpmyadmin.app:8000

Or like this:

192.168.10.10  phpmyadmin.app

phpMyAdmin is now reachable via http://phpmyadmin.app

That’s it!

Pushing Laravel logs to Loggly

logo_loggly

Laravel uses the Monolog logging library for logging, however, it’s saving all logs to a local directory by default. Not a very useful thing in a production environment.

That’s where Loggly comes into play. Loggly acts as a central (cloud)platform that can receive logs from a multitude of platforms such as php frameworks (Laravel being the one), operating systems, their own Loggly API, and a lot of other frameworks and services. In this short tutorial I’ll show you how to implement Loggly into your multi-environment Laravel project.

1) Update composer

First we need to update composer so we’re 100% that we’re using the latest Monolog version, because older versions don’t support Loggly.

composer self-update
composer update

2) Setup Loggly account

Go to https://www.loggly.com and create a (free) account.

3) Add Loggly credentials to your Laravel environment

Now the nice thing about Loggly is that it’s pretty easy to configure. Go to https://www.loggly.com/tokens and setup a token for your project. Do this to keep things seperated. Got a big project? Then you might want to use source groups as well.
Now create a new file in your Laravel folder: /app/config/services.php and add this:

return array(
	// credentials for loggly.com
	'loggly' => array(
		'key'	=> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx',
		'tag'	=> 'ProjectName_' .strtolower(App::environment()),
	),
);

Now if you’re using Laravel’s environment configuration you should do this:

return array(
	// credentials for loggly.com
	'loggly' => array(
		'key'	=> getenv('services.loggly.key'),
		'tag'	=> 'ProjectName_' .strtolower(App::environment()),
	),
);

And then store it in a similar way in your .env.local.php file, or on Laravel Forge.

4) Install the logging code

Now this is where the magic happens. Open up /app/start/global.php and find the “Application Error Logger” section. Replace it with this code:

/*
|--------------------------------------------------------------------------
| Application Error Logger
|--------------------------------------------------------------------------
|
| Here we will configure the error logger setup for the application which
| is built on top of the wonderful Monolog library. By default we will
| build a rotating log file setup which creates a new file each day.
|
*/
 
$logFile = 'log-'.php_sapi_name().'.txt';
Log::useDailyFiles(storage_path().'/logs/'.$logFile);
 
/*
 * Setup Loggly Handler
 */
$handler = new \Monolog\Handler\LogglyHandler(Config::get('services.loggly.key'),\Monolog\Logger::DEBUG);
$handler->setTag(Config::get('services.loggly.tag'));
 
$logger = Log::getMonolog();
$logger->pushHandler($handler);

This way, every environment will have it’s own tag and you can easily see in the Loggly dashboard when something happens on your dev/staging environment, or if it’s on live. I also strongly advise to setup alerts within Loggly so you get notified once you got a couple of 500 errors coming around.
Now the nice thing about Loggly is that you can go back in time and easily see in the stats how many errors occured in the past 24 hours, good luck doing that with log files! This is especially useful for keeping a close eye on cronjobs and incoming API calls.

5) Testing

Throw some 404 errors by opening up a page in your project and appending some random characters. Wait a couple of minutes and there we go. You should see something like this:
Screen Shot 2014-10-02 at 11.05.36

Also worth noting is that all other Laravel logging functions will also push to Loggly.

More info about Laravel’s logging features: http://laravel.com/docs/4.2/errors#logging
More info about Loggly: https://www.loggly.com

Laravel 4 released

Great news! My favorite PHP framework Laravel finally released version 4 after 5 public beta’s and months of waiting. Why am I so happy? Well.. it makes my job as a developer a lot simpler compared to my Zend Framework days.

Here’s why you must try Laravel

  • – Smart framework: write less code (faster + easier)
  • – Works with composer, the PHP plugin tool
  • – Filters, events, rest, built-in auth, … it has it all out of the box
  • – Elequent ORM: the smartest and easiest PHP ORM today!
  • – Unit testing for all components

So what’s new in Laravel 4?

  • – L4 now runs on PHP >= 5.3.7
  • – PSR-0 code standards are used
  • – Composer (packagist.org) instead of Laravel bundles
  • – Lot’s of small but convenient little tweaks and options
  • – Mail is now built in
  • – L4 is built with Unit Testing in mind
  • – Great community!
  • – The command line tool (artisan) is now more powerful

 

Here’s what changed in L4:
https://github.com/laravel/framework

Laravel 4 introduction

Download Laravel @ http://laravel.com/, and make sure to follow them on Twitter: https://twitter.com/laravelphp

Deploying PHP projects with Jenkins on OS X

jenkins

Continuous deployment, automated unit testing, code analysis & reports, git repo’s, Laravel 4 using Composer. This must be a dream project right? Well, yes, if you have it working correctly. It took me a while to get everything working together but now it works like a charm. I chose to deploy via my macbook, but some people might find it handier to install this setup on a public webserver so they can bind their commits to automated deployments, use it as a team, ….
Anyway, this tutorial will be about deployment for php projects (Laravel 4 in particular) on OS X. I’m assuming you already have a webserver up & running (I’m using MAMP).

1) Install Jenkins

Go to http://jenkins-ci.org/ and install Jenkins for Mac OS X, make sure to use seperate new user. Here’s a great tutorial about this: http://colonelpanic.net/2011/06/jenkins-on-mac-os-x-git-w-ssh-public-key/

Addons:
– Jenkins in your dock: https://github.com/stisti/jenkins-app
– JenkinsMobi (iOS app): http://hudson-mobi.com/

2) Configure Jenkins

I strongly recommend you to follow this tutorial http://jenkins-php.org/ as it has almost everything documented for deploying php apps through Jenkins. My setup is actually based on this documentation.

Download the Jenkins plugins listed on the jenkins-php.org website. I’m using these:
– Jenkins Mailer Plugin
– External Monitor Job Type Plugin
– Ant Plugin
– Static Analysis Utitlities
– Checkstyle Plug-in
– Credentials Plugin
– Jenkins CVS Plug-in
– Duplicate Code Scanner Plug-in
– Jenkins Email Extension Plugin
– FTP publisher plugin
– Jenkins GIT client plugin
– Jenkins GIT plugin
– GitHub API Plugin
– Github plugin
– HTML Publisher plugin
– Jenkins JDepend Plugin
– Plot plugin
– PMD Plug-in
– Publish Over FTP
– xUnit Plugin

3) Configure a new project

Install the jenkins-php’s job template tutorial. When you want to create a new project, simply copy the job template project and modify it as you want.

4) Connect your git repo

Connect your git repo to jenkins. Make sure to use the git protocol (git@github.com:username/repo.git) if you setup key pairs. If you’re using the http:// github url it will keep asking for credentials even though your key pairs are correctly installed.

5) Install Composer

Use this tutorial to install Composer: http://getcomposer.org/doc/01-basic-usage.md#installation

And put it in your bin directory so every user can use this great piece of software:

cp composer.phar /usr/local/bin/composer

If you’re using composer you will at one point notice it will clone your depency repo’s using git everytime you build your application. And this gave me some Jenkins problems.. Even though the normal ‘composer update’ worked like a charm under my jenkins user, Jenkins itself was giving problems.

[exec] [RuntimeException]
[exec] Failed to clone http://github.com/nicolas-grekas/Patchwork-UTF8.git, git was not found, check that it is installed and in your PATH env.
[exec]
[exec] sh: git: command not found

more @ http://jenkins.361315.n4.nabble.com/git-not-found-with-jenkins-composer-php-td4660806.html

To fix this, go to http://localhost:8080/configure -> Global configuration -> Environment variables
And add this:
name = PATH
value = /usr/local/git/bin:$PATH

As you can see in the following picture, composer is now fully working in our build process:
1JMuzxRzPabYasaXePdvw9d6GXHitPljLOciovw

6) Install some additional PHP packages

Now of course we want code statistics, automated unit testing, auto generated API documentation, coverage reports, etc so we need to install some extra tools:

pear (http://pear.php.net/)
Follow this quick guide to install pear: https://gist.github.com/macek/1301527

phpdox (https://github.com/theseer/phpdox)

sudo pear config-set auto_discover 1
sudo pear install pear.netpirates.net/phpDox-0.4.0

phpunit (http://www.phpunit.de/manual/3.0/en/installation.html)

sudo pear channel-discover pear.phpunit.de
sudo pear install phpunit/PHPUnit

phploc (https://github.com/sebastianbergmann/phploc)

sudo pear config-set auto_discover 1
sudo pear install pear.phpunit.de/phploc

pdepend (http://pdepend.org/documentation/getting-started.html)

sudo pear channel-discover pear.pdepend.org
sudo pear install pdepend/PHP_Depend-beta

phpcb

sudo pear channel-discover pear.phpqatools.org
sudo pear install --alldeps phpqatools/PHP_CodeBrowser

7) Create your build file

Next we create our build.xml file and we make sure it’s executed correctly by Jenkins (check Project -> Building steps). Put this file in your /jobs/PROJECT directory.

Here’s mine: http://paste.laravel.com/mLw

Note: you can test the build file manually by executing this command. So no need to build via Jenkins and download 24 composer repo’s before it starts executing the ant build file.

ant -f build.xml -v

You will also need some additional XML config files:

PROJECT/phpcs.xml -> http://paste.laravel.com/mLB

PROJECT/phpdox.xml -> http://paste.laravel.com/mLA

PROJECT/phpmd.xml -> http://paste.laravel.com/mLz

You will also have to alter your Laravel’s phpunit.xml file: http://paste.laravel.com/mLC (the one in your root folder)

8) Build your application

Now try to build your application through Jenkins. Enjoy!

Here’s how it should look like: http://paste.laravel.com/mMn

And some fancy screenshots:
1FxhZRcTJZS4fgVepTvJLoAUx_VwyqjI0xBQTBw

1BWoJILiDN0QjSfOlgQmSwLRB3VsH0FOt58ym3g

These are just some Jenkins screenshots.. our build file is also generating documentation, code coverage, getc which you can find in PROJECT/workspace/build/ and PROJECT/build

Interesting links:
http://jenkins-php.org/
http://erichogue.ca/2011/05/php/continuous-integration-in-php/