本页内容

  • 环境要求
  • 安装并配置 Apache Web 服务器
  • 在 Debian 11 上安装 Varnish 服务器
  • 配置 Varnish 服务器
  • 配置 Varnish 与 Apache 协同工作
  • 验证 Varnish 缓存
  • 总结

Varnish Cache 是一款开源、功能强大且最受欢迎的 HTTP 加速器之一,被超过 340 万个网站所使用。它可以作为一个反向缓存代理使用,专为高流量动态网站而设计。它用于在 Web 服务器前端缓存内容。其工作原理是将内容缓存在内存中。当用户请求一个已被缓存的页面时,Varnish 会直接提供缓存的副本,而不是反复向后端服务器重复请求同一个页面。许多 VPS 托管服务商将其作为技术栈的一部分来提升网站性能。

本文将会演示如何在 Debian 11 上安装搭配 Apache 的 Varnish Cache。

环境要求

  • 一台运行 Debian 11 的服务器。
  • 已在服务器上配置 root 密码。

安装并配置 Apache Web 服务器

首先,你需要在服务器上安装 Apache Web 服务器。你可以通过运行以下命令来安装:

apt install apache2 -y

安装完 Apache Web 服务器后,你需要编辑 Apache 配置文件,并将默认端口修改为其他端口。

nano /etc/apache2/ports.conf

找到以下这一行:

Listen 80

然后,将其替换为以下这一行:

Listen 8080

完成后保存并关闭文件。

接下来,你还需要编辑 Apache 默认虚拟主机配置文件,并修改默认端口:

nano /etc/apache2/sites-available/000-default.conf

找到以下这一行:

<VirtualHost *:80>

然后,将其替换为以下这一行:

<VirtualHost *:8080>

保存并关闭文件,然后重启 Apache 服务以应用更改:

systemctl restart apache2

现在你可以使用以下命令检查 Apache 监听的端口:

ss -antpl | grep apache2

你应该会看到如下输出:

LISTEN 0      511                *:8080            *:*    users:(("apache2",pid=19315,fd=4),("apache2",pid=19314,fd=4),("apache2",pid=19313,fd=4),("apache2",pid=19312,fd=4),("apache2",pid=19311,fd=4),("apache2",pid=19310,fd=4))

在 Debian 11 上安装 Varnish 服务器

默认情况下,Debian 默认软件仓库中并不提供最新版本的 Varnish cache。因此你需要将 Varnish 仓库添加到 APT 中。

首先,使用以下命令安装所需的依赖项:

apt install debian-archive-keyring curl gnupg apt-transport-https -y

接下来,使用以下命令添加 Varnish GPG 密钥:

curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg

接下来,创建一个 Varnish 源文件:

nano /etc/apt/sources.list.d/varnishcache_varnish70.list

添加以下这一行:

deb https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main
deb-src https://packagecloud.io/varnishcache/varnish70/debian/ bullseye main

保存并关闭文件,然后使用以下命令更新仓库缓存:

apt update -y

接下来,使用以下命令安装 Varnish Cache:

apt install varnish -y

Varnish Cache 安装完成后,使用以下命令启动 Varnish 服务器。

systemctl restart varnish

你也可以使用以下命令验证 Varnish 版本。

varnishd -V

你将看到如下输出。

varnishd (varnish-7.0.3 revision 6a4c6a5c7e66a664b140278c209f0b18c544cab8)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2022 Varnish Software

配置 Varnish 服务器

接下来,你需要编辑 default.vcl 文件并定义你的后端服务器:

nano /etc/varnish/default.vcl

根据你的后端服务器修改以下行:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

完成后保存并关闭文件。

配置 Varnish 与 Apache 协同工作

接下来,你需要为 Varnish 创建一个自定义的服务配置文件。你可以使用以下命令来创建。

mkdir /etc/systemd/system/varnish.service.d
nano /etc/systemd/system/varnish.service.d/customport.conf

添加以下行:

[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m

保存并关闭文件,然后使用以下命令重新加载 systemd 守护进程:

systemctl daemon-reload

接下来,使用以下命令重启 Varnish 服务:

systemctl restart varnish

现在你可以使用以下命令检查 Varnish Cache 的状态:

systemctl status varnish

你应该会看到如下输出:

? varnish.service - Varnish Cache, a high-performance HTTP accelerator
     Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/varnish.service.d
             ??customport.conf
     Active: active (running) since Fri 2022-12-30 05:31:51 UTC; 9s ago
    Process: 24637 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (>
   Main PID: 24638 (varnishd)
      Tasks: 217
     Memory: 132.4M
        CPU: 535ms
     CGroup: /system.slice/varnish.service
             ??24638 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
             ??24652 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m

Dec 30 05:31:50 debian11 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Dec 30 05:31:51 debian11 varnishd[24638]: Version: varnish-7.0.3 revision 6a4c6a5c7e66a664b140278c209f0b18c544cab8
Dec 30 05:31:51 debian11 varnishd[24638]: Platform: Linux,5.10.0-20-amd64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Dec 30 05:31:51 debian11 varnishd[24638]: Child (24652) Started
Dec 30 05:31:51 debian11 varnishd[24638]: Child (24652) said Child starts
Dec 30 05:31:51 debian11 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

至此,Varnish 已安装并监听在 80 端口。你可以使用以下命令进行检查:

ss -antpl | grep :varnishd

你将看到如下输出:

LISTEN 0      1024         0.0.0.0:80         0.0.0.0:*    users:(("cache-main",pid=24652,fd=3),("varnishd",pid=24638,fd=3))                                                                                                        
LISTEN 0      1024       127.0.0.1:8443       0.0.0.0:*    users:(("cache-main",pid=24652,fd=7),("varnishd",pid=24638,fd=7))                                                                                                        
LISTEN 0      10         127.0.0.1:39231      0.0.0.0:*    users:(("varnishd",pid=24638,fd=11))                                                                                                                                     
LISTEN 0      10             [::1]:36395         [::]:*    users:(("varnishd",pid=24638,fd=10))                                                                                                                                     
LISTEN 0      1024            [::]:80            [::]:*    users:(("cache-main",pid=24652,fd=5),("varnishd",pid=24638,fd=5))                                                                                                        
LISTEN 0      1024           [::1]:8443          [::]:*    users:(("cache-main",pid=24652,fd=6),("varnishd",pid=24638,fd=6))                                                                                                        

验证 Varnish 缓存

至此,Varnish 缓存已安装并正在运行。你现在可以使用 CURL 命令来验证 Varnish 缓存:

curl -I http://localhost/

在以下输出中,你将看到 varnish 缓存:

HTTP/1.1 200 OK
Date: Fri, 30 Dec 2022 05:33:00 GMT
Server: Apache/2.4.54 (Debian)
Last-Modified: Fri, 30 Dec 2022 04:42:33 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29cd-5f1043adffc4c-gzip"
Accept-Ranges: bytes
Connection: keep-alive

总结

恭喜!你已经成功在 Debian 11 上安装了搭配 Apache 的 Varnish Cache。你现在可以将 Varnish 缓存作为前端服务器来加快网站的加载速度。如果你有任何问题,欢迎随时向我提问。