Apache single dynamic virtualhost configuration for local development
apache, configuration, dynamic, generics, virtualhost
Solution
I did it!
First of all, we need to enable two mods: mod_vhost_alias and mod_rewrite
sudo a2enmod vhost_alias
sudo a2enmod rewrite
1] Create a new virtualhost in /etc/apache2/sites-available, i named it zzz-dev
<VirtualHost *:80>
#All requests ending with .dev will use this virtualhost
ServerName dev
ServerAlias *.dev
# Get server name of header Host:
UseCanonicalName Off
# Interpret the request url to find the right project folder. Ex: For customer1.project1.dev, %1 is the first part (here: customer1), %2 the second part (here: project1), so the folder for this url is /home/victor/takative/projets/customer1/project1/public
VirtualDocumentRoot /home/user/mainprojectdirectory/%1/%2/public
# Fix for missing $SERVER['DOCUMENT_ROOT'] while using VirtualDocumentRoot, the setDocumentRoot.php file will be added autmatically to set the variable
php_admin_value auto_prepend_file /home/lib/utils/setDocumentRoot.php
RewriteEngine On
RewriteOptions Inherit
<DirectoryMatch "/home/user/mainprojectdirectory/.*">
IndexOptions +FancyIndexing NameWidth=*
Options Includes FollowSymLinks Indexes
AllowOverride All
Order allow,deny
Allow from all
</DirectoryMatch>
</VirtualHost>
Here is the content of setDocumentroot.php:
<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '',$_SERVER['SCRIPT_FILENAME']);
?>
2] Enable the new virtual host:
sudo a2ensite zzz-dev
3] Reload apache:
sudo service apache2 reload
4] Now, to create a project, you simply have to follow the folder hierarchy from above and edit /etc/hosts file with this line:
127.0.0.1 customer1.project1.dev
Hope it helps. If anyone have suggestion to improve this config, i'm open. Thanks
Problem
i'm configuring my development environement on my local laptop (ubuntu 12.04), and having some problems to achieve what i want, i'm beginner with apache configuration. I created a main project directory, and i'd like to set a single dynamic virtual host for all my projects, according to my folders hierarchy. Here is the folders hierarchy i use: - Main project directory (contain all projects): - Customer1 - Project1 - public (project root directory ) - Project2 - public - More projects... - Customer2 - Project1 - public - More customers... To access a project i use this urls: customer1.project1.dev, customer1.project2.dev, customer2.project1.dev ... I've read some thrad about virtualhost configuration that talk about using parts of url in rootdirectory parameter, like this: /home/user/mainprojectdirectory/%1/%2/public So i started trying with this basis: ``` <VirtualHost *.dev> DocumentRoot /home/user/mainprojectdirectory/%1/%2/public ServerName %1.%2.dev </VirtualHost> ``` But i can't make it work. Am i on the right way? What steps should i follow to achieve what i want? Which file should i edit? All advices are welcome! (remember i'm beginner with apache configuration) Thank you.