Using Vagrant to get a new Laravel project running
In my last post I looked at creating custom Vagrant base box that has a working Nginx, PHP and MySQL setup all ready to go, making it easy to get started on new PHP projects with minimal configuration. Let’s give that a try now by using our custom base box as the basis of a new Laravel project. We’ll only need to write (or indeed clone) a few lines of Vagrant config code to get started!
Create a new Laravel project
Assuming you’ve already installed the laravel
binary file on your machine, make a new project:
laravel new laravel_test
Now inside your new laravel_test
project you should see a load of files - a brand new project you just created. Move into that and then let’s clone some Vagrant config files into Laravel’s app/config
directory. I’ve put these on GitHub so you can get them like this:
cd laravel_test
git clone https://github.com/paulherron/vagrant_laravel.git app/config/vagrant
There’s really not very much in what you just cloned – that’s the beauty of using a custom base box. As usual there’s a bootstrap.sh
file, contained in which are the shell commands to run when first setting up the Vagrant box. Because most of the required software is installed already, all that’s needed is to:
- Install the php5-mcrypt extension, required by Laravel.
- Move a custom Nginx configuration file into place, which has a few tweaks specific to a Laravel setup: namely that the root directory of the website will be the
public
directory of the Laravel project. - Restart Nginx etc. for good measure to ensure the above configuration changes have taken effect.
Vagrant expects to find a file called Vagrantfile
in the root directory. This was included in the files we just cloned, but we need to symlink it into place:
ln -s app/config/vagrant/Vagrantfile .
Now we can fire up the virtual machine and see what happens:
vagrant up
With a bit of luck you can now navigate to 192.168.50.2 (or at default.l if you set up a local testing domain in your /etc/hosts
file).
That’s about it! Do note though that if you want to use a database, you’ll need to enter the default database credentials into your Laravel project’s app/config/database.php
:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'default',
'username' => 'root',
'password' => '100rows',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),