-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdocker-entrypoint.sh
executable file
·243 lines (208 loc) · 7.27 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
#
# Docker Entrypoint for PHP-FPM, Redis, and Monitoring Configuration
set -euo pipefail
# Globals and Default Values
: "${PHP_MAX_EXECUTION_TIME:=300}"
: "${PHP_FPM_CONF_DIR:=/usr/local/etc}"
: "${PHP_MEMORY_LIMIT:=128}" # Changed default to 128M
: "${PHP_POST_MAX_SIZE:=50}"
: "${PHP_UPLOAD_MAX_FILESIZE:=50}"
: "${PHP_OPCACHE_ENABLE:=1}"
: "${PHP_FPM_UPSTREAM_PORT:=9000}"
: "${REDIS_UPSTREAM_HOST:=redis}"
: "${REDIS_UPSTREAM_PORT:=6379}"
: "${LOG_PREFIX:=/var/log/php-fpm}"
: "${DEBUG:=0}"
# Debug Mode
if [[ "$DEBUG" == "1" ]]; then
set -x
echo "DEBUG: Loaded environment variables:"
env | grep -E 'PHP_|APP_|CACHE_|REDIS_'
fi
# Function: Calculate optimal PHP-FPM settings based on available resources
calculate_fpm_settings() {
# Get total RAM and CPU cores
TOTAL_RAM_MB=$(grep MemTotal /proc/meminfo | awk '{print $2 / 1024}')
CPU_CORES=$(nproc)
echo "INFO: Total RAM: ${TOTAL_RAM_MB}MB"
echo "INFO: CPU cores: ${CPU_CORES}"
# Calculate available RAM for PHP processes (30% of total RAM as per your original script)
PHP_AVAILABLE_RAM_MB=$(awk "BEGIN {print $TOTAL_RAM_MB * 0.30}")
# Use memory limit from environment or calculate based on available RAM
if [ -z "${PHP_MEMORY_LIMIT-}" ]; then
# Calculate memory limit as 20% of PHP available RAM
PHP_MEMORY_LIMIT=$(awk "BEGIN {printf \"%.0f\", $PHP_AVAILABLE_RAM_MB * 0.20}")
# Ensure memory limit is between 128MB and 1024MB
if [ "$(awk "BEGIN {print ($PHP_MEMORY_LIMIT < 128)}")" -eq 1 ]; then
PHP_MEMORY_LIMIT=128
elif [ "$(awk "BEGIN {print ($PHP_MEMORY_LIMIT > 1024)}")" -eq 1 ]; then
PHP_MEMORY_LIMIT=1024
fi
fi
# Calculate max children based on available RAM and memory limit
PHP_MAX_CHILDREN=$(awk "BEGIN {print int($PHP_AVAILABLE_RAM_MB / $PHP_MEMORY_LIMIT)}")
# Ensure minimum of 2 children even on very constrained systems
if [ "$PHP_MAX_CHILDREN" -lt 2 ]; then
PHP_MAX_CHILDREN=2
fi
# Calculate other FPM settings with safe minimums
START_SERVERS=$(( (PHP_MAX_CHILDREN + 1) / 2 ))
if [ "$START_SERVERS" -lt 2 ]; then
START_SERVERS=2
fi
MIN_SPARE_SERVERS=$(( START_SERVERS - 1 ))
if [ "$MIN_SPARE_SERVERS" -lt 1 ]; then
MIN_SPARE_SERVERS=1
fi
MAX_SPARE_SERVERS=$((PHP_MAX_CHILDREN - 1))
if [ "$MAX_SPARE_SERVERS" -le "$MIN_SPARE_SERVERS" ]; then
MAX_SPARE_SERVERS=$((MIN_SPARE_SERVERS + 1))
fi
# Calculate max requests before worker restart
MAX_REQUESTS=$((PHP_MAX_CHILDREN * 100))
if [ "$MAX_REQUESTS" -gt 1000 ]; then
MAX_REQUESTS=1000
fi
# Calculate OPcache memory (5% of PHP available RAM)
OPCACHE_MEMORY_MB=$(awk "BEGIN {print int($PHP_AVAILABLE_RAM_MB * 0.05)}")
if [ "$OPCACHE_MEMORY_MB" -lt 64 ]; then
OPCACHE_MEMORY_MB=64
elif [ "$OPCACHE_MEMORY_MB" -gt 512 ]; then
OPCACHE_MEMORY_MB=512
fi
echo "INFO: Configured settings:"
echo "- Memory limit: ${PHP_MEMORY_LIMIT}MB"
echo "- PHP max children: $PHP_MAX_CHILDREN"
echo "- Start servers: $START_SERVERS"
echo "- Min spare servers: $MIN_SPARE_SERVERS"
echo "- Max spare servers: $MAX_SPARE_SERVERS"
echo "- Max requests: $MAX_REQUESTS"
echo "- OPcache memory: ${OPCACHE_MEMORY_MB}MB"
}
find /usr/local/etc/ -maxdepth 3 -type f -exec sed -i -e 's|{{PHP_POST_MAX_SIZE}}|'"${PHP_POST_MAX_SIZE}"'|g' {} +
# Function: Configure PHP-FPM
php_fpm() {
echo "INFO: Configuring PHP-FPM..."
calculate_fpm_settings
mkdir -p "${APP_DOCROOT}" "${LOG_PREFIX}" "${CACHE_PREFIX}/fastcgi"
# Create PHP-FPM Configuration Files
cat <<EOF > "${PHP_FPM_CONF_DIR}/php-fpm.conf"
[global]
include=${PHP_FPM_CONF_DIR}/php-fpm.d/*.conf
EOF
cat <<EOF > "${PHP_FPM_CONF_DIR}/php-fpm.d/docker.conf"
[global]
error_log = ${LOG_PREFIX}/error.log
log_level = error
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s
[www]
access.log = ${LOG_PREFIX}/access.log
clear_env = no
catch_workers_output = yes
request_terminate_timeout = ${PHP_MAX_EXECUTION_TIME}s
EOF
cat <<EOF > "${PHP_FPM_CONF_DIR}/php-fpm.d/zz-docker.conf"
[global]
daemonize = no
[www]
user = www-data
group = www-data
listen = [::]:${PHP_FPM_UPSTREAM_PORT}
listen.mode = 0660
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = ${PHP_MAX_CHILDREN}
pm.start_servers = ${START_SERVERS}
pm.min_spare_servers = ${MIN_SPARE_SERVERS}
pm.max_spare_servers = ${MAX_SPARE_SERVERS}
pm.max_requests = ${MAX_REQUESTS}
pm.status_path = /status
EOF
cat <<EOF > "${PHP_FPM_CONF_DIR}/php/conf.d/50-setting.ini"
max_execution_time=${PHP_MAX_EXECUTION_TIME}
memory_limit=${PHP_MEMORY_LIMIT}M
upload_max_filesize=${PHP_UPLOAD_MAX_FILESIZE}M
post_max_size=${PHP_POST_MAX_SIZE}M
file_uploads=On
max_input_vars=${PHP_MAX_INPUT_VARS:-10000}
error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors=Off
display_startup_errors=Off
log_errors=On
error_log=${LOG_PREFIX}/php_errors.log
user_ini.filename=
realpath_cache_size=4M
realpath_cache_ttl=120
date.timezone=UTC
short_open_tag=Off
session.auto_start=Off
; OpCache settings
opcache.enable=${PHP_OPCACHE_ENABLE}
opcache.memory_consumption=${OPCACHE_MEMORY_MB}
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=2
opcache.enable_cli=0
opcache.save_comments=1
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1
opcache.use_cwd=1
opcache.max_wasted_percentage=5
opcache.consistency_checks=0
opcache.huge_code_pages=1
opcache.file_cache="${CACHE_PREFIX}/fastcgi/.opcache"
opcache.file_cache_only=1
opcache.file_cache_consistency_checks=1
EOF
echo "INFO: PHP-FPM configured successfully."
}
# Function: Configure Redis
redis() {
if [[ -n "$REDIS_UPSTREAM_HOST" ]]; then
echo "INFO: Configuring Redis..."
cat <<EOF > "${PHP_FPM_CONF_DIR}/php/conf.d/zz-redis-setting.ini"
session.gc_maxlifetime=86400
session.save_handler=redis
session.save_path="tcp://$REDIS_UPSTREAM_HOST:$REDIS_UPSTREAM_PORT?weight=1&timeout=2.5&database=3"
EOF
echo "INFO: Redis configured successfully."
else
echo "INFO: Redis not configured. Either Redis is not installed or REDIS_UPSTREAM is not set."
fi
}
# Function: Install Plugins
install_plugin() {
if [[ ! -d /usr/src/plugins/${NGINX_APP_PLUGIN} ]]; then
echo "INFO: NGINX_APP_PLUGIN is not located in the plugin directory. Nothing to install..."
else
echo "OK: Installing NGINX_APP_PLUGIN=${NGINX_APP_PLUGIN}..."
sleep 10
chmod +x "/usr/src/plugins/${NGINX_APP_PLUGIN}/install.sh"
bash -x /usr/src/plugins/${NGINX_APP_PLUGIN}/install.sh
fi
}
# Function: Configure Permissions
set_permissions() {
echo "INFO: Setting ownership and permissions..."
# Set ownership for all files and directories
find "${APP_DOCROOT}" ! -user www-data -exec chown www-data:www-data {} +
find "${APP_DOCROOT}" -type d ! -perm 755 -exec chmod 755 {} +
find "${APP_DOCROOT}" -type f ! -perm 644 -exec chmod 644 {} +
echo "INFO: Ownership and permissions configured successfully."
}
# Function: Main Run
run() {
php_fpm
redis
install_plugin
set_permissions
echo "INFO: Entry script completed. Starting PHP-FPM..."
}
# Execute the main run function
run
# Replace the current process with the CMD passed in the Dockerfile
exec "$@"