Skip to content

Commit 3b448e5

Browse files
committed
added the support for --user and --group options and automatically instructs fpm to run as root when if needed
1 parent f318379 commit 3b448e5

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

cmd/serve/flags.go

+14
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,19 @@ func DefaultFlags(envPrefix string) []cli.Flag {
136136
EnvVars: []string{prefixWrapper("CORS_AGE")},
137137
Value: 0,
138138
},
139+
140+
&cli.StringFlag{
141+
Name: "user",
142+
Usage: "run the fpm www pool user",
143+
EnvVars: []string{prefixWrapper("USER")},
144+
Value: "www-data",
145+
},
146+
147+
&cli.StringFlag{
148+
Name: "group",
149+
Usage: "run the fpm www pool group",
150+
EnvVars: []string{prefixWrapper("GROUP")},
151+
Value: "www-data",
152+
},
139153
}
140154
}

cmd/serve/serve.go

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func Before() cli.BeforeFunc {
6464
WorkerCount: ctx.Int("workers"),
6565
WorkerMaxRequestCount: ctx.Int("requests"),
6666
WorkerMaxRequestTime: ctx.Int("timeout"),
67+
User: ctx.String("user"),
68+
Group: ctx.String("group"),
6769
}
6870

6971
return fpmProcess.Start()

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/alash3al/phoo
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/joho/godotenv v1.5.1

internals/fpm/fpm.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type Process struct {
2525
WorkerCount int
2626
WorkerMaxRequestCount int
2727
WorkerMaxRequestTime int
28+
29+
User string
30+
Group string
2831
}
2932

3033
func (p *Process) Start() error {
@@ -49,6 +52,8 @@ func (p *Process) Start() error {
4952
"worker.count": fmt.Sprintf("%v", p.WorkerCount),
5053
"worker.request.max_count": fmt.Sprintf("%v", p.WorkerMaxRequestCount),
5154
"worker.request.max_time": fmt.Sprintf("%v", p.WorkerMaxRequestTime),
55+
"user": p.User,
56+
"group": p.Group,
5257
})
5358

5459
if err := os.WriteFile(p.ConfigFilename, []byte(fpmConfigFileContents), 0755); err != nil {
@@ -59,7 +64,12 @@ func (p *Process) Start() error {
5964
}
6065

6166
func (p *Process) execAndWait() error {
62-
cmd := exec.Command(p.BinFilename, "-F", "-O", "-y", p.ConfigFilename)
67+
args := []string{"-F", "-O", "-y", p.ConfigFilename}
68+
if p.User == "root" || p.Group == "root" {
69+
args = append(args, "-R")
70+
}
71+
72+
cmd := exec.Command(p.BinFilename, args...)
6373
cmd.Stdout = os.Stdout
6474
cmd.Stderr = os.Stderr
6575

internals/fpm/php-fpm.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ pm.max_children = {{worker.count}}
1212
pm.max_requests = {{worker.request.max_count}}
1313
request_terminate_timeout = {{worker.request.max_time}}
1414
clear_env = no
15-
user = www-data
16-
group = www-data
15+
user = {{user}}
16+
group = {{group}}

0 commit comments

Comments
 (0)