如何在PHP中使用 Caddy2 協同服務
Caddy Server 是一個模塊化的現代Web服務器平臺,支持自動HTTPS證書,QUIC和HTTP/2,Zstd和Brotli壓縮,以及各種現代功能以及經典的Web服務器功能,如可配置的虛擬主機,URL重寫和重定向,反向復制等。
本文介紹了如何將PHP與Caddy Web服務器版本2系列集成,以及高級配置。它還將類似的配置與Apache和Nginx配置進行了比較,以簡化從Apache和Nginx到Caddy的遷移。
Caddy
安裝
官方安裝文檔:https://caddyserver.com/docs/install
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
確認服務已啟動
圖片
服務命令
systemctl start caddy
caddy start
caddy run # Starts server and blocks indefinitely
systemctl stop caddy
caddy stop
systemctl reload caddy
caddy reload
systemctl restart caddy
caddy stop && caddy start
啟動Caddy服務并使其在系統引導時自動啟動
sudo systemctl daemon-reload
sudo systemctl start caddy.service
sudo systemctl enable caddy.service
配置
Caddy可以在許多操作系統和基于Linux的發行版上使用。Caddy文檔解釋了如何安裝Caddy,并將其配置為隨服務器啟動自動運行的服務/守護程序。
Caddy Server配備了安全和高性能的默認配置,這使得它很容易配置最小的配置。
當Caddy安裝并配置為系統服務時,默認的 /etc/caddy/Caddyfile 可以用作全局配置文件,并使用建議名稱 /etc/caddy/conf 的子目錄來包含各個站點的配置文件,類似于Apache和Nginx配置。
/etc/caddy
├── Caddyfile
└── conf/
└── caddy.tinywan.com.conf
主次配置調整
主配置:/etc/caddy/Caddyfile
{
log default {
format console
output file /var/log/caddy/system.log
exclude http.log.access
}
}
import conf/*
次配置目錄:/etc/caddy/Caddyfile/conf。以后就可以在這個目錄下新增多個站點配置了,是不是和Nginx看起來一樣嘍!
例如:caddy.tinywan.com.conf
caddy.tinywan.com {
# Set this path to your site's directory.
root * /home/www/website/caddy
# Enable the static file server.
file_server
}
應用
靜態網站托管
配置文件
caddy.tinywan.com {
# Set this path to your site's directory.
root * /home/www/website/caddy
# Enable the static file server.
file_server
}
訪問域名:https://caddy.tinywan.com/
官方默認頁面
圖片
自定義頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>開源技術小棧Caddy2</title>
</head>
<body>
<h1>2024 開源技術小棧Caddy2入門教程,專注于互聯網技術分享</h1>
</body>
</html>
圖片
反向代理
proxy-caddy.tinywan.com {
reverse_proxy localhost:6140
}
HTTP 重定向添加 www. 子域名
tinywan.com {
redir https://www.{host}{uri}
}
www.tinywan.com {
# Set this path to your site's directory.
root * /home/www/website/full-stack/dist
# Enable the static file server.
file_server
}
訪問域名 tinywan.com 會被重定向到 https://www.tinywan.com/
PHP-FPM 集成
與Apache Web服務器和Nginx與PHP集成的方式類似,Caddy也使用Caddy的FastCGI反向代理與PHP集成。
其基本思想是,當Caddy接收到一個應該用PHP處理的請求(例如,一個對帶有 .php 擴展名的文件名的請求)時,請求被發送到PHP-FPM,在那里執行PHP應用程序,響應被發送回Caddy以返回給用戶。
異常 502 Bad Gateway 錯誤日志
2024/02/02 09:42:36.814 ERROR http.log.error dialing backend: dial unix /var/run/php8.0.7-fpm.sock: connect: permission denied
修改用戶組
sudo vim /lib/systemd/system/caddy.service
修改前
User=caddy
Group=caddy
修改后
User=www
Group=www
完整配置
caddy-php.tinywan.com {
# Resolve the root directory for the app
root * /home/www/website/demo/public
# Allow caddy to serve static files
file_server
# Encode responses in zstd or gzip, depending on the
# availability indicated by the browser.
encode gzip
# Configures multiple PHP-related settings
php_fastcgi unix//var/run/php8.0.7-fpm.sock
}
目錄 /home/www/website/demo/public 新建文件index.php
<?php
echo "Hello 開源技術小棧";
訪問地址:https://caddy-php.tinywan.com/,該域名需要提前解析好哦!
圖片