Sunday, October 27, 2013

Set Apache Document Root (Windows)


I always forget how to set virtual host or more than one document root in Apache server, this is just to remind myself about the changes in httpd.conf file.

Usually the document root is set as default to the www directory of your Apache installation, which Apache keeps listening on port 80. If you create your projects just within the www directory, you can pretty much access it with http://localhost/mypage.php (considering your page is called mypage.php). This looks more like working with a real website where http://localhost/ will be replaced by your registered domain name or ip address. However, if you make sub-directories inside your www directory, you will have to access them as http://localhost/mydirectory/mypage.php, but we don't want to write the specific directories separately from the base domain name, so one way to get around it is to tell your Apache to make 'mydirectory' another document root, so that you can access the pages in a 'more realistic' manner.

Now to keep things clean, we will assign new port numbers to each virtual document root other than the www directory which is listened to port 80. So, if we assign port 8080 to our new document root 'mydirectory', we can now access mypage.php using the url http://localhost:8080/mypage.php which is much better than the previous one and it will cost you less setup hassles when you deploy the project in an actual server.

Here what you need to add to your Apache's httpd.conf file:

Listen 8080

NameVirtualHost *:8080

<VirtualHost *:8080>
ServerName localhost
DocumentRoot "D:/wamp/www/mydirectory" #example path yo your new document root directory
</VirtualHost>

Don't forget to restart your Apache server.


No comments:

Post a Comment