How to set up an Apache virtual host on Ubuntu 16.04

Introduction

The Apache web server is the most popular way to provide web content on the Internet. It accounts for more than half of all active websites on the Internet, and is very powerful and flexible.

Apache breaks down its functions and components into individual units that can be individually customized and configured. The basic unit describing a single site or domain is called a virtual host.

These assignments allow administrators to use a matching mechanism to host multiple domains or sites from a single interface or IP using one server. This is relevant for anyone who wants to host multiple sites on a single VPS.

Each domain configured will direct visitors to a specific directory that holds information about that site, never instructing the same server to be responsible for other sites. As long as your server can handle the load, this solution can be expanded without any software restrictions.

In this guide, we will show you how to set up an Apache virtual host on an Ubuntu 16.04 VPS. In this process, you will learn how to provide different content to different visitors based on the requested domain.

prerequisites

Before starting this tutorial, you should create a non-root user. Those who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab experiment, learn to install and then buy server.

You also need to install Apache to complete these steps. If you have not already done so, you can use apt-get to install Apache on the server in the following way:

sudo apt-get update
sudo apt-get install apache2

After completing these steps, we can start.

For the purpose of this guide, we will configure a virtual host for example.com and test.com. These will be referenced throughout the guide, but you should substitute your own domains or values when following.

If you don't have a domain name, I suggest you go here register a domain name.

Later we will show how to edit the local hosts file to test the configuration when using dummy values. This way you can test the configuration on your home computer, even if your content is not available to other visitors through the domain name.

Step 1-Create the directory structure

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

Our document root (the top-level directory that Apache looks for to find what to provide) will be set to various directories under the /var/www directory. We will create a directory here for the two virtual hosts we plan to make.

In each of these directories, we will create a public_html folder that will hold our actual files. This provides some flexibility for our hosting.

For example, for our website, we will create a directory like this:

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

The red part represents the domain name we want to provide from our VPS.

Step 2-Grant permissions

Now we have a directory structure of files, but they are owned by the root user. If we want our regular users to be able to modify the files in our web directory, we can change the ownership by doing this:

sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/test.com/public_html

When you press Enter, the $USER variable will adopt the value of the user you are currently logged in as. By doing this, our regular users now have the public_html subdirectory where we will store the content.

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

sudo chmod -R 755/var/www

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

Step 3-Create a demo page for each virtual host

We have our directory structure. Let's create some service content.

We are doing a demo, so our page will be very simple. We just make an index.html page for each website.

Let's start with example.com. We can open the index.html file in the editor by typing the following:

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

In this file, create a simple HTML document indicating the site to which it is connected. My file looks like this:

< html><head><title>Welcome to Example.com!</title></head><body><h1>Success!  The example.com virtual host is working!</h1></body></html>

Save and close the file when you are done.

We can copy this file to use as the basis for the second site by entering the following:

cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html

Then we can open the file and modify the related information:

nano /var/www/test.com/public_html/index.html
< html><head><title>Welcome to Test.com!</title></head><body><h1>Success!  The test.com virtual host is working!</h1></body></html>

Save and close this file. You now have the pages needed to test the virtual host configuration.

Step 4-Create a new virtual host file

The virtual host file is a file that specifies the actual configuration of the virtual host and instructs the Apache web server how to respond to various domain requests.

Apache comes with a default virtual host file called 000-default.conf, which we can use as an exit point. We will copy it to create a virtual host file for each domain.

We will start with one domain, configure it, copy it to our second domain, and then make some further adjustments. The default Ubuntu configuration requires each virtual host file to end with .conf.

Create the first virtual host file

First copy the files of the first domain:

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

Open the new file in the editor with root privileges:

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

The file looks like this (I removed the comments here to make the file more approachable):

< VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/html
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
< /VirtualHost>

As you can see, there is not much content here. We will customize the project for our first domain here and add some other instructions. This virtual host part matches any requests made on port 80 (the default HTTP port).

First, we need to change the ServerAdmin instruction to an email that the webmaster can receive via email.

ServerAdmin [email protected]

After this, we need to add two instructions. The first command is named ServerName, which establishes a basic domain that should match this virtual host definition. This is most likely your domain name. The second is named ServerAlias, which defines other names that should be matched as if they were base names. This is useful for matching hosts you define, for example www:

ServerName example.com
ServerAlias www.example.com

The only other thing we need to change for the basic virtual host file is the location of the document root for that domain. We have created the directory we need, so we only need to modify the DocumentRoot directive to reflect the directory we created:

DocumentRoot /var/www/example.com/public_html

In general, our virtualhost file should look like this:

< VirtualHost *:80>
 ServerAdmin [email protected]
 ServerName example.com
 ServerAlias www.example.com
 DocumentRoot /var/www/example.com/public_html
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
< /VirtualHost>

Save and close the file.

Copy the first virtual host and customize the second domain

Now that we have created the first virtual host file, we can create the second virtual host file by copying the file and adjusting as needed.

