一、基于ip地址对web服务器的访问
一个ip对应一个网,例如实现操作:
建立一个192.168.242.111的网站,文件放在/www/111,内容显示this is 111
建立一个192.168.242.222的网站,文件放在/www/222,内容显示this is 222
具体操作步骤如下:
-
配置网络
192.168.242.222//255.255.255.0//192.168.242.2
192.168.242.111//255.255.255.0 -
编辑配置文件
mkdir /www/{111,222} -p
echo this is 111 > /www/111/index.html
echo this is 222 > /www/222/index.html
vim /etc/httpd/conf.d/vhosts.conf //编辑主配置文件参数
<Directory /www>
AllowOverride None
Require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot /www
ServerName 192.168.242.128
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/111
SercerName 192.168.242.111
</VirtualHost>
<VirtualHost *:80>
DocumengRoot /www/222
ServerName 192.168.242.222
</VirtualHost>
- 关闭防火墙,重开
systemctl stop firewalld
setenforce 0
systemctl restart httpd
- 测试
添加子界面访问网站操作:
此操作创建并写入即可,无需更改配置信息。
mkdir 1
mkdir 2
mkdir /www/111/1
mkdir /www/222/2
echo this is 1 > /www/111/1/index.html
echo this is 2 > /www/222/2/index.html
systemctl restart httpd
搜索路径:
http://192.168.242.111/1/index.html
http://192.168.242.111/1/index.html
二、基于域名实现访问不同网站信息
题1:要求搭建web服务器,创建基于域名的虚拟主机,能够使用www.xiaopang.com和www.dapang.com访问各自的网站存放路径分别为/xiaopang和/dapang,内容自定。
- 创建目录
mkdir /www/{xiaopang,dapang} -p
- 修改配置文件
vim /etc/httpd/conf.d/vhosts.conf
<Directory /www>
AllowOverride None
Require all granted
</Directory>
<VirtualHost 192.168.242.222:80>
DocumentRoot /www/xiaopang
ServerName www.xiaopang.com
</VirtualHost>
<VirtualHost 192.168.242.111:80>
DocumentRoot /www/dapang
ServerName www.dapang.com
</VirtualHost>
- 域名解析
Linux下:vim /etc/hosts
windows下:c:\windows\system32\drivers\etc\hosts
在最后行加上两行解析:
192.168.242.111 www.dapang.com
192.168.242.222 www.xiaopang.com
- 写入内容
echo d a p a n g > /www/dapang/index.html
echo x i a o p a n g > /www/xiaopang/index.html
- 测试
systemctl restart httpd