php-server.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package caddy
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "time"
  11. mercureModule "github.com/dunglas/mercure/caddy"
  12. "github.com/caddyserver/caddy/v2"
  13. "github.com/caddyserver/caddy/v2/caddyconfig"
  14. caddycmd "github.com/caddyserver/caddy/v2/cmd"
  15. "github.com/caddyserver/caddy/v2/modules/caddyhttp"
  16. "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
  17. "github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver"
  18. "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
  19. "github.com/caddyserver/certmagic"
  20. "github.com/dunglas/frankenphp"
  21. "go.uber.org/zap"
  22. "github.com/spf13/cobra"
  23. )
  24. func init() {
  25. caddycmd.RegisterCommand(caddycmd.Command{
  26. Name: "php-server",
  27. Usage: "[--domain <example.com>] [--root <path>] [--listen <addr>] [--worker /path/to/worker.php<,nb-workers>] [--access-log] [--debug] [--no-compress] [--mercure]",
  28. Short: "Spins up a production-ready PHP server",
  29. Long: `
  30. A simple but production-ready PHP server. Useful for quick deployments,
  31. demos, and development.
  32. The listener's socket address can be customized with the --listen flag.
  33. If a domain name is specified with --domain, the default listener address
  34. will be changed to the HTTPS port and the server will use HTTPS. If using
  35. a public domain, ensure A/AAAA records are properly configured before
  36. using this option.
  37. For more advanced use cases, see https://github.com/dunglas/frankenphp/blob/main/docs/config.md`,
  38. CobraFunc: func(cmd *cobra.Command) {
  39. cmd.Flags().StringP("domain", "d", "", "Domain name at which to serve the files")
  40. cmd.Flags().StringP("root", "r", "", "The path to the root of the site")
  41. cmd.Flags().StringP("listen", "l", "", "The address to which to bind the listener")
  42. cmd.Flags().StringArrayP("worker", "w", []string{}, "Worker script")
  43. cmd.Flags().BoolP("access-log", "a", false, "Enable the access log")
  44. cmd.Flags().BoolP("debug", "v", false, "Enable verbose debug logs")
  45. cmd.Flags().BoolP("mercure", "m", false, "Enable the built-in Mercure.rocks hub")
  46. cmd.Flags().BoolP("no-compress", "", false, "Disable Zstandard, Brotli and Gzip compression")
  47. cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdPHPServer)
  48. },
  49. })
  50. }
  51. // cmdPHPServer is freely inspired from the file-server command of the Caddy server (Apache License 2.0, Matthew Holt and The Caddy Authors)
  52. func cmdPHPServer(fs caddycmd.Flags) (int, error) {
  53. caddy.TrapSignals()
  54. domain := fs.String("domain")
  55. root := fs.String("root")
  56. listen := fs.String("listen")
  57. accessLog := fs.Bool("access-log")
  58. debug := fs.Bool("debug")
  59. compress := !fs.Bool("no-compress")
  60. mercure := fs.Bool("mercure")
  61. workers, err := fs.GetStringArray("worker")
  62. if err != nil {
  63. panic(err)
  64. }
  65. var workersOption []workerConfig
  66. if len(workers) != 0 {
  67. workersOption = make([]workerConfig, 0, len(workers))
  68. for _, worker := range workers {
  69. parts := strings.SplitN(worker, ",", 2)
  70. if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(parts[0]) {
  71. parts[0] = filepath.Join(frankenphp.EmbeddedAppPath, parts[0])
  72. }
  73. var num int
  74. if len(parts) > 1 {
  75. num, _ = strconv.Atoi(parts[1])
  76. }
  77. workersOption = append(workersOption, workerConfig{FileName: parts[0], Num: num})
  78. }
  79. }
  80. if frankenphp.EmbeddedAppPath != "" {
  81. if _, err := os.Stat(filepath.Join(frankenphp.EmbeddedAppPath, "php.ini")); err == nil {
  82. iniScanDir := os.Getenv("PHP_INI_SCAN_DIR")
  83. if err := os.Setenv("PHP_INI_SCAN_DIR", iniScanDir+":"+frankenphp.EmbeddedAppPath); err != nil {
  84. return caddy.ExitCodeFailedStartup, err
  85. }
  86. }
  87. if _, err := os.Stat(filepath.Join(frankenphp.EmbeddedAppPath, "Caddyfile")); err == nil {
  88. config, _, err := caddycmd.LoadConfig(filepath.Join(frankenphp.EmbeddedAppPath, "Caddyfile"), "")
  89. if err != nil {
  90. return caddy.ExitCodeFailedStartup, err
  91. }
  92. if err = caddy.Load(config, true); err != nil {
  93. return caddy.ExitCodeFailedStartup, err
  94. }
  95. select {}
  96. }
  97. if root == "" {
  98. root = filepath.Join(frankenphp.EmbeddedAppPath, defaultDocumentRoot)
  99. } else if filepath.IsLocal(root) {
  100. root = filepath.Join(frankenphp.EmbeddedAppPath, root)
  101. }
  102. }
  103. const indexFile = "index.php"
  104. extensions := []string{"php"}
  105. tryFiles := []string{"{http.request.uri.path}", "{http.request.uri.path}/" + indexFile, indexFile}
  106. phpHandler := FrankenPHPModule{
  107. Root: root,
  108. SplitPath: extensions,
  109. }
  110. // route to redirect to canonical path if index PHP file
  111. redirMatcherSet := caddy.ModuleMap{
  112. "file": caddyconfig.JSON(fileserver.MatchFile{
  113. Root: root,
  114. TryFiles: []string{"{http.request.uri.path}/" + indexFile},
  115. }, nil),
  116. "not": caddyconfig.JSON(caddyhttp.MatchNot{
  117. MatcherSetsRaw: []caddy.ModuleMap{
  118. {
  119. "path": caddyconfig.JSON(caddyhttp.MatchPath{"*/"}, nil),
  120. },
  121. },
  122. }, nil),
  123. }
  124. redirHandler := caddyhttp.StaticResponse{
  125. StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
  126. Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
  127. }
  128. redirRoute := caddyhttp.Route{
  129. MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
  130. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
  131. }
  132. // route to rewrite to PHP index file
  133. rewriteMatcherSet := caddy.ModuleMap{
  134. "file": caddyconfig.JSON(fileserver.MatchFile{
  135. Root: root,
  136. TryFiles: tryFiles,
  137. SplitPath: extensions,
  138. }, nil),
  139. }
  140. rewriteHandler := rewrite.Rewrite{
  141. URI: "{http.matchers.file.relative}",
  142. }
  143. rewriteRoute := caddyhttp.Route{
  144. MatcherSetsRaw: []caddy.ModuleMap{rewriteMatcherSet},
  145. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(rewriteHandler, "handler", "rewrite", nil)},
  146. }
  147. // route to actually pass requests to PHP files;
  148. // match only requests that are for PHP files
  149. pathList := []string{}
  150. for _, ext := range extensions {
  151. pathList = append(pathList, "*"+ext)
  152. }
  153. phpMatcherSet := caddy.ModuleMap{
  154. "path": caddyconfig.JSON(pathList, nil),
  155. }
  156. // create the PHP route which is
  157. // conditional on matching PHP files
  158. phpRoute := caddyhttp.Route{
  159. MatcherSetsRaw: []caddy.ModuleMap{phpMatcherSet},
  160. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(phpHandler, "handler", "php", nil)},
  161. }
  162. fileRoute := caddyhttp.Route{
  163. MatcherSetsRaw: []caddy.ModuleMap{},
  164. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(fileserver.FileServer{Root: root}, "handler", "file_server", nil)},
  165. }
  166. subroute := caddyhttp.Subroute{
  167. Routes: caddyhttp.RouteList{redirRoute, rewriteRoute, phpRoute, fileRoute},
  168. }
  169. if compress {
  170. gzip, err := caddy.GetModule("http.encoders.gzip")
  171. if err != nil {
  172. return caddy.ExitCodeFailedStartup, err
  173. }
  174. br, err := caddy.GetModule("http.encoders.br")
  175. if err != nil {
  176. return caddy.ExitCodeFailedStartup, err
  177. }
  178. zstd, err := caddy.GetModule("http.encoders.zstd")
  179. if err != nil {
  180. return caddy.ExitCodeFailedStartup, err
  181. }
  182. encodeRoute := caddyhttp.Route{
  183. MatcherSetsRaw: []caddy.ModuleMap{},
  184. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(encode.Encode{
  185. EncodingsRaw: caddy.ModuleMap{
  186. "zstd": caddyconfig.JSON(zstd.New(), nil),
  187. "br": caddyconfig.JSON(br.New(), nil),
  188. "gzip": caddyconfig.JSON(gzip.New(), nil),
  189. },
  190. Prefer: []string{"zstd", "br", "gzip"},
  191. }, "handler", "encode", nil)},
  192. }
  193. subroute.Routes = append(caddyhttp.RouteList{encodeRoute}, subroute.Routes...)
  194. }
  195. if mercure {
  196. mercurePublisherJwtKey := os.Getenv("MERCURE_PUBLISHER_JWT_KEY")
  197. if mercurePublisherJwtKey == "" {
  198. panic(`The "MERCURE_PUBLISHER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
  199. }
  200. mercureSubscriberJwtKey := os.Getenv("MERCURE_SUBSCRIBER_JWT_KEY")
  201. if mercureSubscriberJwtKey == "" {
  202. panic(`The "MERCURE_SUBSCRIBER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
  203. }
  204. mercureRoute := caddyhttp.Route{
  205. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(
  206. mercureModule.Mercure{
  207. PublisherJWT: mercureModule.JWTConfig{
  208. Alg: os.Getenv("MERCURE_PUBLISHER_JWT_ALG"),
  209. Key: mercurePublisherJwtKey,
  210. },
  211. SubscriberJWT: mercureModule.JWTConfig{
  212. Alg: os.Getenv("MERCURE_SUBSCRIBER_JWT_ALG"),
  213. Key: mercureSubscriberJwtKey,
  214. },
  215. },
  216. "handler",
  217. "mercure",
  218. nil,
  219. ),
  220. },
  221. }
  222. subroute.Routes = append(caddyhttp.RouteList{mercureRoute}, subroute.Routes...)
  223. }
  224. route := caddyhttp.Route{
  225. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(subroute, "handler", "subroute", nil)},
  226. }
  227. if domain != "" {
  228. route.MatcherSetsRaw = []caddy.ModuleMap{
  229. {
  230. "host": caddyconfig.JSON(caddyhttp.MatchHost{domain}, nil),
  231. },
  232. }
  233. }
  234. server := &caddyhttp.Server{
  235. ReadHeaderTimeout: caddy.Duration(10 * time.Second),
  236. IdleTimeout: caddy.Duration(30 * time.Second),
  237. MaxHeaderBytes: 1024 * 10,
  238. Routes: caddyhttp.RouteList{route},
  239. }
  240. if listen == "" {
  241. if domain == "" {
  242. listen = ":80"
  243. } else {
  244. listen = ":" + strconv.Itoa(certmagic.HTTPSPort)
  245. }
  246. }
  247. server.Listen = []string{listen}
  248. if accessLog {
  249. server.Logs = &caddyhttp.ServerLogConfig{}
  250. }
  251. httpApp := caddyhttp.App{
  252. Servers: map[string]*caddyhttp.Server{"php": server},
  253. }
  254. var false bool
  255. cfg := &caddy.Config{
  256. Admin: &caddy.AdminConfig{
  257. Disabled: true,
  258. Config: &caddy.ConfigSettings{
  259. Persist: &false,
  260. },
  261. },
  262. AppsRaw: caddy.ModuleMap{
  263. "http": caddyconfig.JSON(httpApp, nil),
  264. "frankenphp": caddyconfig.JSON(FrankenPHPApp{Workers: workersOption}, nil),
  265. },
  266. }
  267. if debug {
  268. cfg.Logging = &caddy.Logging{
  269. Logs: map[string]*caddy.CustomLog{
  270. "default": {
  271. BaseLog: caddy.BaseLog{Level: zap.DebugLevel.CapitalString()},
  272. },
  273. },
  274. }
  275. }
  276. err = caddy.Run(cfg)
  277. if err != nil {
  278. return caddy.ExitCodeFailedStartup, err
  279. }
  280. log.Printf("Caddy serving PHP app on %s", listen)
  281. select {}
  282. }