Skip to content

Commit 836a66d

Browse files
committed
Support PHP-FPM overrides via environment variables
1 parent 644e757 commit 836a66d

File tree

6 files changed

+98
-15
lines changed

6 files changed

+98
-15
lines changed

cmd/php/supervisor/main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ func buildFn(ctx *gcp.Context) error {
7070
overrides := webconfig.OverriddenProperties(ctx, runtimeConfig)
7171
webconfig.SetEnvVariables(l, overrides)
7272

73+
err = overrides.UpdateFromEnvironment(ctx)
74+
if err != nil {
75+
return err
76+
}
77+
7378
fpmConfFile, err := writeFpmConfig(l.Path, overrides)
7479
if err != nil {
7580
return err
@@ -164,11 +169,17 @@ func nginxConfig(layer string, overrides webconfig.OverrideProperties) nginx.Con
164169
frontController = overrides.FrontController
165170
}
166171

172+
root := defaultRoot
173+
if overrides.DocumentRoot != "" {
174+
root = filepath.Join(defaultRoot, overrides.DocumentRoot)
175+
}
176+
167177
nginx := nginx.Config{
168178
Port: defaultNginxPort,
169179
FrontControllerScript: frontController,
170-
Root: filepath.Join(defaultRoot, overrides.DocumentRoot),
180+
Root: root,
171181
AppListenAddress: defaultAddress,
182+
ServesStaticFiles: overrides.NginxServesStaticFiles,
172183
}
173184

174185
if overrides.NginxServerConfInclude {
@@ -205,6 +216,14 @@ func fpmConfig(layer string, overrides webconfig.OverrideProperties) (nginx.FPMC
205216
AddNoDecorateWorkers: true,
206217
}
207218

219+
if overrides.PHPFPMDynamicWorkers {
220+
fpm.DynamicWorkers = overrides.PHPFPMDynamicWorkers
221+
}
222+
223+
if overrides.PHPFPMWorkers > 0 {
224+
fpm.NumWorkers = overrides.PHPFPMWorkers
225+
}
226+
208227
if overrides.PHPFPMOverride {
209228
fpm.ConfOverride = overrides.PHPFPMOverrideFileName
210229
}

cmd/php/webconfig/main.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,10 @@ func buildFn(ctx *gcp.Context) error {
8282
webconfig.SetEnvVariables(l, overrides)
8383
}
8484

85-
if customNginxConf, present := os.LookupEnv(php.CustomNginxConfig); present {
86-
overrides.NginxConfOverride = true
87-
overrides.NginxConfOverrideFileName = filepath.Join(defaultRoot, customNginxConf)
88-
}
89-
90-
nginxServesStaticFiles, err := env.IsPresentAndTrue(php.NginxServesStaticFiles)
85+
err = overrides.UpdateFromEnvironment(ctx)
9186
if err != nil {
9287
return err
9388
}
94-
overrides.NginxServesStaticFiles = nginxServesStaticFiles
95-
96-
nginxDocumentRoot := os.Getenv(php.NginxDocumentRoot)
97-
if (nginxDocumentRoot != "") {
98-
overrides.DocumentRoot = nginxDocumentRoot
99-
}
10089

10190
fpmConfFile, err := writeFpmConfig(ctx, l.Path, overrides)
10291
if err != nil {
@@ -211,6 +200,14 @@ func fpmConfig(layer string, addNoDecorateWorkers bool, overrides webconfig.Over
211200
fpm.ListenAddress = defaultFlexAddress
212201
}
213202

203+
if overrides.PHPFPMDynamicWorkers {
204+
fpm.DynamicWorkers = overrides.PHPFPMDynamicWorkers
205+
}
206+
207+
if overrides.PHPFPMWorkers > 0 {
208+
fpm.NumWorkers = overrides.PHPFPMWorkers
209+
}
210+
214211
if overrides.PHPFPMOverride {
215212
fpm.ConfOverride = overrides.PHPFPMOverrideFileName
216213
}

pkg/env/env.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,18 @@ func IsPresentAndTrue(varName string) (bool, error) {
194194

195195
return parsed, nil
196196
}
197+
198+
// IsPresentAndInt returns true and the integer value if the environment variable evaluates exists and contains an integer.
199+
func IsPresentAndInt(varName string) (bool, int, error) {
200+
varValue, present := os.LookupEnv(varName)
201+
if !present {
202+
return false, 0, nil
203+
}
204+
205+
parsed, err := strconv.Atoi(varValue)
206+
if err != nil {
207+
return false, 0, fmt.Errorf("parsing %s: %v", varName, err)
208+
}
209+
210+
return true, parsed, nil
211+
}

pkg/php/php.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,17 @@ post_max_size = 32M
9191
// CustomNginxConfig is an environment variable to pass a custom nginx configuration.
9292
CustomNginxConfig = "GOOGLE_CUSTOM_NGINX_CONFIG"
9393

94-
// NginxServesStaticFiles is an environment variable to configure Nginx to serve static files.
94+
// NginxServesStaticFiles is an environment variable to configure nginx to serve static files.
9595
NginxServesStaticFiles = "NGINX_SERVES_STATIC_FILES"
9696

97-
// NginxDocumentRoot overrides the document root of nginx.
97+
// NginxDocumentRoot is an environment variable to configure the document root of nginx.
9898
NginxDocumentRoot = "NGINX_DOCUMENT_ROOT"
99+
100+
// PHPFPMDynamicWorkers is an environment variable to enable dynamic workers for php-fpm.
101+
PHPFPMDynamicWorkers = "PHP_FPM_DYNAMIC_WORKERS"
102+
103+
// PHPFPMWorkerCount is an environment variable to configure the number of php-fpm workers.
104+
PHPFPMWorkerCount = "PHP_FPM_WORKER_COUNT"
99105
)
100106

101107
type composerScriptsJSON struct {

pkg/webconfig/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
],
1010
deps = [
1111
"//pkg/appyaml",
12+
"//pkg/env",
1213
"//pkg/gcpbuildpack",
1314
"//pkg/php",
1415
"@com_github_buildpacks_libcnb//:go_default_library",

pkg/webconfig/webconfig.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
package webconfig
33

44
import (
5+
"fmt"
6+
"os"
57
"path/filepath"
68

79
"github.com/GoogleCloudPlatform/buildpacks/pkg/appyaml"
10+
"github.com/GoogleCloudPlatform/buildpacks/pkg/env"
811
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
912
"github.com/GoogleCloudPlatform/buildpacks/pkg/php"
1013
"github.com/buildpacks/libcnb"
@@ -44,6 +47,10 @@ type OverrideProperties struct {
4447
NginxHTTPInclude bool
4548
// NginxHTTPIncludeFileName name of the partial nginx config to be included in the http section.
4649
NginxHTTPIncludeFileName string
50+
// PHPFPMDynamicWorkers boolean to toggle dynamic workers in the php-fpm config file.
51+
PHPFPMDynamicWorkers bool
52+
// PHPFPMWorkers integer to specify the worker thread count in the php-fpm config file.
53+
PHPFPMWorkers int
4754
// PHPFPMOverride boolean to check if user-provided php-fpm config exists.
4855
PHPFPMOverride bool
4956
// PHPFPMOverrideFileName name of the user-provided php-fpm config file.
@@ -56,6 +63,44 @@ type OverrideProperties struct {
5663
NginxServesStaticFiles bool
5764
}
5865

66+
func (overrides *OverrideProperties) UpdateFromEnvironment(ctx *gcp.Context) error {
67+
if customNginxConf, present := os.LookupEnv(php.CustomNginxConfig); present {
68+
overrides.NginxConfOverride = true
69+
overrides.NginxConfOverrideFileName = filepath.Join(defaultRoot, customNginxConf)
70+
}
71+
72+
nginxServesStaticFiles, err := env.IsPresentAndTrue(php.NginxServesStaticFiles)
73+
if err != nil {
74+
return err
75+
}
76+
overrides.NginxServesStaticFiles = nginxServesStaticFiles
77+
78+
nginxDocumentRoot := os.Getenv(php.NginxDocumentRoot)
79+
if nginxDocumentRoot != "" {
80+
overrides.DocumentRoot = nginxDocumentRoot
81+
}
82+
83+
phpFPMDynamicWorkers, err := env.IsPresentAndTrue(php.PHPFPMDynamicWorkers)
84+
if err != nil {
85+
return err
86+
}
87+
overrides.PHPFPMDynamicWorkers = phpFPMDynamicWorkers
88+
89+
phpFPMWorkerCountPresent, phpFPMWorkerCount, err := env.IsPresentAndInt(php.PHPFPMWorkerCount)
90+
if err != nil {
91+
return err
92+
}
93+
if phpFPMWorkerCountPresent {
94+
if phpFPMWorkerCount > 0 {
95+
overrides.PHPFPMWorkers = phpFPMWorkerCount
96+
} else {
97+
return fmt.Errorf("invalid %s value, must be greater than 0", php.PHPFPMWorkerCount)
98+
}
99+
}
100+
101+
return nil
102+
}
103+
59104
// OverriddenProperties returns whether the property has been overridden and the path to the file.
60105
func OverriddenProperties(ctx *gcp.Context, runtimeConfig appyaml.RuntimeConfig) OverrideProperties {
61106
phpIniOverride, phpIniOverrideFileName := overrideProperties(ctx, runtimeConfig.PHPIniOverride, defaultPHPIni)

0 commit comments

Comments
 (0)