How To Set Up Apache Virtual Hosts On Debian 8 - DigitalOcean
How To Set Up Apache Virtual Hosts On Debian 8 - DigitalOcean
TUTORIAL
By Brian Hogan
Published on February 6, 2017 ! 175.3k
Introduction
The Apache web server is the most popular way of serving web content on the internet. It
accounts for more than half of all active websites on the internet and is extremely
powerful and flexible.
Apache breaks its functionality and components into individual units you can customize
independently. The basic unit that describes an individual site or domain is called a virtual
host.
Using virtual hosts, you can use one server to host multiple domains or sites off of a
single interface or IP by using a matching mechanism. You configure a request for a
domain to direct the visitor to a specific directory holding that site’s information. In other
We use cookies to provide our services and for analytics and marketing. To find out more about our use
words,
Sign
of upyou
for
cookies, can
please host
oursee more
newsletter
our than
Privacy one
Policy
Get
web
theand
site
Cookie
latest
on
and
tutorials
aon
single server.
Tracking This
Notice.
SysAdmin
scheme
By continuing
and open
is
to browse our
source topics. ×
expandable
website, without
you agree any
to our usesoftware limit as long as your server can handle the load.
of cookies.
Enter your email address
In this tutorial, you’ll set up two Apache virtual hosts on a Debian 8 server, serving
I understand
different content to visitors based on theSign Up they visit. S
SC CR
ROOLLLL T
TOO T
TOOP
P
domain
1 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
Prerequisites
To complete this tutorial, you will need:
• A Debian 8 server with a non-root user with sudo privileges. You can set up a user with
these privileges in our Initial Server Setup with Debian 8 guide.
• Apache installed and configured, as shown in How To Install Linux, Apache, MySQL, PHP
(LAMP) Stack on Debian 8.
In this guide, we’ll create virtual hosts for example.com and test.com , but you can
substitute your own domains or values while following along. To point your domain names
at your server, follow our tutorial How To Set Up a Host Name with DigitalOcean.
If you don’t have domains available to play with, you can use example.com and
test.com and follow Step 5 of this tutorial to configure your local hosts file to map those
domains to your server’s IP address. This will allow you to test your configuration from
your local computer.
Our document root, the top-level directory that Apache looks at to find content to serve,
will be set to individual directories under the /var/www directory. We will create a
directory for each of the virtual hosts we’ll configure.
Within each of these directories, we’ll create a folder called public_html that will hold
the web pages we want to serve. This gives us a little more flexibility in how we deploy
more complex web applications in the future; the public_html folder will hold web
We use cookies to provide our services and for analytics and marketing. To find out more about our use
content
Sign
of up we
for
cookies, wantsee
toour
serve,
our newsletter
please andPolicy
Privacy the parent
Get theand
folder
Cookie
latest andcan
tutorials
hold scripts
Tracking
on Notice.
SysAdmin
or continuing
By
and open
applicationtocode toour
browse
source topics. ×
supportyou
website, web content.
agree to our use of cookies.
2 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
Since we created the directories with sudo , they are owned by our root user. If we want
our regular user to be able to modify files in our web directories, we change the
ownership, like this:
The $USER variable uses the value of the user you are currently logged in as when you
press ENTER . By doing this, our regular user now owns the public_html subdirectories
where we will be storing our content.
We should also modify our permissions a little bit to ensure that read access is permitted
to the general web directory and all of the files and folders it contains so that pages can
be served correctly. Execute this command to change the permissions on the /var/www
folder and its children:
Your web server should now have the permissions it needs to serve content, and your
user should be able to create content within the necessary folders. Let’s create an HTML
file for each site.
We have our directory structure in place. Let’s create some content to serve.
I understand
Let’s start with the page for example.comSign
. EditUp
S
SCCR
ROOLLLL T
TOO T
TOOP
P
a new index.html file with the following
3 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
command:
In this file, create a simple HTML document that indicates that the visitor is looking at
example.com ’s home page:
/var/www/example.com/public_html/index.html
<html>
<head>
<title>Welcome to Example.com !</title>
</head>
<body>
<h11>Success! The example.com virtual host is working!</h1
1>
</body>
</html>
/var/www/test.com/public_html/index.html
We<html>
use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of
<head>
up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest
<title>Welcome to Test.com !</title>
andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
website, you agree to our use of cookies.
</head>
<body> <h1 1>Success! The test.com virtual host is working!</h1 1>
</body>
I understand
</html> Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
4 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
Save and close this file. You now have the pages necessary to test the virtual host
configuration. Next, let’s configure the virtual hosts.
Apache comes with a default virtual host file called 000-default.conf that you can use
as a jumping off point. Copy this file for the first domain:
Note: The default Apache configuration in Debian 8 requires that each virtual host file end
in .conf .
The file will look something like the following example, with some additional comments:
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80
0>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
We use cookies to provide our services and for analytics and marketing. To find out more about our use
of
</VirtualHost>
Sign up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
website, you agree to our use of cookies.
This virtual host matches any requests that are made on port 80 , the default HTTP port.
Let’s make a few changes to this configuration, and add a few new directives.
I understand
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
5 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
First, change the ServerAdmin directive to an email that the site administrator can
receive emails through.
/etc/apache2/sites-available/example.com.conf
ServerAdmin admin@example.com
Next, we need to add two new directives. The first, called ServerName , establishes the
base domain for this virtual host definition. The second, called ServerAlias , defines
further names that should match as if they were the base name. This is useful for
matching additional hosts you defined, so both example.com and www.example.com both
work, provided both of these hosts point to this server’s IP address.
Add these two directives to your configuration file, right after the ServerAdmin line:
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80
0>
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
...
Next, change the location of the document root for this domain by altering the
DocumentRoot directive to point to the directory you created for this host:
Once you’ve made these changes, your file should look like this:
/etc/apache2/sites-available/example.com.conf
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
<VirtualHost *:800>
website, you agree to our use of cookies.
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.comI understand
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
DocumentRoot /var/www/ example.com /public_html
6 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then create the second configuration file by creating a copy of this file:
Then change the relevant settings to reference your second domain. When you are
finished, your file will look like this:
/etc/apache2/sites-available/test.com.conf
<VirtualHost *:80
0>
ServerAdmin admin@test.com
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>
Now that we have created our virtual host files, we can enable them.
7 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
You’ll see the following output if there were no syntax errors or typos in your file:
Output
Enabling site example.com.
To activate the new configuration, you need to run:
service apache2 reload
In order for your changes to take effect, you have to reload Apache. But before you do,
enable the other site:
Output
Enabling site test.com.
To activate the new configuration, you need to run:
service apache2 reload
Next, disable the default site defined in 000-default.conf by using the a2dissite
command:
8 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
This will intercept any requests for the domains that you configured and point them to
your VPS server, just as the DNS system would do if you were using registered domains.
This will only work from your computer though, and is only useful for testing purposes.
Make sure you follow these steps on your local computer, and not your VPS server. You
will also need to know the local 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 Windows, open a Command Prompt with administrative privileges and type:
Once you have the file open, add a line that maps your server’s public IP address to each
domain name, as shown in the following example:
/etc/hosts
127.0.0.1 localhost
...
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
111.111.111.111
please newsletter
example.com
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
111.111.111.111 test.com
website, you agree to our use of cookies.
This will direct any requests for example.com and test.com on your computer and send
I understand
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
them to your server at 111.111.111.111 .
9 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
Save and close the file. Now you can test out your setup. When you’re confident things
are working, remove the two lines from the file.
Likewise, if you can visit your second host at http://test.com , you’ll see the file you
created for your second site:
If both of these sites work well, you’ve successfully configured two virtual hosts on the
same server.
Note:If you adjusted your home computer’s hosts file as shown in Step 5, you may want to
delete the lines you added now that you verified that your configuration works. This will
prevent your hosts file from being filled with entries that are not actually necessary.
Conclusion
You now have a single server handling two separate domain names. You can expand this
process by following these steps to add additional virtual hosts.
There
We use is no software
cookies limit
to provide ouron the number
services and for of domain
analytics names
and Apache
marketing. can
To find handle,
out so feel
more about our use
Sign
free
of upmake
to for
cookies, our
please newsletter
as see
manyouras yourGet
Privacy server
Policy
theandis Cookie
capable
latest ofon
and
tutorials handling.
Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
website, you agree to our use of cookies.
To use Apache to serve secure content, follow the tutorial How To Secure Apache with
Let’s Encrypt on Debian 8. To use Apache in front of your web application, follow How To
I understand
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
Use Apache as a Reverse Proxy with mod_proxy on Debian 8.
10 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
Report an issue
Brian Hogan
I manage the Write for DOnations
program, write and edit community
articles, and make things on the
Internet.
REL ATED
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
please newsletter
our PrivacyGet
Policy and
J o i nthe
Cookie andon
t h elatest
D i g i ttutorials
Tracking Notice.
a l O c e a n CSysAdmin
×
By continuing to browse our
o m m u n i t yand open source topics.
website, you agree to our use of cookies.
J o i n 1 M + o t h e r d eve l o p e r s a n d : I understand
• Get help and share knowledge in Q&A Sign Up
S
SCCR
ROOLLLL T
TOO T
TOOP
P
11 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
• Get courses & tools that help you grow as a developer or small business owner
Join Now
Comments
6 C o m m e n ts
Leave a comment...
Sign In to Comment
0 Does not work. Entering my own domain and still opens the default index page.
Reply Report
12 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
Second, when you restarted Apache, did you see any messages indicating there were
typos in your configuration files?
1
Yeah, it was the browser’s cache. Restarted the browser and it worked. Thanks :)
Reply Report
0 Hello, I’m trying to use this for Debian 9 and am having some trouble. i followed every step
but so far its not working correctly. Currently localhost shows the index.html from site1,
trying to replace localhost in the browser with either site1 or site2 results in a This site
cannot be reached error. Before I started this tutorial /var/www/html was my localhost, now
localhost returns site1 /index which sits at /var/www/site1. What should localhost return?
What could cause localhost to equal site1 and not site2? I did disable 000-default and I
cleared the cache. Any help here would be greatly appreciated.
Reply Report
2 Somehow HowTo’s for Debian on DigitalOcean are better than other tutorials found on the
web. Different authors, but the style is the same. Clear explanations, good instructions, step-
by-step but with sufficient background information to understand what you are doing. I
wonder if the authors use some sort of template or have clear instructions. Oh, and no
apparently no roving of some other contents, including the same errors and the same
omissions.
So many thanks again for this HowTo.
Reply Report
0 Fantastic tuto
Reply Report
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
website, you agree to our use of cookies.
I understand
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
13 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
website, you agree to our use of cookies.
I understand
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
14 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
website, you agree to our use of cookies.
I understand
BECOME A CONTRIBUTOR
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
15 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
DigitalOcean Products Virtual Machines Managed Databases Managed Kubernetes Block Storage
Object Storage Marketplace VPC Load Balancers
Learn More
Company
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open
About to browse our
source topics. ×
website, you agree to our use of cookies. Leadership
© 2021 DigitalOcean, LLC. All rights reserved.
Blog
I understand Careers
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
Partners
16 de 17 08/10/21 09:08
How To Set Up Apache Virtual Hosts on Debian 8 | DigitalOcean https://www.digitalocean.com/community/tutorials/how-to-set-up-apache...
Referral Program
Press
Legal
Security & Trust Center
We use cookies to provide our services and for analytics and marketing. To find out more about our use
Sign
of up for
cookies, oursee
please newsletter
our PrivacyGet
Policy
theand Cookie
latest andon
tutorials Tracking Notice.
SysAdmin By continuing
and open to browse our
source topics. ×
website, you agree to our use of cookies.
I understand
Sign Up S
SCCR
ROOLLLL T
TOO T
TOOP
P
17 de 17 08/10/21 09:08