Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Andrew Welch
Andrew Welch

Posted on • Originally published atnystudio107.com on

     

Using PhpStorm with Vagrant / Homestead

Using PhpStorm with Vagrant / Homestead

Here’s how to set up Php­Storm with Vagrant / Home­stead for local dev with Xde­bug for Craft CMS or oth­er PHP dev

Andrew Welch /nystudio107

Storm At Sea

There are many guides out there for usingPhp­Storm withVagrant/​Homestead but many of them are out of date, and giv­en the stum­bling blocks I ran into, I fig­ured I’d put up a quick ​“how to” on the subject.

This is avery spe­cif­ic tuto­r­i­al. If you’re not using Php­Storm with Vagrant/​Homestead in order to do PHP devel­op­ment (or a mix of PHP and fron­tend devel­op­ment), it’s prob­a­bly of no inter­est to you at all.

So why would we use Php­Storm? In the words of an angry guy I know named Brad, ​“Frankly, I don’t know how any­one does PHP devel­op­ment with­out it.”

If you do any lev­el of PHP devel­op­ment, it’s worth the expense and set­up time to be able to set break­points, inspect vari­able val­ues, look at stack traces, and so on and so forth. The nor­mal things you’d expect from an Inte­grat­ed Devel­op­ment Envi­ron­ment (IDE).

TheBe Awe­some in Php­Storm Lara­Casts are a must-watch (and free!) for any­one using PhpStorm.

My Secret Shame

Despite the fact that I’ve done a ton ofPHP plu­g­in devel­op­ment, my secret shame is that I’ve been doing it all along withSub­lime Text. That’s not a slight against Sub­lime Text, it’s a fan­tas­tic edi­tor. But it’s not an IDE, and attempt­ing to do devel­op­ment on any rea­son­able scale with­out an IDE is painful at best.

I made sev­er­al abort­ed attempts to get up and run­ning with Php­Storm, but I always ran into stum­bling blocks or flat-out did­n’t have the time to invest in get­ting it up and run­ning, and learn­ing how to use it effectively.

Hidden Shame Face

So my​“one new thing” for an upcom­ing project is to get going with Php­Storm, come hell or high-water. If you’re doingany lev­el of PHP devel­op­ment pro­fes­sion­al­ly, don’t be like me. Get on board the Php­Storm express ASAP.

Get­ting Php­Storm up and Running

So let’s get things up and run­ning, shall we? This tuto­r­i­al assumes that you already have Vagrant installed and run­ning, and are using it for local dev. If you don’t have it up and run­ning yet, the videoSet­up Craft CMS on Vagrant Home­stead will help you out.

First pro tip is when you fire up Php­Storm, don’t use any of its set­up wiz­ards. I’m sure there’s some way to get them to pro­duce the desired end-result, but I sure haven’t found it.

Instead, just go right toOpen and choose your exist­ing root Craft CMS project install, and away you go.

Phpstorm Open

The next thing we need to do is to tell Php­Storm that we’re using Vagrant. Go toPhpStorm→Preferences and then click onTools→Vagrant. If you see an error mes­sageError: Can't run Vagrant executable, even though Vagrant is prop­er­ly installed and work­ing, it’s pos­si­ble that thevagrant com­mand isn’t in your$PATH.

Phpstorm Vagrant

For instance, I had to enter the com­plete path to myvagrant exe­cutable:/usr/local/bin/vagrant Click onApply.

Phpstorm Php Settings

Next we need to tell Php­Storm whatPHP lan­guage lev­el (ver­sion) we’re using (PHP 7 is what I’d sug­gest every­one be using as a base now), and we need to choose theCLI Inter­preter that the project should be usinginside of the Vagrant box.

Click on the lit­tle next to to CLI Inter­preter so set the path to yourVagrantfile (usu­al­ly inside of yourHomestead/ fold­er) so that Php­Storm knows where to find your Vagrant VM, and con­fig­ure your Remote PHP set­tings as appro­pri­ate, then click onApply.

