php-server.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. "go.uber.org/zap/zapcore"
  13. "github.com/caddyserver/caddy/v2"
  14. "github.com/caddyserver/caddy/v2/caddyconfig"
  15. caddycmd "github.com/caddyserver/caddy/v2/cmd"
  16. "github.com/caddyserver/caddy/v2/modules/caddyhttp"
  17. "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
  18. "github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver"
  19. "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
  20. "github.com/caddyserver/certmagic"
  21. "github.com/dunglas/frankenphp"
  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>] [--watch <paths...>] [--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().StringArrayP("watch", "", []string{}, "Directory to watch for file changes")
  44. cmd.Flags().BoolP("access-log", "a", false, "Enable the access log")
  45. cmd.Flags().BoolP("debug", "v", false, "Enable verbose debug logs")
  46. cmd.Flags().BoolP("mercure", "m", false, "Enable the built-in Mercure.rocks hub")
  47. cmd.Flags().BoolP("no-compress", "", false, "Disable Zstandard, Brotli and Gzip compression")
  48. cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdPHPServer)
  49. },
  50. })
  51. }
  52. // cmdPHPServer is freely inspired from the file-server command of the Caddy server (Apache License 2.0, Matthew Holt and The Caddy Authors)
  53. func cmdPHPServer(fs caddycmd.Flags) (int, error) {
  54. caddy.TrapSignals()
  55. domain := fs.String("domain")
  56. root := fs.String("root")
  57. listen := fs.String("listen")
  58. accessLog := fs.Bool("access-log")
  59. debug := fs.Bool("debug")
  60. compress := !fs.Bool("no-compress")
  61. mercure := fs.Bool("mercure")
  62. workers, err := fs.GetStringArray("worker")
  63. if err != nil {
  64. panic(err)
  65. }
  66. watch, err := fs.GetStringArray("watch")
  67. if err != nil {
  68. panic(err)
  69. }
  70. var workersOption []workerConfig
  71. if len(workers) != 0 {
  72. workersOption = make([]workerConfig, 0, len(workers))
  73. for _, worker := range workers {
  74. parts := strings.SplitN(worker, ",", 2)
  75. if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(parts[0]) {
  76. parts[0] = filepath.Join(frankenphp.EmbeddedAppPath, parts[0])
  77. }
  78. var num int
  79. if len(parts) > 1 {
  80. num, _ = strconv.Atoi(parts[1])
  81. }
  82. workersOption = append(workersOption, workerConfig{FileName: parts[0], Num: num})
  83. }
  84. workersOption[0].Watch = watch
  85. }
  86. if frankenphp.EmbeddedAppPath != "" {
  87. if _, err := os.Stat(filepath.Join(frankenphp.EmbeddedAppPath, "php.ini")); err == nil {
  88. iniScanDir := os.Getenv("PHP_INI_SCAN_DIR")
  89. if err := os.Setenv("PHP_INI_SCAN_DIR", iniScanDir+":"+frankenphp.EmbeddedAppPath); err != nil {
  90. return caddy.ExitCodeFailedStartup, err
  91. }
  92. }
  93. if _, err := os.Stat(filepath.Join(frankenphp.EmbeddedAppPath, "Caddyfile")); err == nil {
  94. config, _, err := caddycmd.LoadConfig(filepath.Join(frankenphp.EmbeddedAppPath, "Caddyfile"), "")
  95. if err != nil {
  96. return caddy.ExitCodeFailedStartup, err
  97. }
  98. if err = caddy.Load(config, true); err != nil {
  99. return caddy.ExitCodeFailedStartup, err
  100. }
  101. select {}
  102. }
  103. if root == "" {
  104. root = filepath.Join(frankenphp.EmbeddedAppPath, defaultDocumentRoot)
  105. } else if filepath.IsLocal(root) {
  106. root = filepath.Join(frankenphp.EmbeddedAppPath, root)
  107. }
  108. }
  109. const indexFile = "index.php"
  110. extensions := []string{".php"}
  111. tryFiles := []string{"{http.request.uri.path}", "{http.request.uri.path}/" + indexFile, indexFile}
  112. rrs := true
  113. phpHandler := FrankenPHPModule{
  114. Root: root,
  115. SplitPath: extensions,
  116. ResolveRootSymlink: &rrs,
  117. }
  118. // route to redirect to canonical path if index PHP file
  119. redirMatcherSet := caddy.ModuleMap{
  120. "file": caddyconfig.JSON(fileserver.MatchFile{
  121. Root: root,
  122. TryFiles: []string{"{http.request.uri.path}/" + indexFile},
  123. }, nil),
  124. "not": caddyconfig.JSON(caddyhttp.MatchNot{
  125. MatcherSetsRaw: []caddy.ModuleMap{
  126. {
  127. "path": caddyconfig.JSON(caddyhttp.MatchPath{"*/"}, nil),
  128. },
  129. },
  130. }, nil),
  131. }
  132. redirHandler := caddyhttp.StaticResponse{
  133. StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
  134. Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
  135. }
  136. redirRoute := caddyhttp.Route{
  137. MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
  138. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
  139. }
  140. // route to rewrite to PHP index file
  141. rewriteMatcherSet := caddy.ModuleMap{
  142. "file": caddyconfig.JSON(fileserver.MatchFile{
  143. Root: root,
  144. TryFiles: tryFiles,
  145. SplitPath: extensions,
  146. }, nil),
  147. }
  148. rewriteHandler := rewrite.Rewrite{
  149. URI: "{http.matchers.file.relative}",
  150. }
  151. rewriteRoute := caddyhttp.Route{
  152. MatcherSetsRaw: []caddy.ModuleMap{rewriteMatcherSet},
  153. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(rewriteHandler, "handler", "rewrite", nil)},
  154. }
  155. // route to actually pass requests to PHP files;
  156. // match only requests that are for PHP files
  157. var pathList []string
  158. for _, ext := range extensions {
  159. pathList = append(pathList, "*"+ext)
  160. }
  161. phpMatcherSet := caddy.ModuleMap{
  162. "path": caddyconfig.JSON(pathList, nil),
  163. }
  164. // create the PHP route which is
  165. // conditional on matching PHP files
  166. phpRoute := caddyhttp.Route{
  167. MatcherSetsRaw: []caddy.ModuleMap{phpMatcherSet},
  168. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(phpHandler, "handler", "php", nil)},
  169. }
  170. fileRoute := caddyhttp.Route{
  171. MatcherSetsRaw: []caddy.ModuleMap{},
  172. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(fileserver.FileServer{Root: root}, "handler", "file_server", nil)},
  173. }
  174. subroute := caddyhttp.Subroute{
  175. Routes: caddyhttp.RouteList{redirRoute, rewriteRoute, phpRoute, fileRoute},
  176. }
  177. if compress {
  178. gzip, err := caddy.GetModule("http.encoders.gzip")
  179. if err != nil {
  180. return caddy.ExitCodeFailedStartup, err
  181. }
  182. br, err := caddy.GetModule("http.encoders.br")
  183. if err != nil {
  184. return caddy.ExitCodeFailedStartup, err
  185. }
  186. zstd, err := caddy.GetModule("http.encoders.zstd")
  187. if err != nil {
  188. return caddy.ExitCodeFailedStartup, err
  189. }
  190. var (
  191. encodings caddy.ModuleMap
  192. prefer []string
  193. )
  194. if brotli {
  195. encodings = caddy.ModuleMap{
  196. "zstd": caddyconfig.JSON(zstd.New(), nil),
  197. "br": caddyconfig.JSON(br.New(), nil),
  198. "gzip": caddyconfig.JSON(gzip.New(), nil),
  199. }
  200. prefer = []string{"zstd", "br", "gzip"}
  201. } else {
  202. encodings = caddy.ModuleMap{
  203. "zstd": caddyconfig.JSON(zstd.New(), nil),
  204. "gzip": caddyconfig.JSON(gzip.New(), nil),
  205. }
  206. prefer = []string{"zstd", "gzip"}
  207. }
  208. encodeRoute := caddyhttp.Route{
  209. MatcherSetsRaw: []caddy.ModuleMap{},
  210. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(encode.Encode{
  211. EncodingsRaw: encodings,
  212. Prefer: prefer,
  213. }, "handler", "encode", nil)},
  214. }
  215. subroute.Routes = append(caddyhttp.RouteList{encodeRoute}, subroute.Routes...)
  216. }
  217. if mercure {
  218. mercurePublisherJwtKey := os.Getenv("MERCURE_PUBLISHER_JWT_KEY")
  219. if mercurePublisherJwtKey == "" {
  220. panic(`The "MERCURE_PUBLISHER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
  221. }
  222. mercureSubscriberJwtKey := os.Getenv("MERCURE_SUBSCRIBER_JWT_KEY")
  223. if mercureSubscriberJwtKey == "" {
  224. panic(`The "MERCURE_SUBSCRIBER_JWT_KEY" environment variable must be set to use the Mercure.rocks hub`)
  225. }
  226. mercureRoute := caddyhttp.Route{
  227. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(
  228. mercureModule.Mercure{
  229. PublisherJWT: mercureModule.JWTConfig{
  230. Alg: os.Getenv("MERCURE_PUBLISHER_JWT_ALG"),
  231. Key: mercurePublisherJwtKey,
  232. },
  233. SubscriberJWT: mercureModule.JWTConfig{
  234. Alg: os.Getenv("MERCURE_SUBSCRIBER_JWT_ALG"),
  235. Key: mercureSubscriberJwtKey,
  236. },
  237. },
  238. "handler",
  239. "mercure",
  240. nil,
  241. ),
  242. },
  243. }
  244. subroute.Routes = append(caddyhttp.RouteList{mercureRoute}, subroute.Routes...)
  245. }
  246. route := caddyhttp.Route{
  247. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(subroute, "handler", "subroute", nil)},
  248. }
  249. if domain != "" {
  250. route.MatcherSetsRaw = []caddy.ModuleMap{
  251. {
  252. "host": caddyconfig.JSON(caddyhttp.MatchHost{domain}, nil),
  253. },
  254. }
  255. }
  256. server := &caddyhttp.Server{
  257. ReadHeaderTimeout: caddy.Duration(10 * time.Second),
  258. IdleTimeout: caddy.Duration(30 * time.Second),
  259. MaxHeaderBytes: 1024 * 10,
  260. Routes: caddyhttp.RouteList{route},
  261. }
  262. if listen == "" {
  263. if domain == "" {
  264. listen = ":80"
  265. } else {
  266. listen = ":" + strconv.Itoa(certmagic.HTTPSPort)
  267. }
  268. }
  269. server.Listen = []string{listen}
  270. if accessLog {
  271. server.Logs = &caddyhttp.ServerLogConfig{}
  272. }
  273. httpApp := caddyhttp.App{
  274. Servers: map[string]*caddyhttp.Server{"php": server},
  275. }
  276. var f bool
  277. cfg := &caddy.Config{
  278. Admin: &caddy.AdminConfig{
  279. Disabled: true,
  280. Config: &caddy.ConfigSettings{
  281. Persist: &f,
  282. },
  283. },
  284. AppsRaw: caddy.ModuleMap{
  285. "http": caddyconfig.JSON(httpApp, nil),
  286. "frankenphp": caddyconfig.JSON(FrankenPHPApp{Workers: workersOption}, nil),
  287. },
  288. }
  289. if debug {
  290. cfg.Logging = &caddy.Logging{
  291. Logs: map[string]*caddy.CustomLog{
  292. "default": {
  293. BaseLog: caddy.BaseLog{Level: zapcore.DebugLevel.CapitalString()},
  294. },
  295. },
  296. }
  297. }
  298. err = caddy.Run(cfg)
  299. if err != nil {
  300. return caddy.ExitCodeFailedStartup, err
  301. }
  302. log.Printf("Caddy serving PHP app on %s", listen)
  303. select {}
  304. }