A Virtual Host file contains configuration directives for a website, including a site’s document root, security policies, SSL certificate configuration, and much more. Each website that is configured within an Apache Virtual Host operates independently of each other with separate and unique settings. Apaches Virtual Host feature allows webmasters to maximize server resources by running multiple websites on one host instead of multiple hosts running multiple websites.
How to create website directory structures on Ubuntu Linux
When you’re running multiple websites on a single host, each website will have its document root. A document root is a directory where the website files for a domain are stored and served in response to requests. Below is an example of a directory structure for multiple websites with unique content and domains. As you can see above, each domain will have its folder with a document root included. Example: /var/www/domain/public_html Run the commands below to create a directory for example.com domain with its document root. Each document root will need an index.html file that will be shown to clients. Run the commands below to create an index.html file for the example.com domain. Then copy and paste the content below into the file and save it. Below is just a basic HTML file for testing purposes. Once you save the file, we’re ready to configure Apache Virtual Host to reference this content. To avoid any permission issues, change the ownership of the domain document root directory and all files within the directory to the apache user (www-data) :
How to create an Apache Virtual Host file on Ubuntu Linux
Now that you’ve created domain content in the directory above, go and configure the Apache Virtual Host configuration file for the domain content above. On Ubuntu Linux, Apache Virtual Hosts configuration files are located in the /etc/apache2/sites-available directory. To create a virtual host file in the sites-available directory for our content above, run the commands below to create a site-specific Virtual Host configuration file An example configuration that should work with most environments is shown below. Copy and paste the content below into the file above and save. Once the file is saved, you can go and enable it to become a virtual host. To enable the new virtual host file, you use the a2ensite helper script which creates a symbolic link from the virtual host file to the sites-enabled directory. Run the commands below to enable the configuration file for our domain. After that, run the commands below to restart the Apache service. Once Apache is restarted, you now browse to the server hostname or IP address and it should display the content file we created above. Repeat this for other domains and Virtual Host files you want to create. You can create as many virtual host files as you can as long as the server can run them all. That should do it! Conclusion: This post showed you how to create Apache Virtual Hosts on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.