hosting image
How-to-Configure-Apache-Virtual-Hosts-on-Ubuntu

How to Configure Apache Virtual Hosts on Ubuntu

The Apache HTTP Server is a widely used, open-source platform that provides robust performance, flexibility, and extensive support for developers. Unlike other systems, Apache’s configuration isn’t contained in a single file. Instead, it uses a modular structure, allowing you to add or modify configuration files as needed. Within this modular framework, you can set up separate sites or domains, known as virtual hosts.

Virtual hosts allow a single Apache server to handle multiple websites simultaneously. Each domain or site configured through Apache points to a specific directory containing its corresponding content. The best part is that users visiting these sites won’t even realize they’re all hosted on the same server. This method is easily scalable, with no inherent software limitations, as long as the server can handle the traffic.

This tutorial teaches you how to set up Apache virtual hosts on an Ubuntu server. By following this guide, you can serve distinct content to different visitors based on the domains they request, setting up two separate virtual host sites.

Step 1 — Set Up the Directory Structure

The first task is to establish the directory structure that will house the content for your websites.

Apache’s document root — the main directory it references to find and serve content — will point to specific folders under the `/var/www` directory. You’ll create a separate folder for each of your virtual hosts here.

Inside each of these directories, you’ll add a `public_html` folder. This is where you’ll place the actual content that visitors will see. The parent directories, such as `your_domain_1` and `your_domain_2`, will store the scripts and application files necessary to support the website’s functionality.

To create your directories, use the following commands, replacing the placeholders with your actual domain names:

sudo mkdir -p /var/www/your_domain_1/public_html

sudo mkdir -p /var/www/your_domain_2/public_html

For example, if your domain is `example.com`, the directory structure would be: `/var/www/example.com/public_html`.

Step 2 — Setting Permissions

Now that you’ve created the necessary directories, they are currently owned by the root user. To enable your regular user to manage files within these web directories, you need to change the ownership. Use the following commands to do so:

sudo chown -R $USER:$USER /var/www/your_domain_1/public_html

sudo chown -R $USER:$USER /var/www/your_domain_2/public_html

The `$USER` variable will automatically represent the username of the account you’re currently logged into. This ensures that your regular user now owns the `public_html` subdirectories where your website content will be stored.

See also  What is PC Over IP? PCoIP Technology Explained

Next, you’ll want to adjust the permissions to ensure that the web server can read the directories and their contents properly. Run the following command:

sudo chmod -R 755 /var/www

This sets the appropriate read and execute permissions, allowing your web server to serve the content while also giving you the ability to manage your files within the required directories. With that done, you’re ready to move on to creating content for your virtual host sites.

Step 3 — Creating Default Pages for Each Virtual Host

With the directory structure set up, it’s time to focus on creating the content for each of your virtual host sites. Start by adding an `index.html` file for your first site, `your_domain_1`.

To create this file, open it with your preferred text editor. In this example, we’ll use nano:

nano /var/www/your_domain_1/public_html/index.html

Inside this file, add a simple HTML structure that will display a welcome message to visitors, indicating which site they’ve reached:

<html>

<head>

<title>Welcome to your_domain_1!</title>

</head>

<body>

<h1>Success! The your_domain_1 virtual host is working!</h1>

</body>

</html>

To save and exit nano, press `CTRL+X`, then confirm by pressing `Y` to save the file. Finally, press `ENTER` to exit.

Now, to create a similar page for your second site, simply copy the file from the first site to the second:

cp/var/www/your_domain_1/public_html/index.html/var/www/your_domain_2/public_html/index.html

Next, open the new file and update the content for `your_domain_2`:

nano /var/www/your_domain_2/public_html/index.html

Modify the HTML as needed to reflect the second site:

<html>

<head>

<title>Welcome to your_domain_2!</title>

</head>

<body>

<h1>Success! The your_domain_2 virtual host is working!</h1>

</body>

</html>

Save and close the file. You now have a unique index page for each site, ready to test your virtual host configuration.

Step 4 — Setting Up Virtual Host Files

Virtual host files are key to configuring how Apache handles requests for your different domains. These files control the specifics of your virtual hosts and tell the web server how to respond to various domain requests.

By default, Apache includes a virtual host configuration file named `000-default.conf`. You can easily duplicate this file to create individual configuration files for each of your domains.

Start by copying the default configuration file to create a new file for your first domain:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your_domain_1.conf

Keep in mind that on Ubuntu, virtual host files must end with `.conf`.

Next, open the newly copied file in your preferred text editor with root privileges:

sudo nano /etc/apache2/sites-available/your_domain_1.conf

The file will initially look something like this, with comments removed:

apache

<VirtualHost *:80>

ServerAdmin webmaster@localhost

DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Now, let’s personalize this configuration for your first domain by making a few changes and adding some useful directives. This configuration will handle requests made on port 80 (the default HTTP port).

See also  How To Find A File In Linux

Start by updating the `ServerAdmin` directive to the email address where the site administrator should receive emails:

