如何在PHP中使用 Caddy2 協(xié)同服務(wù)
Caddy Server 是一個(gè)模塊化的現(xiàn)代Web服務(wù)器平臺(tái),支持自動(dòng)HTTPS證書,QUIC和HTTP/2,Zstd和Brotli壓縮,以及各種現(xiàn)代功能以及經(jīng)典的Web服務(wù)器功能,如可配置的虛擬主機(jī),URL重寫和重定向,反向復(fù)制等。
本文介紹了如何將PHP與Caddy Web服務(wù)器版本2系列集成,以及高級(jí)配置。它還將類似的配置與Apache和Nginx配置進(jìn)行了比較,以簡化從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
確認(rèn)服務(wù)已啟動(dòng)
圖片
服務(wù)命令
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
啟動(dòng)Caddy服務(wù)并使其在系統(tǒng)引導(dǎo)時(shí)自動(dòng)啟動(dòng)
sudo systemctl daemon-reload
sudo systemctl start caddy.service
sudo systemctl enable caddy.service
配置
Caddy可以在許多操作系統(tǒng)和基于Linux的發(fā)行版上使用。Caddy文檔解釋了如何安裝Caddy,并將其配置為隨服務(wù)器啟動(dòng)自動(dòng)運(yùn)行的服務(wù)/守護(hù)程序。
Caddy Server配備了安全和高性能的默認(rèn)配置,這使得它很容易配置最小的配置。
當(dāng)Caddy安裝并配置為系統(tǒng)服務(wù)時(shí),默認(rèn)的 /etc/caddy/Caddyfile 可以用作全局配置文件,并使用建議名稱 /etc/caddy/conf 的子目錄來包含各個(gè)站點(diǎn)的配置文件,類似于Apache和Nginx配置。
/etc/caddy
├── Caddyfile
└── conf/
└── caddy.tinywan.com.conf
主次配置調(diào)整
主配置:/etc/caddy/Caddyfile
{
log default {
format console
output file /var/log/caddy/system.log
exclude http.log.access
}
}
import conf/*
次配置目錄:/etc/caddy/Caddyfile/conf。以后就可以在這個(gè)目錄下新增多個(gè)站點(diǎn)配置了,是不是和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
}
應(yīng)用
靜態(tài)網(wǎng)站托管
配置文件
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/
官方默認(rèn)頁面
圖片
自定義頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>開源技術(shù)小棧Caddy2</title>
</head>
<body>
<h1>2024 開源技術(shù)小棧Caddy2入門教程,專注于互聯(lián)網(wǎng)技術(shù)分享</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 會(huì)被重定向到 https://www.tinywan.com/
PHP-FPM 集成
與Apache Web服務(wù)器和Nginx與PHP集成的方式類似,Caddy也使用Caddy的FastCGI反向代理與PHP集成。
其基本思想是,當(dāng)Caddy接收到一個(gè)應(yīng)該用PHP處理的請(qǐng)求(例如,一個(gè)對(duì)帶有 .php 擴(kuò)展名的文件名的請(qǐng)求)時(shí),請(qǐng)求被發(fā)送到PHP-FPM,在那里執(zhí)行PHP應(yīng)用程序,響應(yīng)被發(fā)送回Caddy以返回給用戶。
異常 502 Bad Gateway 錯(cuò)誤日志
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 開源技術(shù)小棧";
訪問地址:https://caddy-php.tinywan.com/,該域名需要提前解析好哦!
圖片