Thanh bên blog
TRONG News

How to set up your Mac for local PHP Development

Author:  ERIC L. BARNES

As a developer I spend countless hours on the computer. Over time I accumulate a ton of cruft. Everything from old forgotten files, unused apps, and worst of all hidden junk that has been installed by following some random tutorial.

This past weekend I decided it was finally time to wipe my Macbook’s hard drive and start fresh. I have used it daily for several years now and still had artifacts from when I used Mamp. Since then Vagrant has turned to my local server of choice and one of the reasons is how clean you can keep your machine by utilizing it.

After finishing the new Mac OS X install it felt like a new beginning. So clean, so minimal. I’ve missed that.

This go around I wanted to keep it as minimal as possible and only install things I know I need and use. This tutorial covers how I set up my Mac for local PHP Development.

ZSH Shell

ZSH is a bash alternative and can be easily installed using an open source project named “Oh My ZSH”. I prefer this route because it has numerous plugins and themes that come preinstalled. So customizing is easy.

Their site will give you instructions for installing and it’s all done automatically via a curl or wget call.

Since mine was a fresh machine this took a while to install because it also handles any requirements. Git, Xcode, etc. Just follow the on-screen instructions until it finishes.

Homebrew and PHP

The next step is to get PHP running on the local machine. It is needed for local PHP so later running a linter, composer, etc is all available to my apps.

Homebrew, labeled as the missing package manager for OS X, allows you to easily install items that are not standard from Apple.

You can install Homebrew by running the following terminal command:

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

Just as before, follow the on-screen steps until it completes.

Once it finishes, move on to installing PHP:

brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew install php56 --with-homebrew-curl --without-bz2 --without-apache --without-fpm --without-ldap --without-mysql
brew install mcrypt php56-mcrypt

These commands will setup a very basic php 5.6 without a lot of the items that typically come installed like Apache, fpm, ldap, MySQL, etc.

Composer

Composer is the PHP package manager. Almost all frameworks and packages are now using it to handle dependencies so it’s good to have it installed locally.

We can easily install it by running the following in terminal:

curl -sS https://getcomposer.org/installer | php

After it completes move the composer.phar executable into your terminals $PATH:

sudo mv composer.phar /usr/local/bin/composer

After this you may have to restart terminal but you should be able to run composer and get a list of available commands.

Vagrant

Vagrant is a tool for building a local virtual development environment. It allows you to run another operating system within your current one. The advantages to using a virtual machine is that everything is self contained.

If you make a mistake, just destroy the machine.

Another use case is you can mirror your production server which will allow you to confidently ensure you are developing to the same requirements.

To give you an example, a few years ago PHP changed it’s array syntax and any old array code would continue to work. However, the new style would throw out a nasty error if the PHP version running didn’t support it. By having your local machine mirror production you can instantly see the error of your ways.

Installing Vagrant is really simple as they offer binaries for Mac OS X, Windows, and Linux. Just head over to their site and download the one that’s right for you.

After it installs it’s time to setup a new Vagrant box and I choose to use Homestead by Laravel.

Homestead

Homestead is created by Laravel but it’s not specific to the framework. You can run any PHP app on it.

What I enjoy about Homestead is that it allows you to create a virtual machine in the style you like. Most Vagrant tutorials recommend you have a machine for each of your apps but I came from using Mamp and I like having one master VM with all my apps. That way I can quickly switch between anything at any time.

To setup Homestead run the following terminal command:

vagrant box add laravel/homestead

This downloads a machine image and it’s pretty hefty, so it will take a few minutes based on your internet speed.

While it’s downloading I open a new terminal tab and create a new Code directory to hold all my apps:

mkdir ~/Code

Next cd into Code and clone the Homestead git repo:

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

Now, cd into Homestead and run its installer:

bash init.sh

This init.sh file does is create a new directory at ~/.homestead with a yaml file for app settings and two extra files for basic settings. These are after.sh and aliases.

Now open ~/.homestead/Homestead.yaml in your favorite editor. The part we are concerned about is the folders, sites, and databases.

Folders are where your code is located on your machine. By default it is ~/Code just as we created earlier.

Sites are your local apps. For example if I wanted my next app to be cakesandpies then I’d use:

sites:
    - map: cakesandpies.app
      to: /home/vagrant/Code/cakesandpies/public

What this basically says is, map the cakesandpies.app url to the directory specified. Homestead uses this to create a vhost on the virtual machine to match them up.

