Why I Don't Bother With Virtual Hosts or Vagrant, And How To Give Them Up For Good!
By Phillip Harrington on 7/5/2015
I don't bother with Virtual Hosts or Vagrant. It may sound blasphemous, or stupid, but it works for me (YMMV). Call me crazy. Say I'm not doing what the cool developers are doing. I don't care.
My main reasons for doing so are speed and simplicity. The way I manage developing projects locally is to have a single, basically default web server configuration, with symbolic links into project directories. Lame, right?
But Virtual Hosts Are Cool!
Yes, they are. Browsing your project at my-project.dev is neat. But you have to create the virtual host every time you start a project. There are things out there that make this super simple like Pow (for Ruby on OS X), and I'm sure there's something similar for Windows or PHP, I just haven't found it (haven't really looked), and I don't want to bother. You can do some clever Rewrite so that *.dev points to a C:\Users\You\Dev\*\public folder. You can probably do a lot of things. I don't. I really don't care. I just want to get down to coding.
Vagrant Is Cool!
And you're even using Laravel, Phil, which has Homestead! What's wrong with you? Flat out, last time I checked, Vagrant didn't run on Windows. Or at least not on my version of Windows. Believe me, I tried.
Windows? Ewww! Use a Mac! Use Linux! I'm stuck on Windows at least half the time due to working at a corporation with an IT dept. that makes their own flavor of Windows. I ran their installer for half a day and ended up with a computer that let's me communicate with everyone else in the company. And I still had Administrator access to install my own IDE and other various programs so I can do my job. My Windows set-up is actually so similar to my iMac at home that there's very little difference in my work environment, besides "ls" being "dir" and "/" being "\". I even have Console with Solarized Dark colors. It's really not so bad.
I don't really have the time or inclination to install Xubuntu on my company issued laptop, and maintain it plus all of my applications all by myself, just to end up with the perfect development environment (and a computer that doesn't run Office).
You Don't Need Virtual Hosts
For reals, yo. You actually don't need to jump through all of these hoops to develop something locally that still works when you deploy it. And if you do need it, well then by all means, make a virtual host. But trust me, this solution works 99% of the time.
How To Do It
On OS X or Linux flavors, you can create a symbolic link in your web server's document root. Do it with the command line using the ln command by specifying the target and link name:
$ cd /var/www/htdocs $ sudo ln -s /home/username/code/my-project/public my-project
On Windows, there's a little tool called Junction which will let you create symbolic links on the command line. The only difference is the parameters are reversed (link name first, target second):
> junction my-project C:\Users\username\Code\MyProject\public
Now your project is "rooted" here: http://localhost/my-project
That's it. No messing around with configuration files. No restarting Apache.
But... But... Relative From Root Paths?
I personally feel that if your application or framework requires you to put code in the root directory, there's something wrong with it. If programmed carefully, you can easily deploy a project at the web root of a virtual host somewhere, without changing any code. Most modern frameworks have a ‘base path' function.
I've only run into one problem doing things this way when working with Laravel's pagination. Their paginator class assumes that the base path is ‘/', but I was able to overcome that by passing url() into the path parameter from the controller. It's hacky, but works. I'm thinking about making a pull request one of these days and seeing how that goes.
Questions? Comments? Emotional outbursts? Let me know how you set-up your development environment.