Apache Virtual Hosting IP Based and Name Based Virtual Hosts

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 Apachewhich 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.

Apache Virtual Hosting in Linux

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

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

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

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

Testing Environment
  1. OS – CentOS 6.5
  2. Application – Apache Web Server
  3. IP Address – 192.168.0.100
  4. IP Address – 192.168.0.101
  5. Domain – www.example1.com
  6. Domain – www.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.

NameVirtualHost

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

NameVirtualHost 192.168.0.100:80

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.

<VirtualHost 192.168.0.100:80>
    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>

<VirtualHost 192.168.0.100: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
</VirtualHost>

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 isOK. Please restart your Web server and add it to chkconfig to make your web server start inrunlevel 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
<html>
  <head>
    <title>www.example1.com</title>
  </head>
  <body>
    <h1>Hello, Welcome to www.example1.com.</h1>
  </body>
</html>
[[email protected] ~]# vi /var/www/html/example2.com/index.html
<html>
  <head>
    <title>www.example2.com</title>
  </head>
  <body>
    <h1>Hello, Welcome to www.example2.com.</h1>
  </body>
</html>

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

http://www.example1.com

http://www.example2.com
Preview: www.example1.com

Virtual Hosting: www.example1.com

Preview: 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:1eth0:2eth0:3 … so forth. Multiple NICcards 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:192.168.0.100  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe4c:ebce/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          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:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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 192.168.0.100 (eth0) and 192.168.0.101 (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 Listendirective 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

Listen 192.168.0.100:80

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

VirtualHost 192.168.0.100:80>
    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
</VirtualHost>

<VirtualHost 192.168.0.101:80>
    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
</VirtualHost>

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.

http://www.example1.com

http://www.example2.com

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.

Apache Virtual Hosting IP Based and Name Based Virtual Hosts,布布扣,bubuko.com

时间: 2024-10-12 19:13:12

Apache Virtual Hosting IP Based and Name Based Virtual Hosts的相关文章

Apache配置基于IP的虚拟主机 Apache virtual host configuration is based on the IP

Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Virtual hosts Include conf/extra/httpd-vhosts.conf 如果没有开启,必须在httpd.conf文件中设置:如果开启,则可以在apache/conf/extra/httpd-vhosts.conf文件中设置,当然也还是可以再httpd.conf文件中进行设置,同样有效. Step 2: httpd.conf文件 DocumentRo

apache禁止使用IP访问的实现方法

apache禁止访问目录列表 apache禁止访问目录列表对于开发人员来说还是蛮实用的,可以迅速查找根目录下的所有项目,但如果一个挂在互联网上的服务器为了提高安全性就必须禁止访问目录列表. 找到Apache 的 httpd.conf 配置文件将: XML/HTML代码 Options Indexes FollowSymLinks 改为:Options FollowSymLinks 也就是把 Indexes 去掉. 额外信息:在apache中设置虚拟主机 在IIS中我们可以方便的通过不同的IP或者

apache禁止用ip直接访问

apache禁止用ip直接访问,可以防止恶意域名指向你的网站 Apache的虚拟主机配置 <VirtualHost *:80> <Location /> Order deny,allow Deny from all        ##屏蔽所有访问 </Location> </VirtualHost> <VirtualHost *:80> DocumentRoot htdocs ServerName www.12.com </VirtualH

nginx前端负载,后端apache获取真实IP设置

原文链接: nginx前端负载,后端apache获取真实IP设置 参考文献: 前端Nginx,后端Apache获取用户真实IP地址  按照第二种方法设置不成功! 网站最前端是nginx,做的PROXY来转发请求到后端的apache上,所以获取的IP 都是NGINX PROXY 机器的IP,这对于分析和记录apache日志等很多基于IP判断的PHP程序都会出现一系列的问题,经过以下两处理便可让apache获取 正式的访问地址:1.修改NGINX的配置文件NGINX.CONF 在里面加上 proxy

配置apache允许所有ip访问

打开  apache安装目录/conf/httpd.conf 配置文件 搜索Listen监听端口,修改如下 # # Listen: Allows you to bind Apache to specific IP addresses and/or# ports, instead of the default. See also the <VirtualHost># directive.## Change this to Listen on specific IP addresses as sh

用Apache实现一个ip虚拟多个web站点

如何用Apache实现一个ip虚拟多个web站点? 首先添加虚拟的服务器名 <virtualhost www.xxx.com:80="">DocumentRoot d:/www/xxx/</virtualhost>  <virtualhost www.yyy.com:80="">  DocumentRoot d:/www/yyy/  </virtualhost> 接着配置目录的访问权限  <directory x

apache本地域名ip重定向vhosts

apache本地域名ip重定向,使本机通过指定域名访问到指定ip路径. 1.apache配置apache/conf/httpd.conf  : 开启配置 Include conf/extra/httpd-vhosts.conf 2.apache配置apache/conf/extra/httpd-vhosts.conf : <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "D:/WWW/App_wxjh&quo

apache限制同一ip大并发访问及下载

内容来自以下网站整理 http://www.jb51.net/article/58060.htmhttp://www.2cto.com/os/201202/120665.htmlhttp://blog.csdn.net/lipei1220/article/details/39293851 测试时请用浏览器下载来测试,不用bt之类下载 如果只限制连接数据我们可以直接使用limit,如果要限制其它的需要使用其它第三方模块了 使用mod_limitipconn模块限制IP并发连接数 安装: wget

nginx反向代理 后端apache获取真实ip方式

系统环境为 centos6.4 64位 nginx配置: 在server中加入 - proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; - 后端apache为yum安装方式: 在网上查找apache需要第三方的模块来获取真实的客户ip 网上给的下载地址用不了只能另辟新径 安装rpaf 我是rpm安装的 别的方式可以自行查找 方法如下 rpm -ivh ftp://ftp.pbone.net/mir