Varnish 是一个 Web 应用程序加速器,旨在通过缓存经常访问的内容来提高 Web 服务器的性能。通过直接从内存中提供缓存内容,它大大减少了服务器负载,缩短了响应时间,并最终增强了用户体验。
在 Debian 上安装 Varnish 12 Bookworm
步骤 1。顺利安装 Varnish 的第一步是确保您的 Debian 12 系统是最新的。这样可以防止冲突并确保兼容性。打开终端并执行以下命令:
<span class="pln">sudo apt update sudo apt upgrade</span>
第2步。安装基本工具。
若要安装必要的工具,请运行以下命令:
<span class="pln">sudo apt install curl sudo apt install apt</span><span class="pun">-</span><span class="pln">transport</span><span class="pun">-</span><span class="pln">https</span>
第 3 步。在 Debian 12 上安装 Varnish。
- 方法 1:从 Debian 存储库安装 Varnish
通过运行以下命令安装 Varnish:
<span class="pln">sudo apt install varnish</span>
安装完成后,通过运行以下命令启动 Varnish 服务:
<span class="pln">sudo systemctl start varnish</span>
若要检查 Varnish 服务的状态,请运行以下命令:
<span class="pln">sudo systemctl status varnish</span>
要验证 Varnish 安装并检查其版本,请使用以下命令:
<span class="pln">varnishd </span><span class="pun">-</span><span class="pln">V</span>
- 方法 2:从 Varnish 缓存存储库安装 Varnish
通过运行以下命令将 Varnish Cache 存储库添加到您的系统:
<span class="pln">curl </span><span class="pun">-</span><span class="pln">s https</span><span class="pun">:</span><span class="com">//packagecloud.io/install/repositories/varnishcache/varnish74/script.deb.sh | sudo bash</span>
通过运行以下命令更新包列表:
<span class="pln">sudo apt update</span>
现在通过运行以下命令安装 Varnish:
<span class="pln">sudo apt install varnish</span><span class="pun">=</span><span class="lit">7.4</span><span class="pun">.</span><span class="lit">1</span><span class="pun">-</span><span class="lit">1</span><span class="pun">~</span><span class="pln">bookworm</span>
第 3 步。配置 Varnish。
Varnish 基于 Varnish 配置语言 (VCL) 运行。了解和配置此语言是优化 Web 服务器性能的关键。
让我们创建一个自定义 VCL 配置文件,以根据您的特定需求定制 Varnish。您可以在此处定义缓存规则和后端服务器设置:
<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">varnish</span><span class="pun">/</span><span class="kwd">default</span><span class="pun">.</span><span class="pln">vcl</span>
在此文件中,您可以定义后端服务器和缓存规则。下面是一个基本示例:
<span class="pln">backend </span><span class="kwd">default</span> <span class="pun">{</span> <span class="pun">.</span><span class="pln">host </span><span class="pun">=</span> <span class="str">"127.0.0.1"</span><span class="pun">;</span> <span class="pun">.</span><span class="pln">port </span><span class="pun">=</span> <span class="str">"8080"</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">sub</span><span class="pln"> vcl_recv </span><span class="pun">{</span> <span class="com"># Define caching rules here</span> <span class="pun">}</span> <span class="kwd">sub</span><span class="pln"> vcl_fetch </span><span class="pun">{</span> <span class="com"># Modify server responses here</span> <span class="pun">}</span>
请记住保存更改。这只是一个起点;您可以根据需要进一步自定义配置。
自定义 VCL 配置到位后,您需要将其加载到 Varnish 中。使用以下命令:
<span class="pln">sudo systemctl reload varnish</span>
您可以通过运行以下命令来验证 VCL 配置:
<span class="pln">varnishd </span><span class="pun">-</span><span class="pln">C </span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">varnish</span><span class="pun">/</span><span class="kwd">default</span><span class="pun">.</span><span class="pln">vcl</span>
第 4 步。用一个简单的网站测试清漆。
创建一个基本的 HTML 文件来测试 Varnish 缓存。将“”替换为您网站的内容:your_website_content
<span class="pln">echo </span><span class="str">"your_website_content"</span> <span class="pun">|</span><span class="pln"> sudo tee </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">html</span>
现在,使用 ‘curl’ 检查 Varnish 是否正在提供缓存的内容:
<span class="pln">curl </span><span class="pun">-</span><span class="pln">I http</span><span class="pun">:</span><span class="com">//localhost</span>
感谢您使用本教程在 Debian 12 Bookworm 上安装最新版本的 Varnish HTTP 缓存。如需更多帮助或有用信息,我们建议您查看 Varnish 官方网站。