apache

ServerAdmin admin@your_domain_1

Next, add two important directives: `ServerName` and `ServerAlias`. The `ServerName` directive sets the base domain name for the virtual host, while `ServerAlias` allows you to include additional names that will point to the same server. For example, `example.com` could have `www.example.com` as an alias, making both domain names resolve to the same server.

Insert these directives right after the `ServerAdmin` line:

apache

ServerName your_domain_1

ServerAlias www.your_domain_1

Then, update the `DocumentRoot` directive to reflect the actual directory where you’re storing the content for this site:

apache

DocumentRoot /var/www/your_domain_1/public_html

Here’s how the updated virtual host file will look:

apache

<VirtualHost *:80>

ServerAdmin admin@your_domain_1

ServerName your_domain_1

ServerAlias www.your_domain_1

DocumentRoot /var/www/your_domain_1/public_html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Once you’ve made these changes, save and close the file.

For your second domain, start by copying the configuration from the first virtual host:

sudo cp /etc/apache2/sites-available/your_domain_1.conf /etc/apache2/sites-available/your_domain_2.conf

Then, open the new file in your text editor:

sudo nano /etc/apache2/sites-available/your_domain_2.conf

Now, modify all the references to `your_domain_1` to reflect the second domain. Once you’re done, your configuration should look like this:

apache

<VirtualHost *:80>

ServerAdmin admin@your_domain_2

ServerName your_domain_2

ServerAlias www.your_domain_2

DocumentRoot /var/www/your_domain_2/public_html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Save and close this file once you’ve completed the changes.

Step 5 — Activating Your Virtual Host Files

With your virtual host configuration files in place, the next step is to activate them. Apache provides a handy tool for this purpose.

You’ll use the `a2ensite` command to enable each of your domains. For more details on how this script works, you can refer to its official documentation. Run the following commands to enable your virtual host sites:

sudo a2ensite your_domain_1.conf

sudo a2ensite your_domain_2.conf

You should see output like this for each domain, reminding you to reload Apache to apply the changes:

Enabling site your_domain_1.

To activate the new configuration, you need to run:

systemctl reload apache2

Before reloading the server, you should disable the default site defined in `000-default.conf` using the `a2dissite` command:

sudo a2dissite 000-default.conf

The output will confirm the action:

Site 000-default disabled.

To activate the new configuration, you need to run:

systemctl reload apache2

Afterward, check for any configuration errors by running:

sudo apache2ctl configtest

If everything is set up correctly, you should see:

Syntax OK

Now, restart Apache to apply the changes:

sudo systemctl restart apache2

To verify that Apache is running smoothly with the new settings, you can check the server status:

sudo systemctl status apache2

At this point, your server should be ready to serve two different websites. If you’re using real domain names, you can skip to Step 7. However, if you’re testing locally, follow Step 6 to learn how to test your configuration on your local machine.

See also  Install OpenSSL on Windows server 2016 A Step-by-Step Guide

Step 6 — (Optional) Modifying the Local Hosts File for Testing

If you’re not using actual registered domain names for testing and have been relying on example domains, you can still simulate how your virtual host sites would behave by adjusting the host file on your local computer. This tweak will direct any requests for the configured domains to your Virtual Private Server (VPS), just as the DNS system would do with real domains. Keep in mind, that this change will only affect your local machine and is purely for testing purposes.

Ensure you’re editing the host file on your local computer, not on your VPS. You’ll need administrative privileges to proceed.

For Mac or Linux users, open the terminal and use the following command to edit the host file:

sudo nano /etc/hosts

Windows users should open the Command Prompt and type:

notepad %windir%\system32\drivers\etc\hosts

Inside the host file, add the public IP address of your server followed by the domain names you wish to use for testing. Using the example domains from this guide and replacing `your_server_IP` with your actual server IP address, the file should look something like this:

127.0.0.1   localhost

127.0.1.1   guest-desktop

your_server_IP your_domain_1

your_server_IP your_domain_2

This will ensure that any requests to `your_domain_1` or `your_domain_2` from your local machine are redirected to your VPS.

Once you’ve added the necessary entries, save and close the file.

Step 7 — Verifying Your Setup

With your virtual hosts now configured, it’s time to test your setup. Open your web browser and visit the domains you’ve set up:

http://your_domain_1

You should see the page you created for your first site, confirming everything is working properly.

Next, check your second domain by visiting:

http://your_domain_2

This should load the page you set up for your second virtual host.

If both domains display as expected, congratulations! You’ve successfully configured two virtual hosts on a single server.

Note: If you edit your local computer’s host file in Step 6 for testing purposes, you may want to remove those entries now that your setup is confirmed. This will keep your host’s file clean and free of unnecessary configurations.

Conclusion

Your server is now capable of handling two distinct domain names. You can easily extend this setup by following the same process to add more virtual hosts. Since Apache has no limit on the number of domains it can manage, you can continue adding as many as your server can handle.

5/5 - (1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Setup Your Server