Apache Virtual Hosting: IP Based and Name Based Virtual Hosts in RHEL/CentOS/Fedora

As we all are aware that Apache is a very powerful, highly flexible and configurable Web server for Nix OS. Here in this tutorial, we are going to discuss one more feature of Apache which allows us to host more than one website on a single Linux machine. Implementing virtual hosting with Apache web server can help you to save costs you are investing on your server maintenance and their administration.

Don’t Miss: NGINX Name-based and IP-based Virtual Hosting (Server Blocks)

Apache Virtual Hosting in Linux

Apache Virtual Hosting in Linux

Concept of Shared web hosting and Reseller web hosting is based on this facility of Apache only.

Types of Virtual Host

There are two types of virtual hosting is available with Apache.

Name Based Virtual Hosting

With the name based virtual hosting you can host several domains/websites on a single machine with a single IP. All domains on that server will be sharing a single IP. It’s easier to configure than IP based virtual hosting, you only need to configure DNS of the domain to map it with its correct IP address and then configure Apache to recognize it with the domain names.

Name Based Virtual Hosting

Name Based Virtual Hosting

IP Based Virtual Hosting

With the IP based virtual hosting, you can assign a separate IP for each domain on a single server, these IP’s can be attached to the server with single NIC cards and as well as multiple NICs.

IP Based Virtual Hosting

IP Based Virtual Hosting

Lets set up Name Based Virtual Hosting and IP based Virtual hosting in RHEL, CentOS and Fedora.

Testing Environment
  1. OSCentOS 6.5
  2. ApplicationApache Web Server
  3. IP Address192.168.0.100
  4. IP Address192.168.0.101
  5. Domainwww.example1.com
  6. Domainwww.example2.com

How to Setup IP Based and Name Based Apache Virtual Hosts

Before setting up virtual hosting with Apache, your system must have Apache Web software installed. if not, install it using default package installer called yum.

[[email protected] ~]# yum install httpd

Setup Name Based Virtual Host

But, before creating a virtual host, you need to create a directory where you will keep all your website’s files. So, create directories for these two virtual hosts under /var/www/html folder. Please remember /var/www/html will be your default Document Root in the Apache virtual configuration.

[[email protected] ~]# mkdir /var/www/html/example1.com/
[[email protected] ~]# mkdir /var/www/html/example2.com/

To set up Name based virtual hosting you must need to tell Apache to which IP you will be using to receive the Apache requests for all the websites or domain names. We can do this with NameVirtualHost directive. Open Apache main configuration file with VI editor.

[[email protected] ~]# vi /etc/httpd/conf/httpd.conf

Search for NameVirtualHost and uncomment this line by removing the # sign in front of it.


Next add the IP with possible in which you want to receive Apache requests. After the changes, your file should look like this:


Now, it’s time to setup Virtual host sections for your domains, move to the bottom of the file by pressing Shift + G. Here in this example, We are setting up virtual host sections for two domains

  1. www.example1.com
  2. www.example2.com

Add the following two virtual directives at the bottom of the file. Save and close the file.

    ServerAdmin [email protected]
    DocumentRoot /var/www/html/example1.com
    ServerName www.example1.com
ErrorLog logs/www.example1.com-error_log
CustomLog logs/www.example1.com-access_log common

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/example2.com
    ServerName www.example2.com
ErrorLog logs/www.example2.com-error_log
CustomLog logs/www.example2.com-access_log common

You are free to add as many directives you want to add in your domains virtual host section. When you are done with changes in httpd.conf file, please check the syntax of files with following command.

[[email protected] ~]# httpd -t

Syntax OK

It is recommended to check the syntax of the file after making some changes and before restarting the Web server because if any syntax goes wrong Apache will refuse to work with some errors and eventually affect your existing web server go down for a while. If syntax is OK. Please restart your Web server and add it to chkconfig to make your web server start in runlevel 3 and 5 at the boot time only.

