您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天小编就为大家带来一篇构建虚拟主机使用教程的文章。小编觉得挺不错的,为此分享给大家做个参考。一起跟随小编过来看看吧。
在同一台物理服务器中运行多个web站点,其中每一个站点并不独立占用一台真正的计算机
* 基于域名的虚拟主机
* 基于IP地址的虚拟主机
* 基于端口的虚拟主机
www.accp.com ip地址为本机地址
www.kgc.com ip地址为本机地址
一台Linux主机做服务器
一台win7主机做测试
[root@localhost ~]# yum install httpd bind -y //安装http,dns服务
[root@localhost ~]# vim /etc/named.conf ##主配置文件
options {
listen-on port 53 { any; }; ##将本机监听为所有
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; ##允许所有
[root@localhost ~]# vim /etc/named.rfc1912.zones ##配置区域配置文件
zone "localhost" IN { ##复制两个模板到下面
type master;
file "named.localhost";
allow-update { none; };
};
zone "accp.com" IN { ##修改localhost为accp.com
type master;
file "accp.com.zone"; ##创建区域数据配置文件
allow-update { none; };
};
zone "kgc.com" IN { ##修改localhost为kgc.com
type master;
file "kgc.com.zone"; ##创建区域数据配置文件
allow-update { none; };
};
[root@localhost ~]# cd /var/named/ ##切换到/var/named目录下
[root@localhost named]# cp -p named.localhost accp.com.zone
##复制模板为accp.com.zone
[root@localhost named]# vim accp.com.zone ##编辑区域数据配置文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.13.133 ##删除ipv6 添加域名解析地址为本机
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
##编辑kgc的区域数据配置文件,内容不需要更改
[root@localhost named]# systemctl start named ##开启dns服务
[root@localhost named]# systemctl stop firewalld.service ##关闭防火墙
[root@localhost named]# setenforce 0 ##关闭增强功能
[root@localhost named]# cd /etc/httpd/conf ##切换到http配置目录中
[root@localhost named]# mkdir extra ##创建扩展配置文件目录
[root@localhost named]# cd extra
[root@localhost named]# vim vhost.conf ##编辑扩展配置文件
<VirtualHost *:80> ##标签 *所有ip 80端口
Documentroot "/var/www/html/accp/" ##站点目录
ServerName www.accp.com ##域名
Errorlog "logs/www.accp.com.error_log" ##错误日志文件
Customlog "logs/www.accp.com.access_log" common ##访问日志
<Directory "/var/www/html/"> ##控制目录权限
Require all granted ##允许所有人可以访问
</Directory>
</VirtualHost>
##上面的logs是软连接/var/log/httpd ##下面的只要修改accp为kgc即可
<VirtualHost *:80>
Documentroot "/var/www/html/kgc/"
ServerName www.kgc.com
Errorlog "logs/www.kgc.com.error_log"
Customlog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost extra]# cd /var/www/html/ ##切换到网页站点中
[root@localhost html]# mkdir accp kgc ##创建两个网站的站点目录
[root@localhost html]# cd accp/
[root@localhost accp]# vim index.html ##编辑accp网页内容
<h2>this is a accp web</h2>
[root@localhost html]# cd kgc/
[root@localhost accp]# vim index.html ##编辑kgc网页内容
<h2>this is a kgc web</h2>
[root@localhost www]# cd /etc/httpd/conf ##切换到http的配置文件目录
[root@localhost conf]# vim httpd.conf ##编辑主配置文件
Include conf/extra/vhost.conf ##在末行加入扩展配置文件目录
[root@localhost conf]# systemctl start httpd ##启动http服务
[root@lolocalhost extra]# vim vhost.conf
<VirtualHost *:80>
Documentroot "/var/www/html/kgc/"
ServerName www.kgc.com
Errorlog "logs/www.kgc.com.error_log"
Customlog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080> ##端口号改成8080
Documentroot "/var/www/html/kgc02/" ##目录更改为02
ServerName www.kgc.com
Errorlog "logs/www.kgc02.com.error_log" ##日志文件更改为02
Customlog "logs/www.kgc02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir kgc02
[root@localhost html]# vim index.html ##编辑kgc02网页的内容
<h2>this is a kgc02 web</h2>
[root@localhost html]# vim /etc/httpd/conf/httpd.conf ##修改配置文件
Listen 192.168.13.133:80 ##修改ipv4监听端口
Listen 192.168.13.133:8080
#Listen 80 ##将ipv6端口注释
[root@localhost html]# systemctl restart httpd.service ##重启网络服务
[root@localhost html]# netstat -ntap ##查看监听端口开启状态
tcp 0 0 192.168.13.133:8080 0.0.0.0:* LISTEN 4093/httpd
tcp 0 0 192.168.13.133:80 0.0.0.0:* LISTEN 4093/httpd
在Linux上添加一块网卡做不同IP
[root@lolocalhost extra]# vim vhost.conf
<VirtualHost 192.168.13.133:80> ##修改ip地址
Documentroot "/var/www/html/accp/" ##域名此处没有用处删除
Errorlog "logs/www.accp.com.error_log"
Customlog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.13.132:80> ##修改ip地址
Documentroot "/var/www/html/accp02/" ##修改目录accp02和日志
Errorlog "logs/www.accp02.com.error_log"
Customlog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02 ##创建132ip地址的目录站点内容
[root@localhost html]# cd accp02/
[root@localhost accp02]# echo "<h2>this is 132 web</h2>" > index.html
[root@localhost accp02]# cd ../
[root@localhost html]# cd accp ##修改133ip地址网页内容
[root@localhost accp]# vim index.html
<h2>this is 133 web</h2>
[root@localhost html]# vim /etc/httpd/conf/httpd.conf ##修改配置文件
Listen 192.168.13.133:80 ##添加两个ip的端口监听
Listen 192.168.13.132:80
#Listen 192.168.13.133:8080 ##注释8080端口的
#Listen 80 ##将ipv6端口注释
[root@localhost conf]# systemctl restart httpd.service ##重启网络服务
[root@lolocalhost extra]# vim vhost.conf
<VirtualHost 192.168.13.133:80>
Documentroot "/var/www/html/accp/"
ServerName www.accp.com ##添加域名
Errorlog "logs/www.accp.com.error_log"
Customlog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.13.132:80>
Documentroot "/var/www/html/accp02/"
ServerName www.naccp.com ##添加另一个域名
Errorlog "logs/www.accp02.com.error_log"
Customlog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
[root@localhost /]# vim /etc/named.rfc1912.zones
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
zone "naccp.com" IN {
type master;
file "naccp.com.zone";
allow-update { none; };
};
[root@localhost named]# cd /var/named/
[root@localhost named]# cp -p accp.com.zone naccp.com.zone ##复制
[root@localhost named]# vim naccp.com.zone ##配置区域数据配置文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.13.132 ##此处的naccp要修改132ip地址
[root@localhost named]# systemctl restart named ##重启DNS
[root@localhost named]# systemctl restart httpd.service ##重启网络
以上就是构建虚拟主机的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注亿速云行业资讯!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。