Configure Virtual Hosts With Symfony2
You may want to have more than one single symfony2 project in your computer for development, so here is how to do it configuring virtual hosts with Apache.
This configuration is for Windows using Xampp, but you can change the path to fit Linux or other environement.
To configure virtual hosts it takes basically two steps:
1. Change your host file
Open the file: C:/Windows/System32/drivers/etc/host and add a new line for each project
127.0.0.1 symfony2.project1.localhost
127.0.0.1 symfony2.project2.localhost
To modify this file, you need administraton rights to do it.
2. Change the vhost file in Apache
Open the file C:\xampp\apache\conf\httpd.conf and make sure that the line:
Include "conf/extra/httpd-vhosts.conf"
is uncommented (delete the # sign before if necessary)
Open the file: C:\xampp\apache\conf\extra\httpd-vhost.conf and add these lines:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#################
### PROJECT 1 ###
#################
<VirtualHost *:80>
ServerName symfony2.project1.localhost
DocumentRoot "C:/xampp/htdocs/symfony2_project1/web"
<Directory "C:/xampp/htdocs/symfony2_project1/web">
DirectoryIndex app.php app_dev.php
Options FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
#################
### PROJECT 2 ###
#################
<VirtualHost *:80>
ServerName symfony2.project2.localhost
DocumentRoot "C:\xampp\htdocs\symfony2_project2\web"
<Directory "C:\xampp\htdocs\symfony2_project2\web">
DirectoryIndex app.php app_dev.php
Options FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
#################
### LOCALHOST ###
#################
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/xampp"
ServerAlias localhost
ServerName localhost
</VirtualHost>
Restart Apache and browse to http://symfony2.project1.localhost you should see your first project now, everything should work.
The Right Virtual Host Configuration
In the http-vhost.conf file follow these rules:
- The variable NameVirtualHost should be declared only once.
- The order of virtual hosts declaration is important, the block corresponding to Localhost should be added in the last place.
That is!, happy coding :)
The NameVirtualHost should be declared only once