[[email protected] ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[[email protected] ~]# chkconfig --level 35 httpd on

Now it’s time to create a test page called index.html add some content to the file so we will have something to check it, when the IP calls the virtual host.

[[email protected] ~]# vi /var/www/html/example1.com/index.html
    <h1>Hello, Welcome to www.example1.com.</h1>
[[email protected] ~]# vi /var/www/html/example2.com/index.html
    <h1>Hello, Welcome to www.example2.com.</h1>

Once you’re done with it, you can test the setup by accessing both the domains in a browser.

Preview: www.example1.com
Virtual Hosting: www.example1.com

Virtual Hosting: www.example1.com

Preview: www.example2.com
Virtual Hosting: www.example2.com

Virtual Hosting: www.example2.com

Setup IP Based Virtual Hosting Linux

To setup IP based virtual hosting, you must have more than one IP address/Port assigned to your server or your Linux machine.

It can be on a single NIC card , For example: eth0:1, eth0:2, eth0:3 … so forth. Multiple NIC cards can also be attached. If you don’t know how to create multiple IP’s on single NIC, follow the below guide, that will help you out in creating.

  1. Create Multiple IP Addresses to One Single Network Interface

Purpose of implementing IP based virtual hosting is to assign implementing for each domain and that particular IP will not be used by any other domain.

This kind of set up required when a website is running with SSL certificate (mod_ssl) or on different ports and IPs. And You can also run multiple instances of Apache on a single machine. To check the IPs attached in your server, please check it using ifconfig command.

[email protected] ~]# ifconfig
Sample Output
eth0      Link encap:Ethernet  HWaddr 08:00:27:4C:EB:CE  
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::a00:27ff:fe4c:ebce/64 Scope:Link
          RX packets:17550 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15120 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:16565983 (15.7 MiB)  TX bytes:2409604 (2.2 MiB)

eth0:1    Link encap:Ethernet  HWaddr 08:00:27:4C:EB:CE  
          inet addr:  Bcast:  Mask:

lo        Link encap:Local Loopback  
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1775 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1775 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3416104 (3.2 MiB)  TX bytes:3416104 (3.2 MiB)

As you can see in above output, two IPs (eth0) and (eth0:1) is attached to the server, both IPs are assigned to the same physical network device (eth0).

Now, assign a specific IP/Port to receive http requests, you can simply do it by changing Listen directive in httpd.conf file.

[[email protected] ~]# vi /etc/httpd/conf/httpd.conf

Search for word “Listen”, You find a section where the short description about Listen directive is written. In that section, comment the original line and write your own directive below that line.

# Listen 80


Now,  create a Virtual host sections for both the domains. Go the bottom of the file and add the following virtual directives.

    ServerAdmin [email protected]
    DocumentRoot /var/www/html/example1
    ServerName www.example1.com
ErrorLog logs/www.example1.com-error_log
TransferLog logs/www.example1.com-access_log

    ServerAdmin [email protected]
    DocumentRoot /var/www/html/example2
    ServerName www.example2.com
ErrorLog logs/www.example2.com-error_log
TransferLog logs/www.example2.com-access_log

Now, since you have modified main Apache conf file, you need to restart the http service like below.

