read

As web developers, we all struggled with technical differences between our local development enviroment — usually MAMP/LAMP — and our staging/production environments. Whether you need specific versions of PHP, install new software or other specific changes, chances are MAMP won't be enough for you, at least without ruining it for the other projects you have hosted in it.

Vagrant

To overcome this kind of problems without the hassle of setting up and maintaining a full featured Virtual Machine, Vagrant was created. Vagrant allows you to easily setup development machines that will be as similar as possible to your production machines. Specific packages can be easily installed and your other Vagrant VMs won't be affected.

Setting up Vagrant can be confusing if you're not familiar with it, but fortunately Chris Fidao has come up with a brilliant solution to help us out: Vaprobash -- A bash provisioning script that will install everything you need to get your Vagrant VM up and running.

Getting Started

VirtualBox + Vagrant

Vagrant runs on top of VirtualBox, so you'll need to get both of them installed. This is as easy as navigating to each project homepage, getting the installer and follow the instructions.

VirtualBox: https://www.virtualbox.org
Vagrant: http://www.vagrantup.com/

Vaprobash

In order to use Vaprobash, you need to get a copy of it on your local machine.

You can either do this manually through your File Manager (Finder on OSX, Nautilus on Ubuntu, etc.) or through the Terminal.

We will use the terminal in this tutorial, and we'll use ~/websites/example as our installation directory:

mkdir ~/websites/example && cd ~/websites/example

To get Vaprobash, we can either use wget or CURL. Run only one of the following commmands:

curl -L http://bit.ly/vaprobash > Vagrantfile

wget -O Vagrantfile http://bit.ly/vaprobash

As stated in Vaprobash repository, the bitly short URL will always point to the master branch Vagrantfile.

Vaprobash already sets most of the settings needed to get your development VM ready. We tell Vaprobash which packages to install by uncommenting lines in the file.

We will use Sublime Text to edit it:

subl Vagrantfile

We should then uncomment — by removing the # character — the lines that refer to Nginx, MySQL, Composer, Laravel and Git-FTP:

config.vm.provision "shell", path: "https://raw.github.com/#{github_username}/#{github_repo}/#{github_branch}/scripts/nginx.sh", args: [server_ip, public_folder]

config.vm.provision "shell", path: "https://raw.github.com/#{github_username}/#{github_repo}/#{github_branch}/scripts/mysql.sh", args: [mysql_root_password, mysql_version]

config.vm.provision "shell", path: "https://raw.github.com/#{github_username}/#{github_repo}/#{github_branch}/scripts/composer.sh", privileged: false, args: composer_packages.join(" ")

config.vm.provision "shell", path: "https://raw.github.com/#{github_username}/#{github_repo}/#{github_branch}/scripts/laravel.sh", args: [server_ip, laravel_root_folder, public_folder]

config.vm.provision "shell", path: "https://raw.github.com/#{github_username}/#{github_repo}/#{github_branch}/scripts/git-ftp.sh", privileged: false

Save your Vagrantfile and run:

vagrant up

Since this is the first time you run Vagrant, it will download and install Ubuntu 14.04 LTS as well as the packages you selected for you. This might take a while, so go and grab a cup of coffee :)

Vagrant configures a shared folder between your VM and your local machine so you can easily edit your files. To do so, you'll need to enter your password when prompted during the installation.

After the installation is done, run:

vagrant ssh

You're now logged into your Vagrant VM, and a /vagrant/laravel folder should have been created for you by Vaprobash. To access it, we just need a small tweak on our Nginx configuration file:

sudo nano /etc/nginx/sites-available/vagrant

Change the root folder line from:

root /vagrant

to

root /vagrant/laravel/public

You then have to restart Nginx for changes to take effect:

sudo service nginx restart

You should then be able to access http://192.168.33.10.xip.io/ on your local machine and see the default Laravel page.

You just need to configure your MySQL database now:

mysql -uroot -proot

You're now in the MySQL prompt. Enter the following to create a new database:

CREATE DATABASE laravel;

exit;

That's it for the MySQL database. It's that easy.

Since Vagrant configures the /vagrant folder on the VM to be shared on your local machine, you can now logout from your SSH session. Either run exit or press CTRL+D.

With you favorite editor or IDE, open up the laravel folder inside ~/websites/example/laravel. Edit app/config/database.php.

Vaprobash defaults the MySQL authentication password to root, so you just need to change the lines:

'password'  => '',

to

'password'  => 'root',

and

'database'  => 'database',

to

'database'  => 'laravel',

You're now running a fully working Laravel installation on a Vagrant LEMP stack.

To test that your routes and database are working, edit your routes.php file and paste in the following:

Route::get('/database', function()
{
    if(DB::connection()->getDatabaseName())
        echo "Sucessfully connected to MySQL - ".DB::connection()->getDatabaseName();
});

Access http://192.168.22.10.xip.io/database and voilà!

Your Laravel installation is now fully working on a Vagrant LEMP stack.

Blog Logo

Rui Gomes

Web Developer, Internet Marketing enthusiast and Student@FEUP. That pretty much sums it up.


Published

Image

Rui Gomes

Knowledge share with a great deal of random thoughts.

Back to Overview