In order for this virtual host to work, the directory /var/www/test must exist and have some contents, such as an index.html file, and DNS needs to be configured to point the DNS name www.test.com to the server's IP address. Note the ServerAlias directive in the configuration file. This allows Apache to accept either www.test.com or test.com as a valid name when a browser requests the site. This is important, as many people omit the initial www when manually typing in website URLs. You can also use ServerAlias to allow the server to deliver content for a completely different name, such as www.foo.com, from the same document root.
Once you have modified the virtual host configuration file to your needs, such as changing the server name and the DocumentRoot directory, then press Ctrl-O to save the file and exit the editor.
At this point, you can go on to create other virtual hosts in the same manner. You can create the document root with the mkdir command on Fedora or CentOS:
An alternative on Ubuntu:
sudo mkdir /var/www/test
Then place an HTML file in that directory called index.html:
Next, type this into the file:
<html> This is a test. </html>
To test the configuration on Fedora or CentOS, use:
service httpd configtest
If it responds with "Syntax OK," you're all set.
For Ubuntu, we first need to enable the site, then test the configuration like so:
sudo a2ensite test
This uses the Ubuntu tool a2ensite to enable the site you configured earlier. We also need to disable the default site if we're working without DNS, or if we want our test virtual host to be the only content served:
sudo a2dissite default
If we don't do this, then accessing the server by IP address with a browser will show the default site that was installed with Apache, rather than our test site. If you want to enable the default site at a later time, just run:
sudo a2ensite default
After this, you can run the following to test the configuration in Ubuntu:
sudo apachectl configtest
If all is well, you can now start or restart Apache to load the new configuration. In Fedora and CentOS:
service httpd restart
sudo /etc/init.d/apache2 restart
Once you've restarted the server, you can test the site by opening it with a browser, assuming that you've configured DNS to translate your site's name to the server's IP address. You should see "This is a test" in the browser, as the server delivers the index.html file you created.