[[email protected] ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

Test your IP based Virtual hosting setup by accessing the URLs on web browser as shown below.


That’s all with Apache virtual host today, If you’re looking to secure and harden your Apache configuration, then read our article that guides.

  1. 13 Apache Web Server Security and Hardening Tips

Reference Links

Apache Virtual Host Documentation

I’ll be again come with some other Apache tips and trick in my future articles, till then Stay Geeky and connected to Tecmint.com. Do not forget to leave your suggestions about the article in our comment section below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Tarunika Shrivastava

I am a linux server admin and love to play with Linux and all other distributions of it. I am working as System Engineer with a Web Hosting Company.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

51 Responses

  1. rajendra says:

    I am not getting the line NameVirtualHost in vi /etc/httpd/conf/httpd.conf file.

  2. Zero says:

    there is no httpd.conf and no httpd to install as
    PRETTY_NAME=”Debian GNU/Linux 9 (stretch)”
    NAME=”Debian GNU/Linux”
    VERSION=”9 (stretch)”
    when try to install it
    sudo apt install httpd
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package httpd is a virtual package provided by:
    apache2 2.4.25-3+deb9u2
    yaws 2.0.4+dfsg-1
    webfs 1.21+ds1-12
    tntnet 2.2.1-3+b1
    ocsigenserver 2.7-1+b4
    nginx-light 1.10.3-1+deb9u1
    nginx-full 1.10.3-1+deb9u1
    nginx-extras 1.10.3-1+deb9u1
    mini-httpd 1.23-1.2
    micro-httpd 20051212-15.1
    lighttpd 1.4.45-1
    ebhttpd 1:1.0.dfsg.1-4.3+b2
    aolserver4-daemon 4.5.1-18.1
    aolserver4-core 4.5.1-18.1
    You should explicitly select one to install.

    E: Package ‘httpd’ has no installation candidate

  3. Mirzet Kadic says:

    Isn’t it better to use /var/www/domain.tld/public_html instead of /var/www/html/domain.tld/public_html because 000-default.conf points to /var/www/html and if enabled you can jump to other sites document if you know the folder name.

  4. Mallikarjuna reddy says:

    Hi, thanks for the material but thing is I followed the steps what you provided for name-based web server but I am not able to get the output can you please provide the full clear material to my mail [email protected]

    • Ravi Saive says:


      What output you getting and where? could you share here so that we can provide more better solution on Apache named-based setup.

  5. ki says:

    Purpose of implementing IP based virtual hosting is to assign implementing for each domain and that particular IP will not be used by any other domain. *didn’t understand*

  6. arun natarajan says:

    I have tried your steps, but not able to see the output. config files are correct, services are running, selinux is disabled, still no success.

    Please let me know the step to get output. my workaround.

    [[email protected] oracle]$ uname -a
    Linux suman.example.com 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
    [[email protected] oracle]$
    [[email protected] oracle]$ httpd -t
    Syntax OK
    [[email protected] oracle]$
    [[email protected] oracle]$ service httpd status
    httpd (pid  2057) is running...
    [[email protected] oracle]$

    Example1.com output

    [[email protected] oracle]$ cat /var/www/html/example1.com/index.html
    hi suman
    welcome to linux training

    Example1.com Output

    [[email protected] oracle]$ cat /var/www/html/example2.com/index.html
    welcome to example2 website
    • Ravi Saive says:


      May I know what output you getting while accessing these domains via web browser? Also could you share your VirtualHost configuration for these domains in httpd.conf file?

  7. Norbert says:

    I have one domain, do I need to add my IP address of server to their control panel (for their DNS ) ?

    Do I need to install DNS server or apache is enough ?

    Thank you


    • Ravi Saive says:


      Just point the domain to IP address of the server in your Hosting DNS control panel, no need to install DNS server, onl Apache is enough to run your website..

  8. Arjun Pawar says:

    i want to redirect my website from http to https. i changed many configuration like .htaccess file and httpd file but i am not getting solution. could help me.

    • Ravi Saive says:


      Create a .htaccess file in root of domain or website that you want to redirect to HTTPS and add the following rules.

      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
      • Arjun Pawar says:

        I have already added this code in .htaccess file in website root but i am not getting solution. i want to know you i am also unable to open website in local network from http with domain name but i able open http with ip domain address.

        • Ravi Saive says:


          Make sure that “AllowOverride All” is set in your httpd.conf file and also make sure you have added DNS ‘A’ record for you domain and point back to the server IP address.

  9. Widyachacra says:

    No it shouldn’t be the same hostname.

    You don’t have to use the same local FQDN as your vhost FQDN(ServerName). Create an ‘CNAME’ record lets say ‘www2 CNAME http://www.example1.com‘ & put the ServerName www2.example1.com. Thats all. 8-)

  10. Mrutyunjaya says:

    hii.. please help me . i want to know what is your FQDN name. if your FQDN is http://www.example1.com then, is your host name is www ???

    • Bhasker says:

      I expect FQDN can be example1.mycompany.com then domain is mycompany and example1 is host name. FQDN is nothing but host name along with domain name.

Got something to say? Join the discussion.

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.