如何在 Debian 11 上安装和使用 Varnish Cache
在此页
- 要求
- 安装和配置 Apache Web 服务器
- 安装 Varnish 服务器 Debian 11
- 配置 Varnish 服务器
- 配置 Varnish 以与 Apache 一起工作
- 验证清漆缓存
- 结论
Varnish Cache 是一种开源的、功能强大的、最受欢迎的 HTTP 加速器之一,被超过 340 万个网站使用。它可以用作专门为高流量动态网站设计的反向缓存代理。它用于在 Web 服务器前面缓存内容。它通过在内存中缓存内容来工作。当用户请求缓存页面时,Varnish 提供缓存副本,而不是从后端服务器一次又一次地请求相同的页面。许多 VPS 托管提供商使用它作为其技术堆栈的一部分来提高网站性能。
这篇文章将向您展示如何在 Debian 11 上使用 Apache 安装 Varnish Cache。
要求
- 运行 Debian 11 的服务器。
- 在您的服务器上配置了根密码。
安装和配置 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 存储库添加到 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
配置清漆服务器
接下来,您需要编辑 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 缓存的状态:
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 缓存已安装并运行。您现在可以使用 CURL 命令验证 Varnish 缓存:
curl -I http://localhost/
您将在以下输出中获得清漆缓存:
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 缓存作为前端服务器来加快网站加载时间。如果您有任何问题,请随时问我。