简介
网页缓存是将一部分经常不会改变或变动很少的页面缓存,下次浏览器再次访问这些页面时,不需要再次去下载这些页面,从而提高了用户的访问速度
实验环境
- 系统环境:centos6.5
- 服务器IP地址:192.168.100.103
- yum挂载目录:/mnt/sr0
- 相关源码包下载地址:百度云下载 密码:pko3
搭建步骤
一、准备工作
1、关闭防火墙及selinux
[root@Init5 ~]# chkconfig iptables off #随开机关闭iptables
[root@Init5 ~]# vim /etc/sysconfig/selinux
[root@Init5 ~]# reboot #重启生效
2、卸载以RPM方式安装httpd相关包
[root@Init5 ~]# rpm -qa | grep "httpd"
3、搭建DNS服务器
如果没有搭建以上两个服务的朋友,请查看我的其他帖子有详细介绍
http://blog.51cto.com/11905606/2156944
[root@Init5 ~]# yum remove httpd #卸载httpd相关包
二、搭建Apache服务
1、安装gcc、gcc-c++、make、arp、arp-util、pcre等工具包
[root@localhost ~]# yum -y install gcc gcc-c++ make zlib-devel #安装C语言编译器以及make
[root@localhost ~]# tar -zxvf apr-1.4.6.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/apr-1.4.6/
[root@localhost apr-1.4.6]# ./configure prefix=/usr/local/apr && make && make install
[root@localhost ~]# tar -zxvf apr-util-1.4.1.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/apr-util-1.4.1/
[root@localhost apr-util-1.4.1]# ./configure prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
[root@localhost ~]# tar -zxvf pcre-8.10.tar.gz -C /usr/src #支持正则
[root@localhost ~]# cd /usr/src/pcre-8.10/
[root@localhost pcre-8.10]# ./configure prefix=/usr/local/pcre && make && make install
2、配置编译安装Apache
[root@Init5 ~]# tar zxvf httpd-2.4.2.tar.gz -C /usr/src/
[root@Init5 ~]# cd /usr/src/httpd-2.4.2/
[root@Init5 httpd-2.4.2]# ./configure \
--prefix=/usr/local/httpd \
--with-apr=/usr/local/apr \
--with-pcre=/usr/local/pcre \
--enable-expires \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
参数解析:
prefix:指定安装目录
enable-expires:启用mod_expires模块
enable-so:启用动态加载模块支持,需要什么功能可以动态加载
enable-rewrite:启用网页地址重写功能,实现伪静态
enable-charset-lite:默认字符集
enable-cgid:启用CGID
[root@Init5 httpd-2.4.2]# make && make install #编译及编译安装
3、添加系统服务
[root@Init5 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@Init5 ~]# vim /etc/init.d/httpd
第2、3两行添加以下参数:
# chkconfig:2345 85 15
# description:Apache is a World Wide Web server.
[root@Init5 init.d]# chmod +x /etc/init.d/httpd
[root@Init5 init.d]# chkconfig --add httpd
4、建立软链接,方便管理
[root@Init5 ~]# mkdir -p /etc/httpd
[root@Init5 ~]# ln -s /usr/local/httpd/conf/ /etc/httpd/ #优化配置文件路径
[root@Init5 ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ #优化命令路径
5、修改配置文件
[root@Init5 ~]# vim /etc/httpd/conf/httpd.conf
修改以下参数:
ServerName www.bt.com:80 #填写完全主机名
Listen 192.168.100.103:80 #监听本地IP
6、启动httpd服务
[root@Init5 ~]# service httpd start
[root@Init5 ~]# netstat -anpt | grep ':80'
三、配置Apache实现网页缓存
1、修改主配置文件
[root@Init5 ~]# vim /etc/httpd/conf/httpd.conf
约108行开启expires模块功能:
LoadModule expires_module modules/mod_expires.so
最后一行添加以下内容:
<IfModule mod_expires.c>
ExpiresActive On #开启expires模块
ExpiresDefault "access plus 60 seconds" #对于http协议下任意格式的文档(html,css,js图片等都是60秒之后过期)
</IfModule>
2、检查是否安装了mod_deflate模块
[root@Init5 ~]# apachectl -t -D DUMP_MODULES | grep 'expires'
[root@Init5 ~]# /etc/init.d/httpd restart #重启服务