So here is the situation:
- you just bought a domain, namely spectacular.com, and you want to point it to you home server with a static IP, or VPS or whatever…
- you want to serve web-pages with apache and want to have configurations that enable you to configure several sub-domain, just like sub1.spectacular.com and sub2.spectacular.com;
- you are running Ubuntu or some Debian-based server.
Apache supports name-based virtual hosts. This nice feature of apache, lets you server “server” several domains with a single web-server, from a single network interface. This means that your several domains will share a single IP and single Apache.
First step is, from your Domain Registrar, buy you domains and create the sub-domains you want. You probably cannot create and a great number of sub-domain, but certainly will be able to create 6 or a little more sub-domains.
In our situation, these should probable be of type CNAME pointing to your main domain. In our case, spectacular.com. The main domain, should also point to the IP of the machine where your running Apache.
With this done, it’s time to install Apache in your machine:
sudo apt-get install apache2
This won’t give you much, just the most basic Apache installation, without support for anything.
However, let’s keep it this way and focus on the objective.
Next, it’s time to mess with Apache configuration files ![]()
First, you should tell Apache which is the IP address that resolves the virtual hosts. In Debina-based systems, the following file should exist, and contain already a NameVirtualHost directive:
/etc/apache2/ports.conf
Open this file (with root privileges) and edit the NameVirtualHost entry putting your IP address instead of the wildcard (* ):
NameVirtualHost 192.192.192.192:80
With this done, it’s a good idea to edit immediately the hosts file. In this file you will tell that your domains resolve to your IP. Open and edit /etc/hosts and add an entry like this, in a new line:
192.192.192.192 spectacular.com
Now it’s time for you to create your virtual hosts!
In a context of good organization, it might be a good idea to create the following directories, one for each virtual host:
/var/www/vhosts/spectacular.com /var/www/vhosts/sub1.spectacular.com /var/www/vhosts/sub2.spectacular.com
These will be the root paths for each of your virtual hosts.
Inside these directories, you can also touch some files, index.html, with different contents, just so you can see the different files being served by your different domains.
Remember, these directories and files need to have permissions that enable Apache to read them! You can, for example, do the following:
sudo chown -R www-data:www-data /var/www/vhosts/
You need now to create a configuration file, to be read by apache, for each of your virtual hosts:
/etc/apache2/sites-available/spectacular.com /etc/apache2/sites-available/sub1.spectacular.com /etc/apache2/sites-available/sub2.spectacular.com
Now, inside each of these files, you’ll create VirtualHost blocks. For example, inside /etc/apache2/sites-available/spectacular.com:
<VirtualHost 192.192.192.192:80> ServerAdmin youremail@blabla.com ServerName spectacular.com DocumentRoot /var/www/vhosts/spectacular.com/ </VirtualHost>
For the other two files, you should change the VirtualHost, ServerName and DocumentRoot directives accordingly:
/etc/apache2/sites-available/sub1.spectacular.com:
<VirtualHost 192.192.192.192:80> ServerAdmin youremail@blabla.com ServerName sub1.spectacular.com DocumentRoot /var/www/vhosts/sub1.spectacular.com/ </VirtualHost>
/etc/apache2/sites-available/sub2.spectacular.com:
<VirtualHost 192.192.192.192:80> ServerAdmin youremail@blabla.com ServerName sub2.spectacular.com DocumentRoot /var/www/vhosts/sub2.spectacular.com/ </VirtualHost>
Note that the IP in the VirtualHost tag is always the same and is the one that we’ve put on the NameVirtualHost
With these files created, you need now to tell apache to read them.
To do that, you can:
sudo a2ensite spectacular.com sub1.spectacular.com sub2.spectacular.com
or
sudo ln -s /etc/apache2/sites-available/spectacular.com /etc/apache2/sites-enabled/spectacular.com sudo ln -s /etc/apache2/sites-available/sub1.spectacular.com /etc/apache2/sites-enabled/sub1.spectacular.com sudo ln -s /etc/apache2/sites-available/sub2.spectacular.com /etc/apache2/sites-enabled/sub2.spectacular.com
Finally, you need only to restart your Apache by invoking:
sudo /etc/init.d/apache restart
and, if you did right
, there should be no errors.
Now, just browse to your domains, and see different contents being served by different domains, from the same web-server.
Keep in mind that this is a minimalistic guide, with the purpose of giving only general guidelines. For example, the installation of Apache is obviously not enough for a php-powered web-site…
References:
http://httpd.apache.org/docs/2.0/vhosts/examples.html
http://en.wikipedia.org/wiki/Domain_name_registrar
https://help.ubuntu.com/community/ApacheMySQLPHP
http://httpd.apache.org/docs/1.3/mod/core.html#namevirtualhost
http://linux.about.com/od/lna_guide/a/gdelna46.htm
Post a Comment