oimi分享美好数字生活 oimi分享美好数字生活
  • 首页
  • AI
  • Lab
  • Apple
  • 生活方式
  • 硬件
首页 › Lab › Apache httpd 切换到 Caddy2
  • 0
  • 0

Apache httpd 切换到 Caddy2

OIMI
29 6 月, 2023
4,727 views

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)

再想想
暂无评论

聚合文章

Hermes Agent Windows 原生支持正式发布
Hermes 凭什么爆:不是技术,是时机
Hermes 原生桌面版
OpenClaw 到底能干嘛?
OpenFang 
Goose
GitHub Trending- Shannon
nanobot超轻量级个人AI助手
beautiful-mermaid
BettaFish

猜你喜欢

palera1n

palera1n

30 12 月, 2022
4,763 0 1
Pierced 是一款钉钉开放平台官方免费提供的内网穿透工具

Pierced 是一款钉钉开放平台官方免费提供的内网穿透工具

18 2 月, 2021
2,899 0 0
一条命令,永久激活Office 2024!

一条命令,永久激活Office 2024!

14 9 月, 2024
4,234 0 0
Microsoft 365永久激活!

Microsoft 365永久激活!

5 12 月, 2022
2,581 0 0

关于

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

社交媒体

Nicky

导航

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

OIMI

272
文章
1
评论
138
喜欢