-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathphp-config-update.sh
More file actions
143 lines (129 loc) · 7.05 KB
/
php-config-update.sh
File metadata and controls
143 lines (129 loc) · 7.05 KB
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
#!/usr/bin/env bash
#----------------------------------------------------------------------------------
# EngineScript - A High-Performance WordPress Server Built on Ubuntu and Cloudflare
#----------------------------------------------------------------------------------
# Website: https://EngineScript.com
# GitHub: https://github.com/Enginescript/EngineScript
# License: GPL v3.0
#----------------------------------------------------------------------------------
# EngineScript Variables
source /usr/local/bin/enginescript/enginescript-variables.txt || { echo "Error: Failed to source /usr/local/bin/enginescript/enginescript-variables.txt" >&2; exit 1; }
source /home/EngineScript/enginescript-install-options.txt || { echo "Error: Failed to source /home/EngineScript/enginescript-install-options.txt" >&2; exit 1; }
# Source shared functions library
source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.sh || { echo "Error: Failed to source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-common.sh" >&2; exit 1; }
# Validate required capacity signals before tuning.
require_basic_system_resources "MEMORY_TOTAL_MB" "CPU_COUNT"
#----------------------------------------------------------------------------------
# Start Main Script
# Calculate PHP FPM tune depending on RAM
calculate_php() {
# No fallbacks needed as this script will be run on a system with a modern kernel.
# MemAvailable field will be present in /proc/meminfo on modern kernels and is the most accurate representation of available memory for applications.
AVAILABLE_MEMORY=$(awk '/MemAvailable/ {printf "%d", $2/1024}' /proc/meminfo)
# Validate that AVAILABLE_MEMORY is a non-empty numeric value; exit if it cannot be determined.
if ! [[ "${AVAILABLE_MEMORY}" =~ ^[0-9]+$ ]]; then
# Fallback: use MemTotal if available, otherwise exit with an error.
AVAILABLE_MEMORY=$(awk '/MemTotal/ {printf "%d", $2/1024}' /proc/meminfo 2>/dev/null || echo "")
if ! [[ "${AVAILABLE_MEMORY}" =~ ^[0-9]+$ ]]; then
echo "Error: Unable to determine available memory from /proc/meminfo; cannot continue." >&2
exit 1
fi
fi
AVERAGE_PHP_MEMORY_REQ_MB=80 # average PHP memory requirement in MB
# Memory thresholds in MB for tuning PHP-FPM and PHP settings
MEMORY_THRESHOLD_LOW=1200
MEMORY_THRESHOLD_MEDIUM=2200
MEMORY_THRESHOLD_HIGH=4200
# PHP-FPM process limits are tuned using available memory and average per-process usage.
# Dynamically calculate pm.start_servers, pm.min_spare_servers, and pm.max_spare_servers based on CPU threads
if [[ "${CPU_COUNT}" -eq 1 ]]; then
PHP_FPM_START_SERVERS=2
PHP_FPM_MIN_SPARE_SERVERS=1
PHP_FPM_MAX_SPARE_SERVERS=3
elif [[ "${CPU_COUNT}" -eq 2 ]]; then
PHP_FPM_START_SERVERS=3
PHP_FPM_MIN_SPARE_SERVERS=2
PHP_FPM_MAX_SPARE_SERVERS=5
elif [[ "${CPU_COUNT}" -eq 3 ]]; then
PHP_FPM_START_SERVERS=3
PHP_FPM_MIN_SPARE_SERVERS=3
PHP_FPM_MAX_SPARE_SERVERS=6
elif [[ "${CPU_COUNT}" -eq 4 ]]; then
PHP_FPM_START_SERVERS=4
PHP_FPM_MIN_SPARE_SERVERS=3
PHP_FPM_MAX_SPARE_SERVERS=6
elif [[ "${CPU_COUNT}" -le 8 ]]; then
PHP_FPM_START_SERVERS=5
PHP_FPM_MIN_SPARE_SERVERS=4
PHP_FPM_MAX_SPARE_SERVERS=7
elif [[ "${CPU_COUNT}" -le 16 ]]; then
PHP_FPM_START_SERVERS=8
PHP_FPM_MIN_SPARE_SERVERS=6
PHP_FPM_MAX_SPARE_SERVERS=14
else
PHP_FPM_START_SERVERS=5
PHP_FPM_MIN_SPARE_SERVERS=4
PHP_FPM_MAX_SPARE_SERVERS=7
fi
# Calculate server configuration based on available memory
if [[ "${AVAILABLE_MEMORY}" -lt "${MEMORY_THRESHOLD_LOW}" ]]; then
PHP_FPM_MAX_CHILDREN=8
PHP_MEMORY_LIMIT="256M"
OPCACHE_JIT_BUFFER="64M"
OPCACHE_INT_BUFFER=16
elif [[ "${AVAILABLE_MEMORY}" -lt "${MEMORY_THRESHOLD_MEDIUM}" ]]; then
PHP_FPM_MAX_CHILDREN=16
PHP_MEMORY_LIMIT="256M"
OPCACHE_JIT_BUFFER="64M"
OPCACHE_INT_BUFFER=32
elif [[ "${AVAILABLE_MEMORY}" -lt "${MEMORY_THRESHOLD_HIGH}" ]]; then
PHP_FPM_MAX_CHILDREN=24
PHP_MEMORY_LIMIT="512M"
OPCACHE_JIT_BUFFER="96M"
OPCACHE_INT_BUFFER=64
else
PHP_FPM_MAX_CHILDREN=48
PHP_MEMORY_LIMIT="512M"
OPCACHE_JIT_BUFFER="128M"
OPCACHE_INT_BUFFER=64
fi
# Apply calculated values to PHP-FPM configuration
sed -i "s|pm.start_servers = .*|pm.start_servers = ${PHP_FPM_START_SERVERS}|g" "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" 2>> /tmp/enginescript_install_errors.log
sed -i "s|pm.min_spare_servers = .*|pm.min_spare_servers = ${PHP_FPM_MIN_SPARE_SERVERS}|g" "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" 2>> /tmp/enginescript_install_errors.log
sed -i "s|pm.max_spare_servers = .*|pm.max_spare_servers = ${PHP_FPM_MAX_SPARE_SERVERS}|g" "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" 2>> /tmp/enginescript_install_errors.log
sed -i "s|pm.max_children = .*|pm.max_children = ${PHP_FPM_MAX_CHILDREN}|g" "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" 2>> /tmp/enginescript_install_errors.log
# Apply memory and OpCache settings to php.ini
sed -i "s|SEDPHPMEMLIMIT|${PHP_MEMORY_LIMIT}|g" "/etc/php/${PHP_VER}/fpm/php.ini" 2>> /tmp/enginescript_install_errors.log
sed -i "s|SEDOPCACHEINTBUF|${OPCACHE_INT_BUFFER}|g" "/etc/php/${PHP_VER}/fpm/php.ini" 2>> /tmp/enginescript_install_errors.log
# Arithmetic expansion result is unlikely to need quoting; template controls any surrounding quotes
sed -i "s|SEDOPCACHEMEM|$((AVAILABLE_MEMORY / 8))M|g" "/etc/php/${PHP_VER}/fpm/php.ini" 2>> /tmp/enginescript_install_errors.log
}
# Update PHP config
if [ -f "/etc/php/${PHP_VER}/fpm/php.ini" ]; then
cp -p "/etc/php/${PHP_VER}/fpm/php.ini" "/etc/php/${PHP_VER}/fpm/php.ini.bak" 2>> /tmp/enginescript_install_errors.log
fi
if ! cp -f /usr/local/bin/enginescript/config/etc/php/php.ini "/etc/php/${PHP_VER}/fpm/php.ini" 2>> /tmp/enginescript_install_errors.log; then
echo "Error: Failed to copy php.ini for PHP ${PHP_VER}" >&2
exit 1
fi
sed -i "s|SEDPHPVER|\"${PHP_VER}\"|g" "/etc/php/${PHP_VER}/fpm/php.ini" 2>> /tmp/enginescript_install_errors.log
if [ -f "/etc/php/${PHP_VER}/fpm/php-fpm.conf" ]; then
cp -p "/etc/php/${PHP_VER}/fpm/php-fpm.conf" "/etc/php/${PHP_VER}/fpm/php-fpm.conf.bak" 2>> /tmp/enginescript_install_errors.log
fi
if ! cp -f /usr/local/bin/enginescript/config/etc/php/php-fpm.conf "/etc/php/${PHP_VER}/fpm/php-fpm.conf" 2>> /tmp/enginescript_install_errors.log; then
echo "Error: Failed to copy php-fpm.conf for PHP ${PHP_VER}" >&2
exit 1
fi
sed -i "s|SEDPHPVER|\"${PHP_VER}\"|g" "/etc/php/${PHP_VER}/fpm/php-fpm.conf" 2>> /tmp/enginescript_install_errors.log
if [ -f "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" ]; then
cp -p "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" "/etc/php/${PHP_VER}/fpm/pool.d/www.conf.bak" 2>> /tmp/enginescript_install_errors.log
fi
if ! cp -f /usr/local/bin/enginescript/config/etc/php/www.conf "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" 2>> /tmp/enginescript_install_errors.log; then
echo "Error: Failed to copy www.conf for PHP ${PHP_VER}" >&2
exit 1
fi
sed -i "s|SEDPHPVER|${PHP_VER}|g" "/etc/php/${PHP_VER}/fpm/pool.d/www.conf" 2>> /tmp/enginescript_install_errors.log
# Tune PHP Configuration
calculate_php
print_last_errors
debug_pause "PHP Configuration Update"