oimi分享美好数字生活 oimi分享美好数字生活
  • 首页
  • AI
  • Lab
  • Apple
  • 生活方式
  • 硬件
  • 0
  • 0

Apache httpd 切换到 Caddy2

OIMI
29 6 月, 2023

Apache httpd 切换到 Caddy2-oimi分享美好数字生活

2022 年 6 月 HTTP/3 的 RFC 发布迄今已经有一年了。市面上常用的几款 Web Server,Caddy Web Server 是最早支持 HTTP/3 的,而 Nginx 直到 2023 年 5 月 23 日 1.25.0 版本才开始体验支持。至于 Apache httpd,也不知道要到什么时候才支持 HTTP/3。
之所以打算正式从 Apache httpd 切换到 Caddy,是因为除 Caddy 之外的 Web Server 配置证书以及自动续期还是比较麻烦。众所周知,一般情况下证书的有效期只有 90 天,需要在过期之前续签。采用 acme.sh 脚本进行自动续签,然后 nginx reload 或者 httpd reload 有时候不太靠谱,所以最后决定 Caddy 一把梭,证书问题一劳永逸。
本文在 (Linux + Caddy + MariaDB + PHP) 环境的基础下,涉及到切换到 Caddy 的一些细节,在此记录一下。

1. 适用于 WordPress 的 Caddy 2 配置

适用于 WordPress 的 Apache httpd 的 .htaccess 基本上不需要转换,是因为 Caddy 2 的 php_fastcgi 指令已经提前做好了一些预设。
php_fastcgi 指令其实是一系列指令的快捷方式。具体解释如下:

route {
	# 为目录请求添加尾部斜线
	@canonicalPath {
		file {path}/index.php
		not path */
	}
	redir @canonicalPath {path}/ 308
	# 如果请求的文件不存在,尝试索引文件
	@indexFiles file {
		try_files {path} {path}/index.php index.php
		split_path .php
	}
	rewrite @indexFiles {http.matchers.file.relative}
	# 将 PHP 文件代理给 FastCGI 应答器
	@phpFiles path *.php
	reverse_proxy @phpFiles  {
		transport fastcgi {
			split .php
		}
	}
}

只需要禁止访问一些 WordPress 的资源即可。比如 xmlrpc.php,以及 wp-content/uploads 下的 php 文件。
下面的配置就是将禁止访问的资源重定向到首页 index.php。

