要求:
1.要求搭建web服务器,能够访问到网页内容为“小胖,你咋这么胖呢!”
2.要求搭建web服务器,创建基于域名的虚拟主机,能够使用www.xiaopang.com和www.dapang.com访问各自的网站网站存放路径分别为/xiaopang和/dapang,内容自定。
配置文件
[root@wxf ~]# cd /etc/httpd/
[root@wxf httpd]# ll
total 0
drwxr-xr-x. 2 root root 37 Aug 16 16:17 conf //主配置文件
drwxr-xr-x. 2 root root 100 Aug 16 16:24 conf.d //子配置文件
drwxr-xr-x. 2 root root 226 Aug 15 13:39 conf.modules.d
lrwxrwxrwx. 1 root root 19 May 20 12:33 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root 29 May 20 12:33 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root 10 May 20 12:33 run -> /run/httpd
lrwxrwxrwx. 1 root root 19 May 20 12:33 state -> ../../var/lib/httpd
在子配置文件夹目录下创建一个vhost.conf(文件名自取),编辑文件
<VirtualHost 192.168.190.130:80> //主机名(ip地址)
DocumentRoot /www/130 //文件位置
ServerName 192.168.190.130
</VirtualHost>
<Directory /www> //能访问的目录权限
AllowOverride none //不覆盖
Require all granted //表示同意访问该目录下的文件
</Directory>
然后在文件位置/www/130 下创建html文件
[root@wxf conf]# pwd
/etc/httpd/conf
[root@wxf conf]# ll
total 28
-rw-r--r--. 1 root root 11926 Aug 16 16:16 httpd.conf //主配置文件
-rw-r--r--. 1 root root 13064 May 20 12:33 magic
先访问一下主配置文件
<IfModule dir_module>
DirectoryIndex index.html //这一项表示能够访问的网页文件
</IfModule>
这里只允许一个index.html 文件,所以在/www/130下创建文件是名字一定要取index.html,否则将无法访问网页(403)如果要改名字或者添加文件类型,要在该主配置文件里,index.html 后面把要加的补上去,比如index.css/130.html 等等
来到网页文件目录下创建index.htm
[root@wxf 130]# echo '小胖,你咋这么胖呢!' > index.html
[root@wxf 130]# cat index.html
小胖,你咋这么胖呢!
重启httpd服务 (首先得关闭防火墙和selinux)
[root@wxf 130]# systemctl stop firewalld
[root@wxf 130]# systemctl stop selinux
[root@wxf 130]# systemctl restart httpd
//disable 关闭开机启动
进入浏览器输入IP即可
一对多(一个IP多个域名)
进入刚刚创建的子配置文件vhost.conf 重新编辑
<VirtualHost 192.168.190.130:80>
DocumentRoot /xiaopang
ServerName www.xiaopang.com //域名
</VirtualHost>
<VirtualHost 192.168.190.130:80>
DocumentRoot /dapang
ServerName www.dapang.com
</VirtualHost>
<Directory /xiaopang>
AllowOverride none
Require all granted
</Directory>
<Directory /dapang>
AllowOverride none
Require all granted
</Directory>
再在各个目录下创建每个域名所对应的index.html,重启httpd
修改host文件
在windows下
hosts文件 添加 ip对应的域名信息
(最后两行是新加的) 保存退出