Skip to content

Instantly share code, notes, and snippets.

@brakkum
Last active March 24, 2018 23:01
Show Gist options
  • Save brakkum/ac6c492f65bb080ee49e7a5aadfdfe91 to your computer and use it in GitHub Desktop.
Save brakkum/ac6c492f65bb080ee49e7a5aadfdfe91 to your computer and use it in GitHub Desktop.
Getting Setup for Symfony with Laravel/Homestead from scratch

Hello!

This is a quide to how I got setup with Symfony step by step while using Homestead. This is written from a total amateur perspective, so I will do my best to include all steps I took, even the bad ones.

The first step is to install Homebrew using Terminal to allow us to easily download and manage some packages we need.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Now we'll grab Cask using Homebrew. Cask is an extension to Homebrew that allows us to get some more graphical applications. The 'brew tap' command just allows us to access other repositories.

brew tap caskroom/cask

Now we'll grab Vagrant which will allow us to manage some virtual machines we'll be using.

brew install Caskroom/cask/vagrant

Next up, we'll grab VirtualBox, the program that will run our virtual server we'll be using.

brew install Caskroom/cask/virtualbox

Now we're going to add another tap with Homebrew that gives us access to more PHP related brews.

brew tap homebrew/homebrew-php

Now comes a part where there may be complications, depending on your current setup. We're going to install PHP. Now, Macs from 10.0.0 onward have come with an install of PHP. But, chances are it's quite out of date by now. So, I'm going to install PHP 7.1. This process may be different for you. In terminal you could run php --version to find out which version you're running, and try a Google search from there to find the best way to upgrade. That said, you could also just leave it be, but some steps further on in this tutorial may not work for you, so be warned. One command that may work after checking your version may be

brew unlink phpxy

where x and y are your version numbers. To instal PHP 7.1, enter:

brew install php71

Now we'll grab Composer. Composer is a dependency manager for PHP.

brew install composer

Now, we're going to use Vagrant to download a 'box' to use that will help us configure our Laravel/Homestead project. When prompted for what type of provider you'll want enter '3', as we will be setting this up with VirtualBox. (This step can take awhile)

vagrant box add laravel/homestead

Alright, now let's download Homestead itself. I recommend making sure that your terminal is in your home directory since this will be a host to all of your Laravel projects. This command will clone the most recent Homestead version from github, and put it in a Homestead folder in the directory you are in.

git clone https://github.com/laravel/homestead.git Homestead

Now, move into that folder:

cd homestead

Now, we'll create the Homestead.yaml file by running:

bash init.sh

Now you should see a Homestead.yaml file in the Homestead directory!

Now, I recommend adapting to the .yaml file for now to make sure you get things working properly. So open up the file and let's take a look. I've added some comments on here, surrounded by * comment * to just try adn explain quickly what some of this means.

---
ip: "192.168.10.10"   * This is the default ip used by Homestead * 
memory: 2048          * How much memory the vm will have *
cpus: 1               * Number of cpus * 
provider: virtualbox  * Who are we using to make the vm * 

authorize: ~/.ssh/id_rsa.pub * The location of your public key, which will be needed *

keys:
    - ~/.ssh/id_rsa          * private key location * 

folders:            * This tells Homestead what folder our projects are in, and what we want them maped to in our server * 
    - map: ~/code   * Where on our host is the file located * 
      to: /home/vagrant/code  * Where should it be located on the server vm *

sites:              * Similarly to folders, this will map our projects to a 'url' we can use to view it. *
    - map: homestead.test     * What url will we use to view our project *
      to: /home/vagrant/code/test/public * where, based on our folder mapping, will the public folder with and 
                                           index file be located. I changed it from code/public, 
                                           to code/test/public to better suit this tutorial.*

databases:
    - homestead * What database is being used, don't worry about this for now *

* Ignore the comments below here, this is for advanced setup *
# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 50000
#       to: 5000
#     - send: 7777
#       to: 777
#       protocol: udp

As noted in the code, I did change the sites: to: directory, so look out for that.

If you don't have a public/private key pair generated on your computer, do that now!

So for starters, go to you ~/ directory, where the Homestead files is located, and create a new directory named 'code'.

mkdir code
cd code

Now that we're in that file, let's make another named test that has a file named public inside.

mkdir test/public
cd test/public

alright, now let's make a file to live in this directory. We'll bring up the nano editor to make a simple php file.

nano

Now, in the nano editor, add this:

<?php echo "this is a php file!" ?>

Then to save, push control+o, and name the file index.php, press enter to confirm. push control+x to exit.

Now, we need to edit our hosts file to tell our internal routing about the update we made in the yaml file.

sudo nano /etc/hosts

You'll need to enter your password here. Now you'll see the nano editor again. scroll down to the bottom of the file, and add this line:

192.168.10.10   homestead.test

We're telling our computer that the url 'homestead.test' should be routed to the Homestead ip config.

alright, let's go back to our Homestead folder.

cd ../../..
cd homestead

If everything is setup right, we should be able to start our server now! This step can bring many errors, I've seen quite a few myself now. But, everything I wrote in here has led to my first perfect bootup, so I hope the same is for you!

vagrant up

This will take a while, but if everything went right, then you should be returned to your regular prompt. Open up a browser and visit 'homestead.test'. You should just see 'this is a php file!' if everythng went well!

Now let's return to our 'code' folder. Let's add in a symfony app! In the code folder execute:

composer create-project symfony/skeleton symfonytest

Now you should have a symfony test folder, populated with a bare symfony skeleton. You'll notice there's a public folder like the one we routed to earlier. Let's do that again! So, let's open up the homestead.yaml file again and add another site.

nano ~/homestead/homestead.yaml

right below our previous site, add this in the same format:

    - map: symfony.test
      to: /home/vagrant/code/symfonytest/public

Now save and exit using control+o and control+x.

Now we need to edit the hosts file again.

sudo nano /etc/hosts

add this listing below the last one we entered:

192.168.10.10  symfony.test

now that that's all done, let's see what we've got. Go to 'symfony.test' in your browser. Well, it looks terrible, because something isn't working right. I'm getting an error, but I'll look into that later. for now, this should be a good barebones start to your Symfony app, so get working! I'll come back and edit this part when I can figure out what isn't redirecting right.

Hopefully this was somewhat helpful. I'm by no means an expert at this, I just started using all of these various parts recently, but through extensive googling and testing it's starting to make more sense. Get coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment