Setting up a virtual host and development environment in Apache
When developing our Laravel app, we'll need a web server to run everything. In PHP 5.4 and up, we can use the built-in web server, but if we need some more functionality, we'll need a full web stack. In this recipe, we'll be using an Apache server on Windows, but any OS with Apache will be similar.
Getting ready
This recipe requires a recent version of WAMP server, available at http://wampserver.com, though the basic principle applies to any Apache configuration on Windows.
How to do it...
To complete this recipe, follow these steps:
Open the WAMP Apache
httpd.conf
file. It is often located inC:/wamp/bin/apache/Apach2.#.#/conf
.Locate the line
#Include conf/extra/httpd-vhosts.conf
and remove the first#
.Move to the
extra
directory, open thehttpd-vhosts.conf
file, and add the following code:<VirtualHost *:80> ServerAdmin {your@email.com} DocumentRoot "C:/path/to/myapp/public" ServerName myapp.dev <Directory "C:/path/to/myapp/public"> Options Indexes FollowSymLinks AllowOverride all # onlineoffline tag - don't remove Order Deny,Allow Deny from all Allow from 127.0.0.1 </Directory> </VirtualHost>
Restart the Apache service.
Open the Windows hosts file, often in
C:/Windows/System32/drivers/etc
, and open the filehosts
in a text editor.At the bottom of the file, add the line
127.0.0.1 myapp.dev
.
How it works...
First, in the Apache config file httpd.conf
, we uncomment the line to allow the file to include the vhosts
configuration files. You can include the code directly in the httpd.conf
file, but this method keeps things more organized.
In the httpd-vhosts.conf
file, we add our VirtualHost code. DocumentRoot
tells the server where the files are located and ServerName
is the base URL that the server will look for. Since we only want to use this for our local development, we make sure to only allow access to the localhost with the IP 127.0.0.1
.
In the hosts
file, we need to tell Windows which IP to use for the myapp.dev
URL. After restarting Apache and our browser, we should be able to go to http://myapp.dev
and view our application.
There's more...
While this recipe is specific to Windows and WAMP, the same idea can be applied to most Apache installations. The only difference will be the location of the httpd.conf
file (in Linux Ubuntu, it's in /etc/apache2
) and the path to the public
directory for DocumentRoot (in Ubuntu, it might be something like /var/www/myapp/public
). The hosts
file for Linux and Mac OS X will be located in /etc/hosts
.