Local Development Environments

For many projects, hosting your development environment using a simpler tool set will be
enough. If you already have MAMP or WAMP or XAMPP installed on your system, that will
likely be fine to run Laravel. You can also just run Laravel with PHP’s built-in web server,
assuming your system PHP is the right version.

All you really need to get started is the ability to run PHP. Everything past that is up to you.
However, Laravel offers two tools for local development, Valet and Homestead, and we’ll
cover both briefly. If you’re unsure of which to use, I’d recommend using Valet and just
skimming the Homestead section; however, both tools are valuable and worth understanding.

Laravel Valet

If you want to use PHP’s built-in web server, your simplest option is to serve every site from
a localhost URL. If you run php -S localhost:8000 -t public from your Laravel site’s
root folder, PHP’s built-in web server will serve your site at http://localhost:8000/. You can
also run php artisan serve once you have your application set up to easily spin up an
equivalent server.

But if you’re interested in tying each of your sites to a specific development domain, you’ll
need to get comfortable with your operating system’s hosts file and use a tool like dnsmasq.
Let’s instead try something simpler.

If you’re a Mac user (there are also unofficial forks for Windows and Linux), Laravel Valet
takes away the need to connect your domains to your application folders. Valet installs
dnsmasq and a series of PHP scripts that make it possible to type laravel new myapp &&
open myapp.dev and for it to just work.

You’ll need to install a few tools using Homebrew, which the documentation will walk you through, but the steps from initial installation to serving your apps are few and simple.

Install Valet (see the docs for the latest installation instruction — it’s under very active
development at this time of writing), and point it at one or more directories where your sites
will live. I ran valet park from my ~/Sites directory, which is where I put all of my underdevelopment
apps. Now, you can just add .dev to the end of the directory name and visit it in
your browser.

Valet makes it easy to serve all folders in a given folder as “FOLDERNAME.dev” using valet
park, to serve just a single folder using valet link, to open the Valet-served domain for a
folder using valet open, to serve the Valet site with HTTPS using valet secure, and to open
an ngrok tunnel so you can share your site with others with valet share.

Laravel Homestead

Homestead is another tool you might want to use to set up your local development
environment. It’s a configuration tool that sits on top of Vagrant and provides a preconfigured
virtual machine image that is perfectly set up for Laravel development, and
mirrors the most common production environment that many Laravel sites run on.

Setting up Homestead

If you choose to use Homestead, it’s going to take a bit more work to set up than something
like MAMP or Valet. The benefits are myriad, however: configured correctly, your local
environment can be incredibly close to your remote working environment; you won’t have to
worry about updating your dependencies on your local machine; and you can learn all about
the structure of Ubuntu servers from the safety of your local machine.

WHAT T OOLS DO HOMES T EAD OFFER ?
You can always upgrade the individual components of your Homestead virtual machine, but here are a few important tools Homestead comes with by default:

  • To run the server and serve the site, Ubuntu, PHP, and Nginx (a web server similar to Apache).
  • For database/storage and queues, MySQL, Postgres, Redis, Memcached, and beanstalkd.
  • For build steps and other tools, Node.

Installing Homestead’s dependencies

First, you’ll need to download and install either VirtualBox or VMWare. VirtualBox is most
common because it’s free.
Next, download and install Vagrant.
Vagrant is convenient because it makes it easy for you to create a new local virtual machine
from a precreated “box,” which is essentially a template for a virtual machine. So, the next
step is to run vagrant box add laravel/homestead from your terminal to download the box.

Installing Homestead

Next, let’s actually install Homestead. You can install multiple instances of Homestead
(perhaps hosting a different Homestead box per project), but I prefer a single Homestead
virtual machine for all of my projects. If you want one per project, you’ll want to install
Homestead in your project directory; check the Homestead documentation online for
instructions. If you want a single virtual machine for all of your projects, install Homestead in
your user ’s home directory using the following command:
git clone https://github.com/laravel/homestead.git ~/Homestead
Now, run the initialization script from wherever you put the Homestead directory:
bash ~/Homestead/init.sh
This will place Homestead’s primary configuration file, Homestead.yaml, in a new
~/.homestead directory.

Configuring Homestead