Finally in the databases section you can list out any you want automatically created.

databases:
    - homestead
    - cakesandpies

Now save this file and it’s time to get it all running. cd into ~/Code/Homestead and run vagrant up. A short while later it should say it’s up and running.

Before we can access our app from the browser we will need to add a local domain to our hosts file. Open /etc/hosts in your favorite editor and add a new line at the bottom:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 
127.0.0.1   cakesandpies.app

Save it and now create a new file at ~/Code/cakesandpies/public/index.php and enter this text:

<?php
echo 'Cakes and Pies!';

Now hit the url in your browser. Be sure and append the port of 8000 which is Homestead’s default. http://cakesandpies.app:8000

If all went as planned you should see “Cakes and Pies!”.

Conclusion

These steps will hopefully allow you to get a fresh machine up and running for PHP development pretty easily. If you have any questions feel free to comment below or hit me up on Twitter. A huge thank you to Taylor Otwell and his Gist for making this easy.

3 bình luận

Laumn

]
[url=http://uindv.anticatrattoriadabruno.it/]uindv[/url]
[url=http://bpwcr.circuitolongdistance.it/]circuitolongdistance.it[/url]
[url=http://osdwq.trasciatti.it/]http://osdwq.trasciatti.it[/url]
[url=http://milionario.esbcbologna2019.it/]http://milionario.esbcbologna2019.it[/url]
[url=http://hdzip.agriturismoserradisotto.it/]hdzip[/url]
[url=http://jsqou.agriturismoserradisotto.it/]http://jsqou.agriturismoserradisotto.it[/url]
[url=http://fohyj.esbcbologna2019.it/]http://fohyj.esbcbologna2019.it[/url]
[url=http://smpcz.agriturismoserradisotto.it/]http://smpcz.agriturismoserradisotto.it[/url]
[url=http://robot-di-trading.circuitolongdistance.it/]http://robot-di-trading.circuitolongdistance.it[/url]
[url=http://kgfif.trasciatti.it/]kgfif[/url]
[url=http://trade.marfanonline.it/]http://trade.marfanonline.it[/url]
[url=http://apkxp.trasciatti.it/]apkxp.trasciatti.it[/url]
[url=http://strategia.trasciatti.it/]http://strategia.trasciatti.it[/url]
[url=http://smktx.brasile-italia.it/]smktx.brasile-italia.it[/url]
[url=http://trading-opzioni-binarie.circuitolongdistance.it/]trading-opzioni-binarie[/url]
[url=http://tffra.trasciatti.it/]trasciatti.it[/url]
[url=http://xldjg.esbcbologna2019.it/]esbcbologna2019.it[/url]
[url=http://profitto.trasciatti.it/]profitto.trasciatti.it[/url]
[url=http://okerh.brasile-italia.it/]http://okerh.brasile-italia.it[/url]
[url=http://broker-di-criptovaluta.circuitolongdistance.it/]broker-di-criptovaluta.circuitolongdistance.it[/url]
[url=http://ckcpu.trasciatti.it/]ckcpu.trasciatti.it[/url]

Laumn

]
[url=http://mostar.id]http://www.mostar.id[/url]
[url=http://tfosorcim.org]tfosorcim.org[/url]
[url=http://nomatterwh.at]http://www.nomatterwh.at[/url]
[url=http://gv38.fr]gv38.fr[/url]
[url=http://antiageblog.at]http://www.antiageblog.at[/url]
[url=http://monitory.id]monitory.id[/url]
[url=http://dpifoto.id]http://www.dpifoto.id[/url]
[url=http://monitory.id]www.monitory.id[/url]
[url=http://diakonietempelhof.de]http://www.diakonietempelhof.de[/url]
[url=http://the-pantheon.net]http://www.the-pantheon.net[/url]
[url=http://smartetechnik.at]www.smartetechnik.at[/url]
[url=http://accordeonklanken.nl]accordeonklanken.nl[/url]
[url=http://hacerturismo.net]www.hacerturismo.net[/url]
[url=http://shoppingberater.at]http://shoppingberater.at[/url]
[url=http://heartlandpartnership.org]www.heartlandpartnership.org[/url]
[url=http://dpifoto.id]dpifoto.id[/url]
[url=http://viptoto.net]http://www.viptoto.net[/url]
[url=http://monitory.id]http://www.monitory.id[/url]

David

It’s nice

Bài viết liên quan