Map a custom domain with Laravel Valet

Laravel Valet serves *.dev domains by default. But what if you need to host hack the production domain to your project? As it turns out, it’s easier than you’d expect it to be.

First, add the record for the domain to your /etc/hosts file:

sudo echo '127.0.0.1 my-project.com' >> /etc/hosts

Then, in your parked directory, create a symlink with the domain you want to serve:

$ cd ~/projects/
$ valet park
This directory has been added to Valet's paths.
$ mkdir my-project
# my-project.dev is now served by Valet
$ ln -s my-project my-project.com
# my-project.com is now served by Valet

Valet’s Nginx configuration handles all requests by default, so don’t bother creating a custom Nginx config file. Having it serve your specific domain is only a matter of making sure Valet knows where to find your project directory, given its auto-discovery behavior.

Use Jetpack’s Photon image resizing in local development

In order to dynamically resize an image, Photon needs to be able to fetch the image file over HTTP. Jetpack disables Photon when JETPACK_DEV_DEBUG is defined because the assumption is that your local development domain isn’t accessible from the public internet. But, if you configure Photon in your local environment, you can tell Jetpack to enable all of Photon’s features too.

Setup Photon

First, check out the Photon codebase to a location served by your VIP Quickstart or Salty WordPress Vagrant:

svn co https://code.svn.wordpress.org/photon/ photon.dev

You’ll need to set up a record within your web server (Nginx or Apache) to serve the domain. If you aren’t using dnsmasq for local development, you’ll need to edit /etc/hosts on your local machine too.

Install Gmagick

You probably don’t have Gmagick installed, which Photon uses to resize images, so you’ll need to install it. For me, this was:

apt-get install php5.6-gmagick
service php5.6-fpm restart

Add a couple of filters

The last step is to add a couple of filters to a local mu-plugin or similar:

add_action( 'jetpack_modules_loaded', function(){
include( Jetpack::get_module_path( 'photon' ) );
add_filter( 'jetpack_photon_development_mode', '__return_false' );
}, 11 );

add_filter( 'jetpack_photon_domain', function( $domain ){
return 'http://photon.dev';
});

dnsmasq to handle *.dev domains for local development

Do you use *.dev domains for local development? Me too.

And, since the dawn of my existence, I’ve edited /etc/hosts to add new *.dev domains — an extra 30 seconds of mental overhead at the beginning of every project, and pure hell when I’m working on a project with unlimited subdomains.

The solution? dnsmasq, which has existed forever and seems to work swimmingly. Here’s the setup instructions for Mac. If you are like me, don’t be like me and edit /etc/hosts for years. Install dnsmasq today.