If it does­n’t show up, click the cir­cu­lar arrow icon for it to con­nect to your Vagrant box and inter­ro­gate it. Click onApply.

Next we need to set up a Run Con­fig­u­ra­tion for our project. Go toRun→Edit Con­fig­u­ra­tions…

Phpstorm Run Config

Then click on the+ icon and choosePHP Web Appli­ca­tion , and give your Run Con­fig­u­ra­tion a name (I just name it after the project name). Choose the default web brows­er you’d like to use (I pre­fer Chrome, but what­ev­er works for you).

Next, we need to set up a serv­er for it to use for this Run Con­fig­u­ra­tion. Click on the lit­tle but­ton next to theServ­er drop­down menu, give your serv­er a name, enter the local URL, and make sure theDebug­ger is set toXde­bug.

Phpstorm Server

Check theUse path map­pings (select if the serv­er is remote or sym­links are used) check­box. Now we need to tell Php­Storm where our local files are on the serv­er. For instance, my project root on my local machine is/Users/andrew/webdev/sites/craft3 but it needs to be mapped to the absolute path/home/vagrant/sites/craft3 on the remote serv­er (our Vagrant box).

We need to add a map­ping for the root project, as well as any­thing else that is sym­linked. For instance, for plu­g­in devel­op­ment, theseomatic plu­g­in is a sym­link to it’s actu­al loca­tion else­where in my dev machine, so we need to add a map­ping for that, too.

While this may seem a lit­tle strange, remem­ber that Php­Storm is read­ing files from your local machine, but let­ting you run and debug them in a ​“remote” serv­er on our Vagrant box. So we need some way to tell it how to map these things.

Click onOK , then choose this new­ly cre­at­ed serv­er con­fig from theServ­er: drop­down menu in theRun/​Debug Con­fig­u­ra­tions set­tings, then click onOK.

Are we there yet?

N.B.: This fol­low­ing been fixed in lat­er ver­sions of Home­stead, so the below may not apply to you… but it does­n’t hurt to san­i­ty check your Xde­bug settings.

No, we’re not there yet — but we’re very close! For what­ev­er rea­son, despite the fact that Home­stead includes Xde­bug, with it enabled by default, it isn’t ready to go for remote debug­ging. Which does­n’t make a whole lot of sense to me in the con­text of a Vagrant box. But anyway…

So we need to set it up. Do so byvagrant ssh​’ing to your Vagrant box, and then assum­ing you’re run­ning php7.0 edit the/etc/php/7.0/mods-available/xdebug.ini (you’ll need tosudo) file to look like this:

zend_extension=xdebug.soxdebug.remote_autostart = 1xdebug.remote_enable = 1xdebug.remote_connect_back = 1xdebug.remote_port = 9000xdebug.max_nesting_level = 512
Enter fullscreen modeExit fullscreen mode

This tells it that we want to be able to useXde­bug for remote debug­ging. Save it, then restart thephp7.0-fpm ser­vice viasudo service php7.0-fpm restart and away you go.

If you’re run­ning PHP 7.1 with lat­er ver­sions of Home­stead, just sub­sti­tute7.1 for7.0 in the above paths/​comments.

You can now start enjoy­ing all of the Php­Storm IDE good­ness by Using theRun→Run Pro­ject­Name orRun→Debug Pro­ject­Name menu items.

Set break­points. Dive through stack traces. Do some­thing awesome.

Further Reading

If you want to be notified about new articles, follownystudio107 on Twitter.

Copyright ©2020 nystudio107. Designed by nystudio107

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Stackless / Full Server developer @ nystudio107Mak­ing the web bet­ter one site at a time, with a focus on per­for­mance, usabil­i­ty & SEO
  • Location
    Webster, NY
  • Work
    Demolitions Expert at nystudio107
  • Joined

More fromAndrew Welch

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp