How to set up Apache Virtual Hosts on Debian 9

How to set up Apache Virtual Hosts on Debian 9

In this tutorial, we will show you how to set up Apache virtual hosts on Debian 9. Apache is a free and open source web server. It is the most popular and widely used web server in the world, and it is commonly used in Linux servers. It is developed and maintained by Apache Software Foundation, over half of all servers around the world are running this fast and secure web server.

Requirements

– SSH access with root privileges to your Debian 9 server
– Properly installed and configured Apache web server

1. Login to your server

First, let’s log in to your server via SSH:

ssh root@server_ip

2. Check for Apache installation

Check whether apache is already installed and running on your server. You can do this with the following command:

dpkg -l apache2

If apache is not installed, you can do this by running the following commands. First, make sure that the system repositories are up to date:

apt-get update

To install the Apache web server, execute the following:

apt-get install apache2

After the installation is complete, you should enable Apache to start automatically upon server reboot with:

systemctl enable apache2

You can also check the status of your Apache service with the following command:

systemctl status apache2

Now that we are sure that Apache is installed and running on our server we can continue with the next step and set up our first virtual host.

3. What is a virtual host?

Apache virtual hosts are set of configuration directives which allow you to host as many websites as you want, using a single web server.Apache web server tsupports two types of virtual hosts:

Name-based virtual hosts
IP based virtual hosts

The name-based virtual host is commonly used to host multiple websites on the same server, while in IP based virtual host we can only configure one website on one IP address. In this tutorial will show you how to create name-based virtual hosts. For this purpose, we will host two websites using the following domain names, domain1.com and domain2.com. You can also replace them with your actual domain names.

4. Create the webroot directories

Before setting up the virtual hosts, we will need to create the document root directories for our websites. Let’s create them in the /var/www/html directory with the following commands:

mkdir -p /var/www/html/domain1.com
mkdir -p /var/www/html/domain2.com

Let’s also create a test demo page for each of our domain, so we can later test our configuration.
Navigate to the domain1.com document root directory:

cd /var/www/domain1.com

Create a new index.html page with:

nano index.html

And add the following content:

<html>
 <body>
  <center><h1>This is domain1.com!</h1></center>
 </body>
</html>

Now lets, do the following for the domain2.com domain.

cd /var/www/domain2.com
nano index.html

And add the following content:

<html>
 <body>
  <center><h1>This is domain2.com!</h1></center>
 </body>
</html>

We have now successfully created the test pages for both domains. In order for our Apache webserver to be able to access these files, we also need to give them appropriate permissions and set the user and group to the www-data. We update the permissions to the whole /var/www/html directory, with the following command.

chown -R www-data: /var/www/html

5. Create the Virtual Host Files

We can now create our virtual host files. The virtual host configuration files usually end with .conf extension.
Run the following command to create the virtual host configuration file for our first domain, domain1.com:

nano /etc/apache2/sites-available/domain1.com.conf

And add the following content to the file:

<VirtualHost *:80>

ServerAdmin admin@domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/www/html/domain1.com

ErrorLog ${APACHE_LOG_DIR}/domain1.com_error.log
CustomLog ${APACHE_LOG_DIR}/domain2.com_access.log combined

</VirtualHost>

Now, let’s do the same for our second domain name, domain2.com:

nano /etc/apache2/sites-available/domain2.com.conf

And add the following code:

<VirtualHost *:80>

ServerAdmin admin@domain2.com
ServerName domain2.com
ServerAlias www.domain2.com
DocumentRoot /var/www/html/domain2.com

ErrorLog ${APACHE_LOG_DIR}/domain2.com_error.log
CustomLog ${APACHE_LOG_DIR}/domain2.com_access.log combined

</VirtualHost>

Here is a short explanation about each line in our virtual host files.

The following lines shows that the virtual host is listening on port 80:

<VirtualHost *:80>

The ServerAdmin sets the contact address that the server includes in any error messages it returns to the client. You can specify your email address here, or even remove the line.

ServerAdmin admin@domain1.com

ServerName is the domain name and the ServerAlias defines additional names that should match as if they are the original domain names.

ServerName domain1.com
ServerAlias www.domain1.com

The DocumentRoot defines the location where Apache should look for when processing a request for the domain defined in ServerName or ServerAlias.

DocumentRoot /var/www/html/domain1.com

The last two lines, define the location of the log files:

ErrorLog ${APACHE_LOG_DIR}/domain1.com_error.log
CustomLog ${APACHE_LOG_DIR}/domain1.com_access.log combined

6. Enabling the virtual hosts

The next step would be to enable the virtual hosts we have just created. You can do this with the following commands:

a2ensite domain1.com.conf
a2ensite domain2.com.conf

Another alternative way to do this is by creating a symbolic link for each virtual host in /etc/apache2/sites-enabled

ln -s /etc/apache2/sites-available/domain1.com.conf /etc/apache2/sites-enabled/
ln -s /etc/apache2/sites-available/domain2.com.conf /etc/apache2/sites-enabled/

Once you enable the virtual hosts, using either of above methods, you will need to restart the Apache web server:

systemctl restart apache2

That’s it. If you all the instructions properly you should now be able to open each of the domains in your web browser and get the following result.

http://domain1.com

set up apache virtual hosts debian 9

 

 

 

 

 

 

 

 

http://domain2.com

apache virtual hosts set up on debian 9


Of course, you don’t need to set up Apache Virtual Hosts on Debian 9, if you use one of our Debian VPS Hosting services in which case, our technical support team will help you creating the virtual hosts immediately. They are available 24/7, and can cater to any questions or requests.

PS. If you liked this post on How to set up Apache Virtual Hosts on Debian 9, feel free to share it with your friends by using the social media share shortcuts below, or simply leave a comment. Thanks.

4 thoughts on “How to set up Apache Virtual Hosts on Debian 9”

    • You can use the same instructions to add a subdomain.
      Just add your subdomain at the “ServerName” line when creating the virtual host file. For example:
      ServerName test.domain1.com

      Reply
  1. How I have to setup virtual ports if i have just IP and I want to have like main IP on port 80 and another on port 81. or on ports 81 and 82. thanks for answer

    Reply
    • You need to open a port in ports.conf file
      and change the port in your virtual host. Example:
      <VirtualHost *:81>
      Also, you should add the port on the browser. http://your_IP_address:81

      Reply

Leave a Comment