Sometimes, you might need your Apache web server to handle multiple names for domains, and to deliver the correct site to the visitors. You need to know how to create Apache virtual hosts, test the names of domains so as to be sure that the web server they are pointing to is the correct one, and then perform a configuration on the Apache virtual host files so that the names for the domains can be pointing to a specific folder.
Configuration of the Apache vhost
When the domains are working effectively and as expected, we should configure the Apache so that it can route the domain names to the site directory. This can be done by following the steps given below
1. Locate and navigate to the directory having your Apache configuration. For Ubuntu users, then can be found at “/etc/apache2.” For other types of servers, this can be found at “/etc/http.”
2. You can then locate the vhost configuration. For Ubuntu users, this should be the directory “sites-available.” For the users of other types of servers, then they might have to edit the file “httpd.conf.”
3. You can then open or create the vhost configuration. For Ubuntu users, just create a new file in the directory “sites-available.” The file can be given the same name as the name of the domain. However, you can choose the name that you want for the file, provided you can recall it.
4. A new vhost record can then be added. The Apache directives can then be added to the file. However, one has to have ServerAlias, ServerName, and DocumentRoot directives for a particular host. An example of this is given below:
# The vhost record can now be started on the default HTTP port 80 # The vhost name. ServerName udrupal.com # The alternative names for the same vhost. # The other domains can be added here. These will be moved to the same place. ServerAlias news.udrupal.com ServerAlias udrupalalumni.com # This is the Directory where the code for the website lives. DocumentRoot /home/udrupal/www Options FollowSymLinks AllowOverride All
5. The changes made to the file can now be saved.
6. The site can now be enabled. This is to make sure that the Apache web server applies the newly made changes to the configuration.
7. Just open the command prompt, and then run the following command
What will happen is that you will be notified that the site is being enabled, and then the reload command will be issued.
8. The Apache can then be reloaded or restarted. However, the Apache web server will not immediately notice the changes. This is why you have to restart the configuration files for Apache. The command for doing this will depend on the type of system that you are using. In most systems, the command should have the “sudo” command in front of it as shown below: sudo /etc/init.d/apache2 reload
9. The system should now be set up. You can open up your browser, and then type one of the domain names. The directory of the site should be observed loading.