查看原文
其他

LAMP架构应用实战—Apache服务基于IP的虚拟主机配置

2016-10-18

作者 民工哥

LAMP架构应用实战—Apache服务

基于IP的虚拟主机配置


基于IP的虚拟主机————就是说每个虚拟主机使用一个独立的IP地址

服务器配置多块网卡实现,或者同一块网卡配置多个IP实现都可以

今天模拟的环境是同一网卡配置多个IP地址实现基于IP的虚拟主机配置


一:新增IP地址

[root@Centos ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:AA:CA:2B  

          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:feaa:ca2b/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:2345 errors:0 dropped:0 overruns:0 frame:0

          TX packets:694 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:224750 (219.4 KiB)  TX bytes:92020 (89.8 KiB)


在eth0上新增一个IP地址用于测试使用

[root@Centos ~]# ifconfig eth0:20 192.168.1.20 netmask 255.255.255.0 up

[root@Centos ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:AA:CA:2B  

          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:feaa:ca2b/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:2468 errors:0 dropped:0 overruns:0 frame:0

          TX packets:767 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:235882 (230.3 KiB)  TX bytes:100158 (97.8 KiB)


eth0:20   Link encap:Ethernet  HWaddr 00:0C:29:AA:CA:2B  

          inet addr:192.168.1.20  Bcast:192.168.1.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


二:联通性测试

在本地客户端主机上测试联通性

C:\Users\Administrator>ping 192.168.1.20

正在 Ping 192.168.1.20 具有 32 字节的数据:

来自 192.168.1.20 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.1.20 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.1.20 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.1.20 的回复: 字节=32 时间<1ms TTL=64

192.168.1.20 的 Ping 统计信息:

    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

    最短 = 0ms,最长 = 0ms,平均 = 0ms


三:备份虚拟主机配置文件

提示:生产环境一定要养成良好的习惯

[root@Centos ~]# cd /application/apache/conf/extra/

[root@Centos extra]# cp httpd-vhosts.conf httpd-vhosts.conf.$(date +%F)

[root@Centos extra]# ls -ll

total 76

-rw-r--r--. 1 root root  2895 Sep 12 03:30 httpd-autoindex.conf

-rw-r--r--. 1 root root  1852 Sep 12 03:30 httpd-dav.conf

-rw-r--r--. 1 root root  2942 Sep 12 03:30 httpd-default.conf

-rw-r--r--. 1 root root  1119 Sep 12 03:30 httpd-info.conf

-rw-r--r--. 1 root root  5078 Sep 12 03:30 httpd-languages.conf

-rw-r--r--. 1 root root  1039 Sep 12 03:30 httpd-manual.conf

-rw-r--r--. 1 root root  4444 Sep 12 03:30 httpd-mpm.conf

-rw-r--r--. 1 root root  2243 Sep 12 03:30 httpd-multilang-errordoc.conf

-rw-r--r--. 1 root root 13506 Sep 12 03:30 httpd-ssl.conf

-rw-r--r--. 1 root root   694 Sep 12 03:30 httpd-userdir.conf

-rw-r--r--. 1 root root  1037 Sep 13 06:35 httpd-vhosts.conf

-rw-r--r--. 1 root root  1481 Sep 13 06:28 httpd-vhosts.conf.2016-09-13

-rw-r--r--. 1 root root  1037 Sep 15 02:04 httpd-vhosts.conf.2016-09-15

-rw-r--r--. 1 root root  3161 Sep 12 03:30 proxy-html.conf


四:配置虚拟主机配置文件

[root@Centos extra]# vi httpd-vhosts.conf

# Virtual Hosts

#

# Required modules: mod_log_config


# If you want to maintain multiple domains/hostnames on your

# machine you can setup VirtualHost containers for them. Most configurations

# use only name-based virtual hosts so the server doesn't need to worry about

# IP addresses. This is indicated by the asterisks in the directives below.

#

# Please see the documentation at

# <URL:http://httpd.apache.org/docs/2.4/vhosts/>

# for further details before you try to setup virtual hosts.

#

# You may use the command line option '-S' to verify your virtual host

# configuration.

#

# VirtualHost example:

# Almost any Apache directive may go into a VirtualHost container.

# The first VirtualHost section is used for all requests that do not

# match a ServerName or ServerAlias in any <VirtualHost> block.

#

<VirtualHost 192.168.1.20:80>

    ServerAdmin admini@abc.com

    DocumentRoot "/data/www/bbs"

    ServerName 192.168.1.20

    ServerAlias abc.com

    ErrorLog "logs/bbs-error_log"

    CustomLog "logs/bbs-access_log" common

</VirtualHost>

上面的标记部分为修改的配置


[root@Centos extra]# ../../bin/apachectl graceful

httpd not running, trying to start

发现httpd服务并没有开机自启动


检查发现/etc/init.d/下没有httpd服务

[root@Centos extra]# cd /etc/init.d/

[root@Centos init.d]# ls

abrt-ccpp         crond       killall         ntpd         rpcgssd         sshd

abrtd             cups        lvm2-lvmetad    ntpdate      rpcidmapd       sssd

abrt-oops         dnsmasq     lvm2-monitor    oddjobd      rpcsvcgssd      sysstat

acpid             firstboot   mdmonitor       portreserve  rsyslog         udev-post

atd               functions   messagebus      postfix      sandbox         wdaemon

auditd            haldaemon   netconsole      psacct       saslauthd       winbind

autofs            halt        netfs           quota_nld    single          wpa_supplicant

blk-availability  ip6tables   network         rdisc        smartd          ypbind

bluetooth         iptables    NetworkManager  restorecond  snmpd

certmonger        irqbalance  nfs             rngd         snmptrapd

cpuspeed          kdump       nfslock         rpcbind      spice-vdagentd

[root@Centos init.d]# cp /application/apache/bin/apachectl ./httpd


不支持chkconfig

[root@Centos init.d]# chkconfig --add httpd

service httpd does not support chkconfig


因此编辑下面的文件,增加如下配置

[root@Centos init.d]# vi /etc/init.d/httpd

#!/bin/sh

#

#chkconfig:345 85 15

#description:Start and stop the Apache HTTP Server

[root@Centos init.d]# chkconfig --add httpd               加入启动

[root@Centos init.d]# chkconfig --list httpd                 查看启动情况

httpd           0:off   1:off   2:off   3:on    4:on    5:on    6:off

[root@Centos init.d]# cd -

/application/apache/conf/extra

[root@Centos extra]# ../../bin/apachectl -t

Syntax OK

[root@Centos extra]# ../../bin/apachectl graceful

[root@Centos extra]# lsof -i tcp:80

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

httpd   2390   root    4u  IPv6  16911      0t0  TCP *:http (LISTEN)

httpd   2531 daemon    4u  IPv6  16911      0t0  TCP *:http (LISTEN)

httpd   2532 daemon    4u  IPv6  16911      0t0  TCP *:http (LISTEN)

httpd   2533 daemon    4u  IPv6  16911      0t0  TCP *:http (LISTEN)

[root@Centos extra]# ps -ef |grep httpd

root      2390      1  0 02:08 ?        00:00:00 /application/apache2.4.23/bin/httpd -k graceful

daemon     2531   2390  0 02:22 ?        00:00:00 /application/apache2.4.23/bin/httpd -k graceful

daemon     2532   2390  0 02:22 ?        00:00:00 /application/apache2.4.23/bin/httpd -k graceful

daemon     2533   2390  0 02:22 ?        00:00:00 /application/apache2.4.23/bin/httpd -k graceful

root       2623   1934  0 02:24 pts/1    00:00:00 grep httpd


五:测试访问

[root@Centos extra]# echo '192.168.1.20:80 this server is the server of bbs stie'>>/data/www/bbs/index.html 

[root@Centos extra]# cat /data/www/bbs/index.html

welcome to bbs.abc.com

192.168.1.20:80 this server is the server of bbs stie

打开本地客户端浏览器


经测试表示之前的配置是正确的,同样的方法可以配置多个基于IP的虚拟主机

长按二维码图片关注微信公众号

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存