"It is considered that each project would usually have three stages:
1. Production: Live web application, accessible by public.
2. Test: Web application under testing mode, accessible by development team and the project client.
3. Development: Web application under development mode, accessible by development team only. [Each member of development team would have a separate copy of application under development mode.]
Test and Development stages will be running under sub-domains 'test' and 'dev' respectively. For example, a project would be running on a domin 'example.com' then, Test stage would be running under 'test.example.com' and Development stage would be running under 'dev.example.com'.
NOTE: It is highly recommended that developers use 'dev' sub-domain for running their separate development stage copy. Running application as a subdirectory of default domain e.g. http://localhost/example is not recommended.
Creating Virtual Hosts¶
By adding the above line in 'hosts' file, 'dev.example.com' will point to a local machine. The 'hosts' file is usually located in 'C:\WINDOWS\system32\drivers\etc' on windows machine.
Initial setup for virtual hosts:¶
NOTE: This is only an initial setup before creating Virtual Hosts. This setup will not be needed every time Virtual Host is created. Once this setup is configured, this step can be skipped.
Usually, to add new virtual host for a domain, 'httpd.conf' file is needed to be edited as explained in official apache documentation, http://httpd.apache.org/docs/2.2/vhosts/name-based.html
But, instead of the traditional method, the following conventions are highly recommended for each virtual host:
* Create a separate directory containing all virtual hosts. It is recommended to place this directory in a different hard disk drive then the system drive where operating system is installed. For example, 'D:/vhosts'
* Create a directory named '_default_' inside 'vhosts' directory. This directory will be default document root. That means URLs like 'http://localhost/' will open web documents from this directory.
* Create a file named 'registry.conf'. This file will contain common configuration for 'vhosts' directory and default document root. Add the following lines inside 'registry.conf':
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot 'D:/vhosts/_default_'
</VirtualHost>
<Directory 'D:/vhosts'>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
* Add following line to 'httpd.conf' file. (Normally found at '<apache_installation_dir>/conf/httpd.conf')
Include 'D:/vhosts/registry.conf'
Adding virtual host in apache for a domain:¶
* Create an individual directory for the domain inside 'vhosts' directory. For example: a directory named 'D:/vhosts/example1.com' should be used for sub-domain 'dev.example1.com', 'D:/vhosts/example2.com' should be used for sub-domain 'dev.example2.com'.
* Create the following three sub-directories inside the domain directory:
1. conf/
Contains '*.conf' files for domain specific apache configurations including virtual hosts configuration.
2. logs/
Apache generated logs will be created under this directory.
3. public_html/
Application code will be placed under this directory. Usually, this directory can be set as Document Root for the domain.
* Create 'vhosts.conf' file inside 'conf' directory and add the following lines for creating 'dev.example.com' Virtual Host:
<VirtualHost *:80>
ServerName dev.example.com
DocumentRoot 'D:/vhosts/example.com/public_html'
ErrorLog 'D:/vhosts/example.com/logs/error.log'
CustomLog 'D:/vhosts/example.com/logs/access.log' common
</VirtualHost>
* Now we only need to include every configuration files ('*.conf') files in 'registry.conf' which is already created and included in main 'httpd.conf' file of Apache as explained in Initial setup for virtual hosts. Open 'registry.conf' file and add following line for 'dev.example.com' domain:
Include 'D:/vhosts/example.com/conf/*.conf'
* Restart Apache service
Registering a domain as a local domain:¶
Once all setup explained above is configured properly, Apache web server will start serving web applications according to Virtual Host configurations. But it is still needed to register the domain as a local domain, otherwise the web pages from the application will not be displayed. The following line needs to be added in 'hosts' file to register 'dev.example.com' as a local domain:
dev.example.com 127.0.0.1
Now the web application should be running successfully on the registered domain."
No comments:
Post a Comment