1.安装

yum install httpd -y

2.多域名配置虚拟主机

# 创建虚拟主机配置文件:

vim /etc/httpd/conf.d/vhost.conf

<VirtualHost *:80>

DocumentRoot "/var/www/html/jf6"

ServerName www.jf6.com

ErrorLog "logs/www.jf6.com_error_log"

CustomLog "logs/www.jf6.com_access_log" common

</VirtualHost>

<VirtualHost *:80>

DocumentRoot "/var/www/html/jfedu"

ServerName www.jfedu.com

ErrorLog "logs/www.jfedu.com_error_log"

CustomLog "logs/www.jfedu.com_access_log" common

</VirtualHost>

# 创建各自的发布目录:

mkdir -p /var/www/html/{jf6,jfedu}

# 创建各自的测试页面:

echo "this is jf6 page" > /var/www/html/jf6/index.html

echo "this is jfedu page" > /var/www/html/jfedu/index.html

# 启动apache服务:

apachectl start或者systemctl start httpd

# 编辑本地hosts文件:

192.168.75.125 www.jfedu.com www.jf6.com www.lutixia.com

# 访问测试:

[root@node5 ~]# curl www.jfedu.com

this is jfedu page

[root@node5 ~]# curl www.jf6.com

this is jf6 page

[root@node5 ~]#

3.多端口配置虚拟主机

# 修改主配置文件,添加监听端口:

vim /etc/httpd/conf/httpd.conf

Listen 80

Listen 8080

# 创建虚拟主机配置文件:

<VirtualHost *:80>

DocumentRoot "/var/www/html/jf6"

ServerName www.jf6.com

ErrorLog "logs/www.jf6.com_error_log"

CustomLog "logs/www.jf6.com_access_log" common

</VirtualHost>

<VirtualHost *:8080>

DocumentRoot "/var/www/html/jfedu"

ServerName www.jfedu.com

ErrorLog "logs/www.jfedu.com_error_log"

CustomLog "logs/www.jfedu.com_access_log" common

</VirtualHost>

# 创建各自的发布目录:

mkdir -p /var/www/html/{jf6,jfedu}

# 创建各自的测试页面:

echo "this is 80 page" > /var/www/html/jf6/index.html

echo "this is 8080 page" > /var/www/html/jfedu/index.html

# 启动apache服务:

apachectl start或者systemctl start httpd

# 访问测试:

[root@node5 ~]# curl 192.168.75.125:80

this is 80 page

[root@node5 ~]# curl 192.168.75.125:8080

this is 8080 page

4.基于shell脚本实现虚拟主机配置

4.1 v1版本(原始脚本)

[root@node5 bin]# pwd

/root/bin

[root@node5 bin]# vim vhost_v1.sh

#!/bin/bash

# 第一步:创建虚拟主机配置文件

echo "

<VirtualHost *:80>

DocumentRoot "/var/www/html/jf6"

ServerName www.jf6.com

ErrorLog "logs/www.jf6.com_error_log"

CustomLog "logs/www.jf6.com_access_log" common

</VirtualHost>

<VirtualHost *:80>

DocumentRoot "/var/www/html/jfedu"

ServerName www.jfedu.com

ErrorLog "logs/www.jfedu.com_error_log"

CustomLog "logs/www.jfedu.com_access_log" common

</VirtualHost> " > /etc/httpd/conf.d/vhots.conf

# 第二步:创建各自的发布目录

mkdir -p /var/www/html/{jf6,jfedu}

# 第三步:创建测试页面

echo "this is jf6 page" > /var/www/html/jf6/index.html

echo "this is jfedu page" > /var/www/html/jfedu/index.html

# 第四步:重启服务

apachectl restart && "虚拟主机创建成功"

chmod +X vhost_v1.sh

4.2 v2版本(引入变量)

#!/bin/bash

# 第一步:创建虚拟主机配置文件

read -p "请输入你要创建的虚拟主机名:" name

echo "

<VirtualHost *:80>

DocumentRoot "/var/www/html/$name"

ServerName www.$name.com

ErrorLog "logs/www.$name.com_error_log"

CustomLog "logs/www.$name.com_access_log" common

</VirtualHost>" >> /etc/httpd/conf.d/vhost.conf

# 第二步:创建各自的发布目录

mkdir -p /var/www/html/$name

# 第三步:创建测试页面

echo "this is $name page" > /var/www/html/$name/index.html

# 第四步:重启服务

apachectl restart && echo "$name 虚拟主机创建成功"

4.3 v3版本(引入判断)

#!/bin/bash

# 第一步:创建虚拟主机配置文件

read -p "请输入你要创建的虚拟主机名:" name

awk -F. '/ServerName/{print $2}' /etc/httpd/conf.d/vhost.conf | grep -w $name

if [ $? -eq 0 ];then

echo "$name 虚拟主机已经存在,请核实"

else

echo -e "$name 虚拟主机不存在,\n是否需要创建?\n创建请输入1,否则请输入任意键退

出!\n"

read -p "<<< " num

if [ $num = 1 ];then

echo "

<VirtualHost *:80>

DocumentRoot "/var/www/html/$name"

ServerName www.$name.com

ErrorLog "logs/www.$name.com_error_log"

CustomLog "logs/www.$name.com_access_log" common

</VirtualHost>" >> /etc/httpd/conf.d/vhost.conf

# 第二步:创建各自的发布目录

mkdir -p /var/www/html/$name

# 第三步:创建测试页面

echo "this is $name page" > /var/www/html/$name/index.html

# 第四步:重启服务

apachectl restart && echo "$name 虚拟主机创建成功"

fi

fi