How to start a new Laravel 5 project with Homestead - quick reference
Last updated: 2015-05-11 :: Published: 2015-04-23 :: [ history ]You can also subscribe to the RSS or Atom feed, or follow me on Twitter.
I wrote this short get-started guide mainly for my own use, to have a reference handy to quickly set up a new Laravel project with a MySQL database. But as I felt the need to write it, one might find some interest in it as well.
[UPDATE 11/05/2015]:
As the use of Composer to install Homestead has been deprecated (at least it disappeared from the doc), this post is not using the homestead
commands anymore.
Prerequisites
This is assuming homestead is installed.
If it is not the case, please follow the instructions first: once completed, you will have a fully provisioned virtual machine run by Vagrant and containing everything necessary to develop with Laravel in the best conditions (if you are having trouble following the different steps, here is a free Laracast video that might explain it better).
If you have no idea what Vagrant is and why you need to install it along with something like VirtualBox, feel free to read the article I wrote on the subject.
A note about ssh
Laravel's documentation suggests to add an alias to ssh the Homestead box more quickly. I personnally prefer to use an ssh config to that purpose, as it is intended to.
Update or create a config
file in the .ssh
folder of your home directory:
vim ~/.ssh/config
Add the following content:
# Homestead
Host homestead
HostName 127.0.0.1
Port 2222
User vagrant
You can now access the Homestead machine from anywhere running:
ssh homestead
Create the project
First add a new site to the ~/.homestead/Homestead.yaml
file:
vim ~/.homestead/Homestead.yaml
Something like:
sites:
- map: site1.local
to: /home/vagrant/projects/site1
- map: new-site.local
to: /home/vagrant/projects/new-site/public
Here we want to start a new Laravel project (arbitrarily called "new-site") so we point the root to the public/
directory.
There is also another method, using the serve
script, allowing to add a new project without having to edit Homestead.yaml
nor to provision the box again. My preference goes to the first method because I like keep track of my existing projects in Homestead.yaml
.
I don't specify the database in this file, because I don't want the databases to be reset every time I provision the Vagrant box. Speaking of which, we now need to run this command, from ~/Homestead/
:
vagrant provision
or:
vagrant up —-provision
if your box isn't running yet.
This will basically create the right Nginx config on the Vagrant box and restart Nginx in there (and, like I said, reset the databases that are declared in the Homestead.yaml
file, so be careful. Simply remove the databases for which you don't want that to happen under databases
from that file).
Now, edit the hosts
file of your host machine (sudo vim /etc/hosts
on MacOS) to match the new domain to the box's IP:
192.168.10.10 new-site.local
Then, ssh
the box and create your new project using Composer (in my config, all my projects are under the ~/projects/
directory of the Vagrant box) (this will probably take a little while):
ssh homestead
cd projects
composer create-project laravel/laravel new-site
This will copy Laravel and all its dependencies in the new-site
directory.
If everything went fine, you should now be able to access http://new-site.local and see the default Laravel screen:
Set up the database
From the Homestead box, connect to MySQL and create the database with the right user (the password for homestead
is "secret"):
mysql -uhomestead -p
create database newsite;
grant usage on *.* to newsite@localhost identified by 'password';
grant all privileges on newsite.* to newsite@localhost;
flush privileges;
\q
Be sure to note the chosen password and edit the .env
file of your project, changing the values for the database details:
DB_HOST=localhost
DB_DATABASE=newsite
DB_USERNAME=newsite
DB_PASSWORD=password
Laravel comes with a couple of database migration scripts, creating the users
and password_resets
tables respectively. A quick way to check that the database is correctly set up is to run these scripts, from the project's root:
php artisan migrate
If the migration is successful, you're all set! Reset the database if necessary:
php artisan migrate:reset
That's it! A few quick steps to get started on a new Laravel project with Homestead.
You can also subscribe to the RSS or Atom feed, or follow me on Twitter.