如何在 Ubuntu 22.04 上使用 Apache 安装和配置 Varnish
本教程适用于这些操作系统版本
- Ubuntu 22.04(Jammy Jellyfish)
- Ubuntu 16.04(Xenial Xerus)
在此页
- 要求
- 安装和配置 Apache Web 服务器
- 安装清漆缓存
- 配置 Varnish 以与 Apache 一起工作
- 验证清漆缓存
- 结论
Varnish Cache 是专为高流量动态网站设计的高性能 HTTP 加速器。它充当客户端和服务器之间的中间人,在所有入站请求到达您的 Web 服务器后端之前处理它们。 Varnish 缓存 Web 服务器提供的每个网页的副本。当用户请求缓存页面时,Varnish 提供缓存副本,而不是从后端服务器一次又一次地请求相同的页面。它使 Varnish 的 Full Page Cache 非常适合拥有高流量商店和数千种产品的商店。
这篇文章将向您展示如何在 Ubuntu 22.04 上安装 Varnish Cache 并使用 Apache 配置它。
要求
- 一台运行 Ubuntu 22.04 的服务器。
- 在您的服务器上配置了根密码。
安装和配置 Apache Web 服务器
在开始之前,必须在您的服务器上安装 Apache Web 服务器。如果没有安装,可以使用以下命令安装:
apt install apache2 -y
接下来,您需要编辑 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 8080
您应该看到以下输出:
LISTEN 0 511 *:8080 *:* users:(("apache2",pid=2553,fd=4),("apache2",pid=2552,fd=4),("apache2",pid=2551,fd=4))
安装清漆缓存
默认情况下,最新版本的 Varnish 缓存在 Ubuntu 默认存储库中不可用。因此,您需要将 Varnish 存储库添加到 APT。
首先,使用以下命令安装所需的依赖项:
apt install debian-archive-keyring curl gnupg apt-transport-https -y
接下来,使用以下命令添加 Varnish GPG 密钥:
curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | sudo 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/ubuntu/ focal main
deb-src https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
保存并关闭文件,然后创建另一个配置文件:
nano /etc/apt/preferences.d/varnish
添加以下行:
Package: varnish
Pin: origin packagecloud.io
Pin-Priority: 900
保存并关闭文件,然后使用以下命令更新存储库缓存:
apt update
接下来,使用以下命令安装 Varnish Cache:
apt install varnish -y
安装 Varnish Cache 后,编辑 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 Tue 2022-10-18 13:07:44 UTC; 14s ago
Process: 4968 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (c>
Main PID: 4969 (varnishd)
Tasks: 217
Memory: 90.6M
CPU: 595ms
CGroup: /system.slice/varnish.service
??4969 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
??4983 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
Oct 18 13:07:43 ubuntu2204 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Version: varnish-7.0.3 revision 6a4c6a5c7e66a664b140278c209f0b18c544cab8
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Platform: Linux,5.15.0-33-generic,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Child (4983) Started
Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Child (4983) said Child starts
Oct 18 13:07:44 ubuntu2204 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
至此Varnish已经安装完成,并监听80端口,可以通过以下命令查看:
ss -antpl | grep :80
您将获得以下输出:
LISTEN 0 1024 0.0.0.0:80 0.0.0.0:* users:(("cache-main",pid=4983,fd=3),("varnishd",pid=4969,fd=3))
LISTEN 0 1024 [::]:80 [::]:* users:(("cache-main",pid=4983,fd=5),("varnishd",pid=4969,fd=5))
LISTEN 0 511 *:8080 *:* users:(("apache2",pid=4749,fd=4),("apache2",pid=4748,fd=4),("apache2",pid=4745,fd=4))
验证清漆缓存
您现在可以使用 CURL 命令验证 Varnish 缓存:
curl -I http://localhost/
您将在以下输出中获得清漆缓存:
HTTP/1.1 200 OK
Date: Tue, 18 Oct 2022 13:08:27 GMT
Server: Apache/2.4.52 (Ubuntu)
Last-Modified: Tue, 18 Oct 2022 13:03:09 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
ETag: W/"29af-5eb4eb6b9e071-gzip"
Accept-Ranges: bytes
Content-Length: 10671
Connection: keep-alive
结论
恭喜!您已经在 Ubuntu 22.04 上成功安装了带有 Apache 的 Varnish Cache。您现在可以使用服务器中的 Varnish 缓存来加快网站加载时间。如果您有任何问题,请随时问我。