php-server.go 7.7 KB

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