This post shows students and new users how to configure an Apache Virtual Host file on Ubuntu Linux. A Virtual Host is a feature of Apache that allows users to run more than one website on a single server.
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 configured within an Apache Virtual Host operates independently of each other with separate and unique settings.
Apaches Virtual Host feature allows web admins 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
Each website will have its document root when running multiple websites on a single host. 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.
/var/www/ ├── example.com │ └── public_html ├── example.net │ └── public_html
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.
sudo mkdir -p /var/www/example.com/public_html
Each document root will need an index.html file to be shown to clients. Run the commands below to create an index.html file for the example.com domain.
sudo nano /var/www/example.com/public_html/index.html
Then copy and paste the content below into the file and save it. Below is just a basic HTML file for testing purposes.
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Welcome to example.com</title> </head> <body> <h1>Success! example.com home page!</h1> </body> </html>
Once you save the file, we can 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) :
sudo chown -R www-data: /var/www/example.com
How to create an Apache Virtual Host file on Ubuntu Linux
Now that you’ve created domain content in the directory above configure the Apache Virtual Host configuration file for the above domain content.
On Ubuntu Linux, Apache Virtual Hosts configuration files are 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.
sudo nano /etc/apache2/sites-available/example.com.conf
An example configuration that should work with most environments is shown below. Copy and paste the content below into the file above and save.
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com ServerAdmin [email protected] DocumentRoot /var/www/example.com/public_html <Directory /var/www/example.com/public_html> Options -Indexes +FollowSymLinks AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined </VirtualHost>
Once the file is saved, you can 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.
sudo a2ensite example.com.conf
After that, run the commands below to restart the Apache service.
sudo systemctl restart apache2
Once Apache is restarted, you browse to the server hostname or IP address, which 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 long as the server can run them all.
That should do it!
Conclusion:
- Configuring Apache Virtual Hosts on Ubuntu Linux allows you to efficiently manage multiple websites from a single server.
- Each website operates independently with its own unique settings and configurations.
- Proper directory structures and document roots are essential for serving website content correctly.
- Remember to adjust ownership and permissions to avoid access issues for your web server.
- Use the
a2ensite
command to easily enable new virtual hosts and manage your configurations. - Restarting Apache is crucial to apply the changes and ensure the new settings are active.
- With these steps, you can expand your server’s capabilities and support additional websites as needed.
Leave a Reply