Name-based Virtual Host configuration on apache
Virtual host configuration on apache
I’ve been seeing a lot of clients that are having some issues creating a virtual host configuration on apache or nginx or any other web server (usually apache). In this article I’m going to show you a quick and easy way how to create a virtual host configuration (vhost for short) on a linux based operating system or windows based for apache. Well there isn’t much of a difference really, the only change you need to make between these two operating systems are the document_root paths. So lets get right to the point.
Creating virtual host configuration on Debian linux
There are two major distribution based linux, Debian or Redhat. We’ll start with Debian first, keep in mind that for this operating system, by default you have a folder named sites-enabled in the bellow location:
/etc/apache2/
or
/usr/local/apache2/sites-enabled/
Virtual hosts configurations will need to be created as a separated files named .conf, so for example, I would want to create the virtual host configuration for my domain yourhowto.net
1 2 3 4 5 6 7 8 9 10 11 12 |
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName yourhowto.net ServerAlias www.yourhowto.net DocumentRoot /home/yourho/public_html/ <Directory /home/yourho/public_html/> Options Indexes FollowSymLinks MultiViews AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> |
The configuration above would be created inside a file yourhowto.net.conf in the location
/etc/apache2/sites-enabled/yourhowto.net.conf
The above is a default configuration file, usually you do need to have more then this, but depending on server configurations there could more directives like suPHP user and group or suexec user and group or multiple aliases and so on.
Creating virtual host configuration on Redhat linux
The virtual host configuration content is the same, the only difference is that by default Redhat webserver configurations does not have a separated folder for virtulhosts. Meaning that all virtual host configurations will be added directly to the webserver, in this case apache, configuration file:
/etc/httpd/conf/httpd.conf
In the above file you literately add to the bottom of the file the content bellow:
1 2 3 4 5 6 7 8 9 10 11 12 |
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName yourhowto.net ServerAlias www.yourhowto.net DocumentRoot /home/yourho/public_html/ <Directory /home/yourho/public_html/> Options Indexes FollowSymLinks MultiViews AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> |
Now you will obviously change the domain name with your own, the same with the document root and server alias. If you need maybe CGI added to your vhosts, you will also add +ExecCGI to the options value.
Creating virtual host configuration on Windows
As I already said, the only difference on this operating system is that the paths are different, the rest should be the same, see bellow example:
1 2 3 4 5 6 7 8 9 10 11 12 |
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName yourhowto.net ServerAlias www.yourhowto.net DocumentRoot c:\www\yourho\public_html\ <Directory c:\www\yourho\public_html\> Options Indexes FollowSymLinks MultiViews AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> |
The path to where this needs to be added really depends on where you installed your webserver, but usually it should have a folder named vhosts or virtual hosts or file names with these names. Sorry I can’t be more specific, at the moment I don’t have a test case and can’t remember from the back of my head.
If you maybe have IP based virtual hosts, you would change the first line
1 |
<VirtualHost *:80> |
to something like:
1 |
<VirtualHost 25.25.26.26:80> |
but you will need to be sure that the domain will point to that server IP to work as expected.
That’s all for now, don’t forget to share it if you liked it!