How to Configure Apache Virtual Hosts on Ubuntu

1,107 Views

You will also need to have Apache installed in order to perform these steps. If you haven’t done so yet, you can install Apache on your server using the package method apt:

If you want more detailed instructions as well as firewall configuration, please refer to our How To Install Apache Web Server on Ubuntu 18.04 guide .

We’ll show you later how to edit your local hosts file to test the configuration if you’re using test values. This will allow you to validate your configuration from your personal computer, but your content will not be accessible to other visitors via the domain name.

Step One – Create the Directory Structure

The first step we are going to take is to create a directory structure that will contain the site data that we are going to serve to visitors.

We also need to modify our permissions to ensure that read access is allowed to the general web directory and all files and folders within it, so that pages can be served correctly:

Your web server should now have the necessary permissions to serve content, and your user should be able to create content in the necessary folders.

Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the Apache web server will respond to various domain requests.

Apache comes with a default virtual host file called 000-default.confthat we can use as a starting point. We will copy it in order to create a virtual host file for each of our domains.

We’ll start with one domain, set it up, copy it over to our second domain, and then make any additional adjustments needed. Ubuntu’s default configuration requires each virtual host file to end with .conf.

After that, we need to add two directives. This will most likely be your domain. The second, called Server Alias, defines other names that must match as if they were the base name. This is useful for matching hosts you have defined, such as www:

Copy the first virtual host and customize it for the second domain

Now that we have established our first virtual host file, we can create a second one by copying this file and adjusting it as needed.

We will use the tool a2ensiteto activate each of our sites. If you want to know more about this scenario, you can refer to the documentationd’a2ensite .

When you are done, you must restart Apache for these changes to take effect and use l’état system ctlto verify the success of the restart.

Your server should now be configured to serve two websites.

Step Six – Set Up the Local Hosts File (Optional)

If you did not use actual domain names that you own to test this procedure and instead used a few example domains, you can at least test the functionality of this procedure by temporarily editing the file d’hôteson your local computer .

This will intercept all requests for the domains you have configured and direct them to your VPS server, just like the DNS system would if you were using registered domains. This will only work from your local computer, however, and for testing purposes only.

Make sure you are working on your local computer for these steps and not on your VPS server. You must know the computer’s administrative password or be a member of the administrative group.

If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:

If you’re on a Windows machine, you ‘ll find instructions here for editing your hosts file .

The details you need to add are your server’s public IP address followed by the domain you want to use to reach that server.

Using the domains shown in this guide, and replacing your server IP with the text your_server_IP, your file should look like this:

This will direct any request from exemple.comand test.comto our computer and send it to our server. This is what we want if we are not actually the owners of these domains in order to test our virtual hosts.

Leave a Reply