www.example.com {
	header {
		Strict-Transport-Security "max-age=31536000; preload"
		X-Content-Type-Options nosniff
		X-Frame-Options SAMEORIGIN
	}
	# Set this path to your site's directory.
	root * /data/www/yoursiterootfolder
	encode gzip
	@disallowed {
		path /xmlrpc.php
		path /wp-content/uploads/*.php
	}
	rewrite @disallowed /index.php
	# Serve a PHP site through php-fpm
	php_fastcgi unix//run/php-fpm/www.sock
	# Enable the static file server.
	file_server {
		index index.html
	}
	log {
		output file /var/log/caddy/ssl_access.log {
			roll_size 100mb
			roll_keep 3
			roll_keep_for 7d
		}
	}
}

注意替换域名 www.example.com 为你自己的域名,以及网站根目录 /data/www/yoursiterootfolder 为你自定义的路径。同时在此之前,域名的 DNS 也要解析到 Caddy 所在服务器的 IP 地址。

2. 适用于 Typecho 的 Caddy 2 配置

适用于 Typecho 的配置如下:

www.example.com {
	header {
		Strict-Transport-Security "max-age=31536000; preload"
		X-Content-Type-Options nosniff
		X-Frame-Options SAMEORIGIN
	}
	# Set this path to your site's directory.
	root * /data/www/yoursiterootfolder
	encode gzip
	handle_path / {
		try_files {path} {path}/ /index.php/{uri}
	}
	# Serve a PHP site through php-fpm
	php_fastcgi unix//run/php-fpm/www.sock
	# Enable the static file server.
	file_server {
		index index.html
	}
	log {
		output file /var/log/caddy/ssl_access.log {
			roll_size 100mb
			roll_keep 3
			roll_keep_for 7d
		}
	}
}

Typecho 默认是由 index.php 进行静态化路由的,所以try_files {path} {path}/ /index.php/{uri}将网址参数统一由 index.php 重定向。
注意替换域名 www.example.com 为你自己的域名,以及网站根目录 /data/www/yoursiterootfolder 为你自定义的路径。同时在此之前,域名的 DNS 也要解析到 Caddy 所在服务器的 IP 地址。

3. 适用于 YOURLS 的 Caddy 2 配置

适用于 YOURLS 的配置如下:

www.example.com {
	header {
		Strict-Transport-Security "max-age=31536000; preload"
		X-Content-Type-Options nosniff
		X-Frame-Options SAMEORIGIN
	}
	# Set this path to your site's directory.
	root * /data/www/yoursiterootfolder
	encode gzip
	route {
		try_files {path} {path}/index.php /yourls-loader.php
		php_fastcgi unix//run/php-fpm/www.sock {
			split .php
			index index.php
		}
	}
	# Enable the static file server.
	file_server {
		index index.html
	}
	log {
		output file /var/log/caddy/ssl_access.log {
			roll_size 100mb
			roll_keep 3
			roll_keep_for 7d
		}
	}
}

YOURLS 默认是由 yourls-loader.php 进行加载的,所以try_files {path} {path}/index.php /yourls-loader.php将网址参数统一由 yourls-loader.php 尝试重定向。
注意替换域名 www.example.com 为你自己的域名,以及网站根目录 /data/www/yoursiterootfolder 为你自定义的路径。同时在此之前,域名的 DNS 也要解析到 Caddy 所在服务器的 IP 地址。

4. 关于 Caddy 2 发行的证书位置

Caddy 2 最大的优势就是证书一把梭,自动 HTTPS,基本上不让人操心。
其发行的证书默认的存储位置,经过实际使用体验,应该是在如下位置。
Linux 系统

/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/

Windows 系统

C:\Windows\System32\config\systemprofile\AppData\Roaming\Caddy\certificates\acme-v02.api.letsencrypt.org-directory\

5. 关于 Caddy 2 的 API

Caddy 2 默认可以通过使用地址 localhost:2019 的 RESTAPI 进行 HTTP 访问。除非你知道怎么用,否则还是建议将其禁用。不然 Caddy 2 启动默认会监听 localhost 的 2019 端口。
禁用方法很简单,在 Caddyfile 的全局配置理如下设置。

{
	admin off
}

当然禁用之后,就不能使用 systemctl reload caddy 命令了,因为 Caddy 是通过 API 来加载新配置的。
使用 systemctl restart caddy 命令重启即可。

0
Linux + Caddy + MariaDB + PHP
上一篇
Home业界消息Proton Pass – 免费、开源密码管理器,隐私优先的新选择 Proton Pass – 免费、开源密码管理器,隐私优先
下一篇

评论 (0)

再想想
暂无评论

聚合文章

理想星环OS开源项目
亚马逊云科技部署DeepSeek模型
重磅发布:Windows/Office被国外大神破解,全部离线永久激活!
OOMOL Studio 免费、底层开源
DeepSeek开源周首日推出FlashMLA项目 可以显著降低内存占用和计算开销
DeepSeek-R1 发布,性能对标 OpenAI o1 正式版
HEU KMS Activator v42.3.2
Windows/Office通用激活神器MAS v2.9版发布
2024年12月,Windows11 24H2官方原版ISO的系统版本号为26100.2605
小米官方发布「米家集成」,可在 Home Assistant 中使用小米 IoT 智能设备

Android 11 Apache Apple M1 Aria2 AWS Route53 Calm CDN ChatGPT Cloudflare CloudXNS CNNIC Debian Debian 9 Debian 9 Stretch DNS DNSpod Domain Name System gdnsd Google Play HEU KMS Activator iOS 11 iOS11 Beta2 iOS11 Beta2 update1 LEMP Let’s Encrypt LNMP macOS MySQL Nginx OEM PanDownload PHP Postgres PowerDNS PowerDNS-Admin Pure DNS Spectre SQLite3 Stretch Windows 11 24H2 Youtube 公共DNS 数字许可证激活 自控力 降级iOS10

猜你喜欢

理想星环OS开源项目

理想星环OS开源项目

3 5 月, 2025
30 0 0
亚马逊云科技部署DeepSeek模型

亚马逊云科技部署DeepSeek模型

2 4 月, 2025
151 0 0
重磅发布:Windows/Office被国外大神破解,全部离线永久激活!

重磅发布:Windows/Office被国外大神破解,全部离线永久激活!

24 2 月, 2025
359 0 0
OOMOL Studio 免费、底层开源

OOMOL Studio 免费、底层开源

24 2 月, 2025
301 0 0

关于

OIMI(oimi.me)是分享美好数字生活的内容平台,同时还涉及 macOS、iOS 等知名系统的使用技巧。 科技 / 旅行 / 摄影 / 生活方式

社交媒体

Nicky

导航

Nicky
Copyright © 2016-2025 oimi分享美好数字生活. Designed by OIMI.
  • ChatTTS,HyperOS,HEU KMS Activator,Win10/11数字权利激活

OIMI

258
文章
2
评论
135
喜欢