1 安装Apache的软件包httpd yum list|grep httpd 2 配置httpd 的配置文件 vim /etc/httpd/conf/httpd.conf 里面的相关设置 S
1 安装Apache的软件包httpd
yum list|grep httpd
2 配置httpd 的配置文件
vim /etc/httpd/conf/httpd.conf 里面的相关设置
ServerName localhost
监听设置我80端口
Listen 80
httpd启动根目录的设置,例如:DocumentRoot "/var/www/html"
这个根目录可以自己定义
启用访问控制
<Directory "/myweb">
Options FollowSymLinks
AllowOverride None
Order allow,deny <-----定义acl控制的顺序,这里代表先允许,不再允许范围内的都拒绝
Allow from all <----定义允许所有
IndexOptions NameWidth=* #根据文件名自动调整列宽
IndexOptions Charset=UTF-8 #编码格式,防止中文乱码
</Directory>
配置完成后重启httpd
service httpd restart
在浏览器中访问主机
httpd启动默认访问的是设置的根/myweb下的index.html,如果没有index.html 则主页就会显示上面的阿帕奇的主页。
自己定义个index.html
[root@wzlvm myweb]# cat index.html
hello word!
295 <Directory "/myweb">
297 Options Indexes FollowSymLinks
298 AllowOverride None
299 Order allow,deny
300 Allow from all
302 </Directory>
上面的Indexes 索引能提供文件链接,如下:
3 口令验证
修改参数 ,把AllowOverride none 改成 AllowOverride ALL
在httpd的启动根目录下建.htaccess文件,文件内容如下:
Authname "Just for test"
authtype basic
authuserfile /etc/httpd/userpw
require valid-user
用一下命令设置用户和密码
htpasswd -c /etc/httpd/userpw test
设置完用户名和密码就可以重启httpd 然后登陆
4 设置虚拟主机
如果只有一个主机,需要部署多个服务,就需要配置虚拟主机
有两种设置方法,基于域名的虚拟主机 和基于端口的虚拟主机
4.1 基于域名的虚拟主机:
打开/etc/httpd/conf/httpd.conf 文件里面的 NameVirtualHost *:80 并添加虚拟主机的配置文件
<VirtualHost *:80>
DocumentRoot /www/wzltest1
ServerName www.wzltest1.com
ErrorLog logs/www.wzltest1.com-error_log
CustomLog logs/www.wzltest1.com-access_log common
</VirtualHost>
该虚拟主机指定了主机名称为www.wzltest1.com 启动根目录为www.wzltest1和相关的日志文件
建好根目录/www/wzltest1并且在里面创建也index.html文件
[root@wzlvm wzltest1]# cat index.html
hello my name is wzltest1
[root@wzlvm wzltest1]#
4.2 给域名虚拟主机配置DNS
根据域名虚拟主机的主机名ServerName www.wzltest1.com 设置DNS的主区域配置文件和区域配置文件。
zone "wzltest1.com"{
type master;
file "wzltest1.com";
};
记得修改区域配置文件的权限为root.named
区域配置文件的内容为:
[root@wzlvm named]# cat wzltest1.com
$TTL 1D
@ IN SOA ns.wzltest1.com. admin.wzltest1.com. (
2016100903 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ); minimum
wzltest1.com. IN NS ns.wzltest1.com.
ns.wzltest1.com. IN A 192.168.125.132
www IN A 192.168.125.132
根据上面的区域配置文件,可以解析到192.168.125.132
注意:本机的/etc/reslove.conf 也要指定 本DNS服务器
4.2.1 重启DNS 和httpd 并验证
[root@wzlvm named]# service named restart
Stopping named: [ OK ]
Starting named: [ OK ]
[root@wzlvm named]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
校验一下DNS解析是否成功:
[root@wzlvm named]# dig -t A www.wzltest1.com
在浏览器中验证。注意:本测试时在字符界面虚拟机中测试的,字符界面没有浏览器,需要安装elinks 字符浏览器。
elinks www.wzltest1.com
、
由于本机是Windows,无法DNS解析,可以直接访问192.168.125.132进行访问
重新再配一台虚拟主机:
/etc/httpd/conf/httpd.conf中添加
<VirtualHost *:80>
DocumentRoot /www/wzltest2
ServerName www.wzltest2.com
ErrorLog logs/www.wzltest2.com-error_log
CustomLog logs/www.wzltest2.com-access_log common
</VirtualHost>
/etc/named.rfc1912.zones 中添加第二部域名虚拟主机的配置
zone "wzltest2.com"{
type master;
file "wzltest2.com";
};
在/var/named下面添加第二部虚拟主机区域配置文件
内容如下:
[root@wzlvm named]# cat wzltest2.com
$TTL 1D
@ IN SOA ns.wzltest2.com. admin.wzltest2.com. (
2016100903 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ); minimum
wzltest2.com. IN NS ns.wzltest2.com.
ns.wzltest2.com. IN A 192.168.125.132
www IN A 192.168.125.132
注意:上面的DNS服务器地址和需要DNS解析的目的IP地址是两个不同的概念,只是当前做的实验DNS服务器好客户机是同一台,所以IP地址是一样的。
添加第二台虚拟主机的启动根目录:
/www/wzltest2
重启service named restart 和service httpd restart
验证 dig -t A www.wzltest2.com
根据上面两台虚拟主机www.wzltest1.com 和www.wzltest2.com的部署失效了同有个IP 192.168.125.132部署两个应用。
4.3 基于不同端口的虚拟主机
和4.2 基于域名的虚拟主机相似,httpd配置文件中要添加
<VirtualHost 172.16.10.1:8081> DocumentRoot /www/uplooking ServerName www.uplooking.com ErrorLog logs/www.uplooking.com-error_log CustomLog logs/www.uplooking.com-access_log common</VirtualHost><VirtualHost 172.16.10.1:8082> DocumentRoot /www/up17 ServerName www.up17.com ErrorLog logs/www.up17.com-error_log CustomLog logs/www.up17.com-access_log common</VirtualHost>
4.2 的配置段是放在/etc/httpd/conf/httpd.conf中,同样的也可以放在/etc/httpd/conf.d/ 路径下面以.conf结尾
如下:
<VirtualHost 192.168.125.132:8081> DocumentRoot /www/wzltest3 ServerName www.wzltest3.com ErrorLog logs/www.wzltest3.com-error_log CustomLog logs/www.wzltest3.com-access_log common</VirtualHost>
<VirtualHost 192.168.125.132:8082> DocumentRoot /www/wzltest4 ServerName www.wzltest3.com ErrorLog logs/www.wzltest4.com-error_log CustomLog logs/www.wzltest4.com-access_log common</VirtualHost>
注意,在/etc/httpd/conf/httpd.conf里面要把监听8081 和8082添加上
同样根据上面两个配置文件里面设置的8081端口启动根目录/www/wzltest3 和8082端口的启动根目录/www/wzltest4 添加后相应的目录
主区域配置文件/etc/named.rfc1912.conf 里面也是需要添加之前指定的主机名ServerName www.test3.com
/etc/named.rfc1912.zones
zone "wzltest3.com"{ type master; file "wzltest3.com"; };
最后添加好wzltest3.com对应的区域配置文件
[root@wzlvm www]# cat /var/named/wzltest3.com $TTL 1D@ IN SOA ns.wzltest3.com. admin.wzltest3.com. ( 2016100903 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ); minimum wzltest3.com. IN NS ns.wzltest3.com. ns.wzltest3.com. IN A 192.168.125.132 www IN A 192.168.125.132
注意:需要说明的是两个不同的端口配置的DocumentRoot 指定启动根目录不同,根据不同的端口对应不同的应用;ServerName 指定主机名可以相同也可以不同,如果指定相同的主机名则DNS只需要配置一个解析,如果指定不同的主机名则需要配两个DNS 解析,否则会启动httpd会报错找不到主机;但是配置两个不同的主机名到时候解析到的IP应该是一样的,否则失去虚拟主机的意义;所以一般方便期间读只用配相同的主机名就可以了。
重启service named restart 和service httpd restart
验证同一个域名不同的端口对应不同的应用:
elinks www.wzltest3.com:8081
说明:由于域名都是一样的,不受DNS解析的影响,所以我们可以在本机windows 相同的浏览器中直接输入IP加上不同的端口进行验证:
上面两张图足以验证。