This post shows students and new users the steps to configure an Apache Virtual Host file on Ubuntu Linux. A virtual host is an Apache feature that allows users to run more than one website on a single server.

A virtual host file contains configuration directives for the website, including the site document root, security policies, SSL certificate settings, and much more. Every website. Websites that are configured with Apache Virtual Host work independently of each other with separate and unique settings.

The Apaches Virtual Host feature enables the webmaster to maximize the resources of a server by running multiple websites on one host instead of multiple hosts running multiple websites.

How to create website directory structures in Ubuntu Linux

When you run multiple websites on a single host, each website will have its own document root. The root of a document is the directory where website files for a domain name are stored and served in response to requests.

Here 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 own folder with a document root included.

Example: / var / www /domain/ public_html

Run the following commands to create a directory for the example.com domain with your document root.

sudo mkdir -p /var/www/example.com/public_html

Each document root will need an index.html file that will be displayed to clients. Run the following commands to create an index.html file.

sudo nano /var/www/example.com/public_html/index.html

Then copy and paste the content below into the file and save it.

<!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 are ready to configure Apache Virtual Host to reference this content. To avoid permission issues, change 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 have created a domain content in the above directory, go ahead and configure the Apache Virtual Host configuration file for the above domain content.

YOU CAN ALSO READ:   How to install Skype on Ubuntu Linux

On Ubuntu Linux, the Apache Virtual Hosts configuration files are located in the / etc / apache2 / sites-available directory.

To create a virtual host file in the sites directory available for our content above, run the following commands to create a site-specific virtual host configuration file

sudo nano /etc/apache2/sites-available/example.com.conf

Here is an example configuration that should work with most environments. Copy and paste the content below into the file above and save it.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    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 go ahead and enable it to become a virtual host.

To enable the new virtual host file, use the a2ensite helper script that creates a symbolic link from the virtual host file to the enabled sites directory.

Run the following commands to enable the configuration file for our domain.

sudo a2ensite example.com.conf

After that, run the following commands to restart the Apache service.

sudo systemctl restart apache2

Once Apache restarts it now navigates to the server hostname or IP address and it should display the content file we created earlier.

Repeat this for other domains and virtual host files that you want to create. You can create as many virtual host files as you can as long as the server can run all of them.

You should do that!

conclusion:

In this tutorial we have seen how to create Apache virtual hosts on Ubuntu Linux. If you find any errors above or have something to add, use the comment form below.

YOU CAN ALSO READ:   How to install PostgreSQL on Ubuntu Linux

Write A Comment