Open up Homestead.yaml and configure it how you’d like. Here’s what it looks like out of the
box:
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/Code
to: /home/vagrant/Code
sites:
- map: homestead.app
to: /home/vagrant/Code/Laravel/public
databases:
- homestead
# blackfire:
# - id: foo
# token: bar
# client-id: foo
# client-token: bar
# ports:
# - send: 50000
# to: 5000
# - send: 7777
# to: 777
# protocol: udp
You’ll need to tell it your provider (likely virtualbox), point it to your public SSH key (by
default ~/.ssh/id_rsa.pub; if you don’t have one, GitHub has a great tutorial on creating
SSH keys), map folders and sites to their local machine equivalents, and provision a database.
Mapping folders in Homestead allows you to edit files on your local machine and have those
files show up in your Vagrant box so they can be served. For example, if you have a ~/Sites
directory where you put all of your code, you’d map the folders in Homestead by replacing
the folders section in Homestead.yaml with the following:
folders:
- map: ~/Sites
to: /home/vagrant/Sites
You’ve now just created a directory in your Homestead virtual machine at
/home/vagrant/Sites that will mirror your computer ’s directory at ~/Sites.
T OP-LEVEL DOMAINS FOR DEVELOPMENT SIT ES
You can choose any convention for local development sites’ URLs, but .app and .dev are the most common.
Homestead suggests .app, so if I’m working on a local copy of symposiumapp.com, I’ll develop at
symposiumapp.app.
Now, let’s set up our first example website. Let’s say our live site is going to be
projectName.com. In Homestead.yaml, we’ll map our local development folder to
projectName.app, so we have a separate URL to visit for local development:
sites:
- map: projectName.app
to: /home/vagrant/Sites/projectName/public
As you can see, we’re mapping the URL projectName.app to the virtual machine directory
/home/vagrant/Sites/projectName/public, which is the public folder within our Laravel install.
We’ll learn more about that later.
Finally, you’re going to need to teach your local machine that, when you try to visit
projectName.app, it should look at your computer ’s local IP address to resolve it. Mac and
Linux users should edit /etc/hosts, and Windows users
C:\Windows\System32\drivers\etc\hosts. Add a line to this file that looks like this:
192.168.10.10 projectName.app
Once you’ve provisioned Homestead, your site will be available to browse (on your machine)
at http://projectName.app/.

Creating databases in Homestead

Just like you can define a site in Homestead.yaml, you can also define a database. Databases
are a lot simpler, because you’re only telling the provisioner to create a database with that
name, nothing else. We do this as follows:
databases:
- projectname

Provisioning Homestead

The first time you actually turn on a Homestead box, you need to tell Vagrant to initialize it.
Navigate to your Homestead directory and run vagrant up:
cd ~/Homestead
vagrant up
Your Homestead box is now up and running; it’s mirroring a local folder, and it’s serving it
to a URL you can visit in any browser on your computer. It also has created a MySQL
database. Now that you have that environment running, you’re ready to set up your first
Laravel project — but first, a quick note about using Homestead day-to-day.

Using Homestead day-to-day

It’s common to leave your Homestead virtual machine up and running at all times, but if you
don’t, or if you have recently restarted your computer, you’ll need to know how to spin the
box up and down.
Since Homestead is based on Vagrant commands, you’ll just use basic Vagrant commands for
most Homestead actions. Change to the directory where you installed Homestead (using cd)
and then run the following commands as needed:
  • vagrant up spins up the Homestead box.
  • vagrant suspend takes a snapshot of where the box is and then shuts it down; like
  • “hibernating” a desktop machine.
  • vagrant halt shuts the entire box down; like turning off a desktop machine.
  • vagrant destroy deletes the entire box; like formatting a desktop machine.
  • vagrant provision re-runs the provisioners on the preexisting box.

Connecting to Homestead databases from desktop applications

If you use a desktop application like Sequel Pro, you’ll likely want to connect to your
Homestead MySQL databases from your host machine. These settings will get you going:
Connection type: Standard (non-SSH)
Host: 127.0.0.1
Username: homestead
Password: secret
Port: 33060

No comments:

Post a Comment

=>SEO

1.  Syllabus 2.  Unit Wise Question/Material 3. Paper 4. Previous Paper