-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathphp-update.sh
More file actions
274 lines (228 loc) · 9.95 KB
/
Copy pathphp-update.sh
File metadata and controls
274 lines (228 loc) · 9.95 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/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; }
#----------------------------------------------------------------------------------
# Start Main Script
# Prompt for EngineScript Update
if prompt_yes_no "Do you want to update EngineScript before continuing?\nThis will ensure you have the latest core scripts and variables."; then
/usr/local/bin/enginescript/scripts/update/enginescript-update.sh 2>> /tmp/enginescript_install_errors.log
else
echo "Skipping EngineScript update."
fi
print_last_errors
debug_pause "EngineScript Update"
# Accept target version from argument (used by menu) or fall back to variables + override
if [[ -n "${1}" ]]; then
NEW_PHP_VER="${1}"
else
NEW_PHP_VER="${PHP_VER}"
fi
new_php_valid=false
for ver in "${SUPPORTED_PHP_VERSIONS[@]}"; do
if [[ "${NEW_PHP_VER}" == "${ver}" ]]; then
new_php_valid=true
break
fi
done
if [[ "${new_php_valid}" != true ]]; then
echo "Unsupported PHP version: ${NEW_PHP_VER}"
exit 1
fi
# Auto-detect currently installed PHP-FPM version
DPKG_LIST_OUTPUT="$(dpkg -l)"
OLD_PHP_VERS=()
for ver in "${SUPPORTED_PHP_VERSIONS[@]}"; do
if [[ "${ver}" != "${NEW_PHP_VER}" ]] && grep -q "php${ver}-fpm" <<< "${DPKG_LIST_OUTPUT}"; then
OLD_PHP_VERS+=(
"${ver}"
)
fi
done
if [[ ${#OLD_PHP_VERS[@]} -eq 0 ]]; then
# Check if target version is already installed
if dpkg-query -W -f='${Status}' "php${NEW_PHP_VER}-fpm" 2>/dev/null | grep -q 'install ok installed'; then
echo "PHP ${NEW_PHP_VER} is already installed. Nothing to upgrade."
exit 0
else
echo "No existing PHP-FPM installation detected. Use php-install.sh for fresh installs."
exit 1
fi
fi
# Keep backward-compatible single-version variable for legacy downstream logic.
# Migration logic must use MIGRATION_SOURCE_PHP_VERS to ensure all detected old versions are handled.
OLD_PHP_VER="${OLD_PHP_VERS[0]}"
MIGRATION_SOURCE_PHP_VERS=(
"${OLD_PHP_VERS[@]}"
)
echo ""
echo "============================================================="
echo ""
echo "PHP Upgrade: Migrating to PHP ${NEW_PHP_VER} from detected old version(s): ${MIGRATION_SOURCE_PHP_VERS[*]}"
echo ""
echo "============================================================="
echo ""
echo "Detected PHP installation(s): ${MIGRATION_SOURCE_PHP_VERS[*]}"
echo "Proceeding with upgrade to PHP ${NEW_PHP_VER}..."
# Stop old PHP service(s)
for old_ver in "${MIGRATION_SOURCE_PHP_VERS[@]}"; do
echo "Stopping PHP ${old_ver} service..."
systemctl stop "php${old_ver}-fpm" 2>/dev/null || true
done
# Install new PHP version
echo "Installing PHP ${NEW_PHP_VER}..."
# Package blocking pins every non-selected PHP version during install. Remove
# the target pin before switching so apt can install the requested PHP version.
rm -f "/etc/apt/preferences.d/php${NEW_PHP_VER//./}-block"
# Define the PHP packages to install
mapfile -t php_packages < <(get_php_packages_array "${NEW_PHP_VER}")
# Install the packages with error checking
apt install -qy "${php_packages[@]}" 2>> /tmp/enginescript_install_errors.log || {
echo "Error: Unable to install PHP ${NEW_PHP_VER} packages. Exiting..."
exit 1
}
# Install expanded PHP packages if enabled
if [[ "$INSTALL_EXPANDED_PHP" == "1" ]]; then
echo "Installing expanded PHP ${NEW_PHP_VER} packages..."
mapfile -t expanded_php_packages < <(get_expanded_php_packages_array "${NEW_PHP_VER}")
apt install -qy "${expanded_php_packages[@]}" 2>> /tmp/enginescript_install_errors.log || {
echo "Error: Unable to install expanded PHP ${NEW_PHP_VER} packages. Exiting..."
exit 1
}
fi
print_last_errors
debug_pause "PHP Package Installation"
# Logrotate
if [[ -f "/etc/logrotate.d/php${NEW_PHP_VER}-fpm" ]]; then
sed -i "s|rotate 12|rotate 5|g" "/etc/logrotate.d/php${NEW_PHP_VER}-fpm"
fi
# Backup PHP config
/usr/local/bin/enginescript/scripts/functions/backup/php-backup.sh 2>> /tmp/enginescript_install_errors.log
# Update PHP config with the latest EngineScript settings
/usr/local/bin/enginescript/scripts/update/php-config-update.sh "${NEW_PHP_VER}" 2>> /tmp/enginescript_install_errors.log
print_last_errors
debug_pause "PHP Configuration"
# Create necessary directories and log files
mkdir -p /var/cache/opcache
mkdir -p /var/cache/php-sessions
mkdir -p /var/cache/wsdlcache
mkdir -p /var/log/opcache
mkdir -p /var/log/php
touch "/var/log/opcache/opcache.log"
touch "/var/log/php/php${NEW_PHP_VER}-fpm.log"
# Assign PHP Permissions
set_php_permissions
# Update Nginx configuration to use new PHP version
echo "Updating Nginx configuration for PHP ${NEW_PHP_VER}..."
# Update php-fpm.conf
if [[ -f "/etc/nginx/globals/php-fpm.conf" ]]; then
for OLD_VER in "${MIGRATION_SOURCE_PHP_VERS[@]}"; do
sed -E -i \
-e "s|(unix:/run/php/)php${OLD_VER//./\.}-fpm(\.sock)|\1php${NEW_PHP_VER}-fpm\2|g" \
-e "s|(fastcgi_pass[[:space:]]+[^;]*php)${OLD_VER//./\.}(-fpm)|\1${NEW_PHP_VER}\2|g" \
"/etc/nginx/globals/php-fpm.conf"
done
fi
# Update all nginx site configurations
for config_file in /etc/nginx/sites-available/*; do
if [[ -f "$config_file" ]]; then
for OLD_VER in "${MIGRATION_SOURCE_PHP_VERS[@]}"; do
sed -E -i -e "s|(fastcgi_pass[[:space:]]+[^;]*php)${OLD_VER//./\.}(-fpm)|\1${NEW_PHP_VER}\2|g" "$config_file"
done
fi
done
# Update phpSysInfo configuration
if [[ -f "/var/www/admin/tools/phpsysinfo/phpsysinfo.ini" ]]; then
echo "Updating phpSysInfo configuration..."
for OLD_VER in "${MIGRATION_SOURCE_PHP_VERS[@]}"; do
sed -E -i "s|^([[:space:]]*[[:alnum:]_.-]+[[:space:]]*=[[:space:]]*.*)php${OLD_VER//./\.}(-fpm)?|\1php${NEW_PHP_VER}\2|g" "/var/www/admin/tools/phpsysinfo/phpsysinfo.ini"
done
fi
# Update admin control panel API configuration
if [[ -f "/var/www/admin/control-panel/api.php" ]]; then
echo "Updating admin control panel API configuration..."
for OLD_VER in "${MIGRATION_SOURCE_PHP_VERS[@]}"; do
sed -E -i "/(php-fpm|fastcgi_pass|sock(et)?|service)/ s|(php)${OLD_VER//./\.}(-fpm)?|\1${NEW_PHP_VER}\2|g" "/var/www/admin/control-panel/api.php"
done
fi
# Start new PHP service
echo "Starting PHP ${NEW_PHP_VER} service..."
systemctl enable "php${NEW_PHP_VER}-fpm" 2>> /tmp/enginescript_install_errors.log
systemctl start "php${NEW_PHP_VER}-fpm" 2>> /tmp/enginescript_install_errors.log
# Reload Nginx to pick up the new PHP configuration
echo "Reloading Nginx configuration..."
systemctl reload nginx 2>> /tmp/enginescript_install_errors.log
print_last_errors
debug_pause "PHP Service Start"
# PHP Service Check
STATUS="$(systemctl is-active "php${NEW_PHP_VER}-fpm")"
if [[ "${STATUS}" == "active" ]]; then
echo "PASSED: PHP ${NEW_PHP_VER} is running."
set_install_state "PHP" "1"
else
echo "FAILED: PHP ${NEW_PHP_VER} not running. Please diagnose this issue before proceeding."
exit 1
fi
# Remove old PHP version(s)
echo "Removing old PHP installation(s)..."
for OLD_VER in "${OLD_PHP_VERS[@]}"; do
echo "Removing PHP ${OLD_VER} installation..."
# Stop and disable old PHP service
systemctl stop "php${OLD_VER}-fpm" 2>/dev/null || true
systemctl disable "php${OLD_VER}-fpm" 2>/dev/null || true
# Remove old PHP packages
apt purge -y "php${OLD_VER}*" 2>/dev/null || true
# Remove old PHP configuration directory
rm -rf "/etc/php/${OLD_VER}" 2>/dev/null || true
# Remove old PHP logrotate configuration
rm -f "/etc/logrotate.d/php${OLD_VER}-fpm" 2>/dev/null || true
# Archive old log
if [[ -f "/var/log/php/php${OLD_VER}-fpm.log" ]]; then
mv "/var/log/php/php${OLD_VER}-fpm.log" "/var/log/php/php${OLD_VER}-fpm.log.old" 2>/dev/null || true
fi
echo "PHP ${OLD_VER} has been removed."
done
# Keep PHP apt pins aligned with the selected version after the switch.
for ver in "${SUPPORTED_PHP_VERSIONS[@]}"; do
sanitized="${ver//.}"
pin_file="/etc/apt/preferences.d/php${sanitized}-block"
if [[ "${ver}" == "${NEW_PHP_VER}" ]]; then
rm -f "${pin_file}"
else
echo -e "Package: php${ver}*\nPin: release *\nPin-Priority: -1" > "${pin_file}"
fi
done
# Cleanup
/usr/local/bin/enginescript/scripts/functions/php-clean.sh 2>> /tmp/enginescript_install_errors.log
/usr/local/bin/enginescript/scripts/functions/enginescript-cleanup.sh 2>> /tmp/enginescript_install_errors.log
print_last_errors
debug_pause "Cleanup"
# Display PHP version and modules
echo -e "\n\n=-=-=-=-=-=-=-=-=-\nPHP Info\n=-=-=-=-=-=-=-=-=-\n"
php --version
echo ""
php -m
# Final message
echo ""
echo ""
echo "============================================================="
echo ""
echo "PHP upgrade from ${MIGRATION_SOURCE_PHP_VERS[*]} to ${NEW_PHP_VER} completed successfully."
echo ""
echo "Changes made:"
echo " - Installed PHP ${NEW_PHP_VER} and extensions"
echo " - Applied EngineScript configuration for PHP ${NEW_PHP_VER}"
echo " - Updated Nginx configuration"
echo " - Removed PHP version(s): ${MIGRATION_SOURCE_PHP_VERS[*]}"
echo ""
echo "============================================================="
echo ""