记录在CentOS 7 Apache环境中配置Varnish Cache反向网站缓存服务器

Varnish Cache,一个免费开源的反向网站缓存服务器,可以帮助我们提高WEB服务器的性能。Varnish Cache ,一般用于Web服务器后面的,可将网站速度提高大约 300+ 倍。这是通过在内存中缓存请求的网站并在请求时提供它们而不构建相同的信息来实现的。更重要的是,当运行多个服务器时,Varnish Cache 可以充当负载平衡器。

这里,我们来记录用 CentOS7 Apache 环境添加Varnish Cache反向网站缓存服务器。

第一、配置防火墙

如果我们有用防火墙或者安全组,需要允许将80端口放行。

$ sudo firewall-cmd –zone=public –permanent –add-service=http

$ sudo firewall-cmd –reload

第二、安装Apache

这个我们是准备在Apache环境中用到缓存加速的,但是如果我们环境已经是那就不要安装。

$ sudo yum install -y httpd

安装HTTP服务。

$ sudo nano /etc/httpd/conf/httpd.conf

准备设置8080端口放行。将Listen 80一行更改为Listen 8080,然后保存退出。

$ sudo systemctl start httpd.service

$ sudo systemctl enable httpd.service

重启和启动HTTP服务。

第三、测试Apache环境

$ sudo touch /var/www/html/test.html

准备添加个文件测试是否可以打开。

$ curl -I http://localhost:8080/test.html

如果测试后看到有200反馈说明没有问题。

第四、安装Varnish缓存

添加EPEL存储库:

$ sudo yum install -y epel-release

安装依赖包:

$ sudo yum install -y pygpgme yum-utils

添加清漆缓存存储库。编辑/etc/yum.repos.d / varnish60lts.repo文件。

$ sudo nano /etc/yum.repos.d/varnish60lts.repo

添加脚本:

[varnish60lts]

name=varnishcache_varnish60lts

baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/7/x86_64

repo_gpgcheck=1

gpgcheck=0

enabled=1

gpgkey=https://packagecloud.io/varnishcache/varnish60lts/gpgkey

sslverify=1

sslcacert=/etc/pki/tls/certs/ca-bundle.crt

metadata_expire=300

然后运行和升级。

$ sudo yum -q makecache -y –disablerepo=’*’ –enablerepo=’varnish60lts’

安装Varnish:

$ sudo yum install -y varnish

安装完毕之后,我们可以用脚本验证是否安装:

varnishd -V

设置开机启动:

$ sudo systemctl enable –now varnish

配置Varnish从默认的6081端口监听80端口。

$ sudo nano /usr/lib/systemd/system/varnish.service

将以ExecStart开头的行从端口6081更改为端口80,然后保存并关闭文件。

ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

重启后生效:

$ sudo systemctl daemon-reload

$ sudo systemctl restart varnish

如果我们要测试是否成功,可以执行:

$ curl -I http://localhost/test.html

然后可以看到反馈的信息包括Varnish信息反馈出来。

未经允许不得转载:老蒋笔记 » 记录在CentOS 7 Apache环境中配置Varnish Cache反向网站缓存服务器

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 「706011751」 获取商家优惠推送