First copy it:

sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf

Open the new file with root privileges in the editor:

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

You now need to modify all the information to reference your second domain. When finished, it might look like this:

< VirtualHost *:80>
 ServerAdmin [email protected]
 ServerName test.com
 ServerAlias www.test.com
 DocumentRoot /var/www/test.com/public_html
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
< /VirtualHost>

Save and close the file when you are done.

Step 5-Enable the new virtual host file

Now that we have created the virtual host files, we must enable them. Apache includes some tools that allow us to do this.

We can use the a2ensite tool to enable each of our websites as follows:

sudo a2ensite example.com.conf
sudo a2ensite test.com.conf

Next, disable the default site defined by 000-default.conf:

sudo a2dissite 000-default.conf

Once completed, Apache needs to be restarted for these changes to take effect:

sudo systemctl restart apache2

In other documents, you can also use the following service command to see examples:

sudo service apache2 restart

This command will still work, but it may not give you the output you saw on other systems before, because it is now the systemctl of the wrapper of systemd.

Step 6-Set up the local host file (optional)

If you are not using an actual domain name you own to test this process and have been using some example domains, you can at least test the functionality of this process by temporarily modifying the hosts file on your local computer.

This will intercept any requests for your configured domain and point them to your VPS server, just like the DNS system does when you use a registered domain. This can only be run on your computer and is for testing purposes only.

Make sure you run these steps on your local computer and not the VPS server. You need to know the administrative password of the computer or become a member of the administrative group in other ways.

If you are using a Mac or Linux computer, type the following command to edit a local file with administrative rights:

sudo nano /etc/hosts

If you are using a Windows computer, you can find instructions on changing the host file inhere.

The details you need to add are the public IP address of the VPS server, followed by the domain you want to use to access the VPS.

For the domain I used in this guide, assuming my VPS IP address is 111.111.111.111, I can add the following line to the bottom of the hosts file:

127.0.0.1 localhost
127.0.1.1 guest-desktop
111.111.111.111 example.com
111.111.111.111 test.com

This will instruct any request for example.com and test.com on our computer and send them to 111.111.111.111 located on our server. If we are not actually the owners of these domains in order to test our virtual hosts, then this is what we want.

Save and close the file.

Step 7-Test your results

Now that you have configured the virtual host, you can easily test your settings by going to the domain configured in the web browser:

http://example.com

You should see a page like this:

Similarly, if you can visit the second page:

http://test.com

You will see the files created for the second site:

If both sites are running well, you have successfully configured two virtual hosts on the same server.

If you have adjusted the host file of your home computer, you may need to delete the line that is now added to verify that the configuration is valid. This will prevent your hosts file from being filled with actually unnecessary entries.

If you need long-term access, please consider purchasing a domain name for each site you need and set it to point to your VPS server

in conclusion

If you continue, you should now have one server handling two separate domain names. You can extend this process by following the steps outlined above to create other virtual hosts.

There is no software limit on the number of domain names that Apache can handle, so you can use the number of domain names that the server can handle at will.

For more Ubuntu tutorials, please go to Tencent Cloud + Community to learn more.


Reference: "How To Set Up Apache Virtual Hosts on Ubuntu 16.04"

Recommended Posts

How to set up an Apache virtual host on Ubuntu 16.04
How to set up an Apache virtual host on Ubuntu 20.04
How to set up an Apache virtual host on CentOS 7
How to set up Gogs on Ubuntu 14.04
How to set up R on Ubuntu 14.04
How to set up Shiny Server on Ubuntu 14.04
How to set up time synchronization on Ubuntu 18.04
How to set up a DNS server on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to set up Java Home on Ubuntu and Raspbian
How to set up a Masterless Puppet environment on Ubuntu 14.04
How to set up a firewall with UFW on Ubuntu 14.04
How to set up vsftpd for anonymous downloads on Ubuntu 16.04
How to set up a production Elasticsearch cluster on Ubuntu 14.04
How to set up password authentication with Nginx on Ubuntu 14.04
How to set up vsftpd for user directories on Ubuntu 16.04
How to set PostgreSQL startup on Ubuntu 16.04
How to install Apache Maven on Ubuntu 20.04
How to install Apache Tomcat 8 on Ubuntu 16.04
How to set static IP on Ubuntu 18.04 Server
How to set static IP on Ubuntu 18.04 Server
How to set up SSH keys on CentOS 8
Explain how to set static IP on ubuntu14.04
How to configure Apache content caching on Ubuntu 14.04
How to protect Apache with Let&#39;s Encrypt on Ubuntu 16.04
Detailed steps to configure Ubuntu 16.04 and Apache virtual host
How to set a fixed IP based on Ubuntu 16.04
How to Run Tmux Service Scripts on Ubuntu Start Up
How to install Ruby on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to set or modify the time zone on Ubuntu 20.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to deploy Django on Ubuntu 14.04
How to install R on Ubuntu 20.04
How to set or modify the time zone on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04