-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvhost-install.sh
More file actions
407 lines (334 loc) · 16.8 KB
/
vhost-install.sh
File metadata and controls
407 lines (334 loc) · 16.8 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/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; }
# Source shared vhost functions library
source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-shared-vhost.sh || { echo "Error: Failed to source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-shared-vhost.sh" >&2; exit 1; }
# Source shared database credential functions
source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-db-credentials.sh || { echo "Error: Failed to source /usr/local/bin/enginescript/scripts/functions/shared/enginescript-db-credentials.sh" >&2; exit 1; }
#----------------------------------------------------------------------------------
# Start Main Script
# Prompt timeout settings (seconds)
WORDPRESS_PROMPT_TIMEOUT=300
# WordPress admin username length policy
MIN_WP_ADMIN_USERNAME_LENGTH=3
MAX_WP_ADMIN_USERNAME_LENGTH=60
# Security policy settings
MIN_WP_ADMIN_PASSWORD_LENGTH=12
# Note: The following functions and constants are now defined in enginescript-db-credentials.sh:
# - escape_sql_string_literal()
# - validate_db_identifier()
# - MULTIPART_PUBLIC_SUFFIXES / MULTIPART_SUFFIX_CASE_PATTERN
# - DB_USER_MIN_LENGTH / DB_USER_MAX_LENGTH
# Check if services are running
check_required_services
# Intro Warning
echo ""
echo "-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-"
echo "| Domain Creation |"
echo "-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-"
echo ""
echo "WARNING: Do not run this script on a site that already exists."
echo "If you do, things will break."
echo ""
sleep 1
# Domain Input
echo "For the domain name, enter only the domain portion (e.g., 'wordpresstesting')."
echo "Note: lowercase text only, no spaces or special characters. Do not include https:// or www."
echo ""
echo "Then, select a valid TLD from the provided list."
echo ""
# Prompt for domain name
# IMPORTANT: Single-character domain names (e.g., 'x.com', 'a.io') MUST be accepted by this regex.
# They are fully valid under DNS and ICANN rules, and EngineScript must support them.
#
# INTENTIONAL DESIGN — DO NOT CHANGE THIS REGEX:
# ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$
#
# The optional group `([a-z0-9-]*[a-z0-9])?` makes the entire suffix optional, which means a
# single alphanumeric character (e.g., "x") satisfies the pattern on its own. The group is still
# required for multi-character names to prevent leading or trailing hyphens (e.g., "-bad" or
# "bad-" would not match). Changing this back to `^[a-z0-9][a-z0-9-]*[a-z0-9]$` would silently
# reject every single-character label and break installs for legitimate one-letter domains.
#
# Rules enforced by this regex:
# - Minimum length: 1 character (single-char labels are valid DNS labels per RFC 1035)
# - Only lowercase letters (a-z), digits (0-9), and hyphens (-) are permitted
# - The label must not start or end with a hyphen (per RFC 952 / RFC 1123)
#
# This is intentional behaviour. Do not "fix" it to require at least two characters.
while true; do
read -p "Enter the domain name (e.g., 'wordpresstesting'): " DOMAIN_NAME
if [[ "$DOMAIN_NAME" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]]; then
echo "You entered: ${DOMAIN_NAME}"
break
else
echo "Invalid domain name. Only lowercase letters, numbers, and hyphens are allowed. Hyphens cannot be at the start or end."
fi
done
# Prompt for TLD
echo ""
echo "Select a valid TLD from the list below:"
VALID_TLDS=(
# Common gTLDs
"com" "net" "org" "info" "biz" "name" "pro" "int"
# Popular gTLDs
"io" "dev" "app" "tech" "ai" "cloud" "store" "online" "site" "xyz" "club"
"design" "media" "agency" "solutions" "services" "digital" "studio" "live"
"blog" "news" "shop" "art" "finance" "health" "law" "marketing" "software"
# Country-code TLDs (ccTLDs)
"us" "uk" "ca" "au" "de" "fr" "es" "it" "nl" "se" "no" "fi" "dk" "jp" "cn"
"in" "br" "ru" "za" "mx" "ar" "ch" "at" "be" "pl" "gr" "pt" "tr" "kr" "hk"
"sg" "id" "my" "th" "ph" "vn" "nz" "ie" "il" "sa" "ae" "eg" "ng" "ke" "gh"
# Common multi-part public suffixes
"co.uk" "co.jp" "com.au" "co.nz" "com.sg" "com.my" "com.br" "com.mx" "co.za" "com.tr" "com.hk"
)
select TLD in "${VALID_TLDS[@]}"; do
if [[ -n "$TLD" ]]; then
echo "You selected: ${TLD}"
break
else
echo "Invalid selection. Please choose a valid TLD from the list."
fi
done
# Combine domain name and TLD
DOMAIN="${DOMAIN_NAME}.${TLD}"
echo ""
echo "Full domain: ${DOMAIN}"
sleep 2
# Verify if the domain is already configured
if grep -Fxq "${DOMAIN}" /home/EngineScript/sites-list/sites.sh; then
echo -e "\n\n${BOLD}Preinstallation Check: Failed${NORMAL}\n\n${DOMAIN} is already installed.${NORMAL}\n\nIf you believe this is an error, please remove the domain by using the ${BOLD}es.menu${NORMAL} command and selecting the Server & Site Tools option\n\n"
exit 1
else
echo "${BOLD}Preinstallation Check: Passed${NORMAL}"
fi
# Logging
LOG_FILE="/var/log/EngineScript/vhost-install.log"
exec > >(tee -a "${LOG_FILE}") 2>&1
echo "Starting domain installation for ${DOMAIN} at $(date)"
# WordPress Installation Choice
echo ""
echo "-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-"
echo "| Installation Type |"
echo "-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-"
echo ""
echo "You can install this domain with or without WordPress."
echo ""
echo " With WordPress: Full LEMP stack with WordPress, database,"
echo " plugins, Redis caching, and backups."
echo ""
echo " Without WordPress: Nginx vhost, SSL certificates, and a"
echo " placeholder page. No database or CMS."
echo ""
# Keep WORDPRESS_PROMPT_TIMEOUT variable for this prompt.
if prompt_yes_no "Would you like to install WordPress on this domain?" "y" "${WORDPRESS_PROMPT_TIMEOUT}"; then
INSTALL_WORDPRESS="1"
echo "WordPress will be installed on ${DOMAIN}."
else
INSTALL_WORDPRESS="0"
echo "Skipping WordPress. A placeholder page will be installed on ${DOMAIN}."
fi
echo ""
sleep 1
# Continue the installation
# Cloudflare API Settings
# Set Cloudflare settings for the domain using the Cloudflare API
configure_cloudflare_settings "${DOMAIN}"
# Create nginx vhost configuration files
create_nginx_vhost "${DOMAIN}"
# Create and install SSL certificates
create_ssl_certificate "${DOMAIN}"
# Print date for logs
echo "System Date: $(date)"
# Create backup directories
create_backup_directories "${DOMAIN}"
# Site Root
mkdir -p "/var/www/sites/${DOMAIN}/html"
cd "/var/www/sites/${DOMAIN}/html"
# Create domain log directories and files
create_domain_logs "${DOMAIN}"
if [[ "${INSTALL_WORDPRESS}" == "1" ]]; then
#----------------------------------------------------------------------------------
# WordPress Installation Path
#----------------------------------------------------------------------------------
# Domain Creation Variables
# Generate random credential strings using main variables file
source /usr/local/bin/enginescript/enginescript-variables.txt
PREFIX="${RAND_CHAR2}"
# Generate the database name using the install method (domain parsing, hash, suffix)
generate_install_db_name "${DOMAIN}" || exit 1
database_name="${ES_DB_NAME}"
database_user="${RAND_CHAR16}"
database_password="${RAND_CHAR32}"
# Validate generated credentials before writing any sensitive data to disk.
validate_install_credentials "${database_user}" "${database_password}" "${DOMAIN}" || exit 1
# Write credentials file and source it (sets DB, USR, PSWD)
write_credentials_file "/home/EngineScript/mysql-credentials" "${DOMAIN}" "${database_name}" "${database_user}" "${database_password}"
echo "Randomly generated MySQL database credentials for ${DOMAIN}."
# Execute the SQL statements to create the database, user, and grant privileges
execute_install_sql "${DB}" "${USR}" "${PSWD}" "${DOMAIN}" || exit 1
# Download WordPress using WP-CLI
wp core download --allow-root
if ! wp plugin delete hello-dolly --allow-root; then
echo "Notice: Could not delete 'hello-dolly' via WP-CLI. This is expected on newer WordPress versions where the plugin is not installed by default. Continuing installation."
fi
# Create Extra WordPress Directories
# WordPress often doesn't include these directories by default, despite them being used or checked in the Health Check plugin.
create_extra_wp_dirs "${DOMAIN}"
# Create wp-config.php
cp -rf /usr/local/bin/enginescript/config/var/www/wordpress/wp-config.php "/var/www/sites/${DOMAIN}/html/wp-config.php"
sed -i "s|SEDWPDB|${DB}|g" "/var/www/sites/${DOMAIN}/html/wp-config.php"
sed -i "s|SEDWPUSER|${USR}|g" "/var/www/sites/${DOMAIN}/html/wp-config.php"
sed -i "s|SEDWPPASS|${PSWD}|g" "/var/www/sites/${DOMAIN}/html/wp-config.php"
sed -i "s|SEDPREFIX|${PREFIX}|g" "/var/www/sites/${DOMAIN}/html/wp-config.php"
sed -i "s|SEDURL|${DOMAIN}|g" "/var/www/sites/${DOMAIN}/html/wp-config.php"
# Configure Redis for WordPress
configure_redis "${DOMAIN}" "/var/www/sites/${DOMAIN}/html/wp-config.php"
# WP Salt Creation
fetch_wp_salts "/var/www/sites/${DOMAIN}/html/wp-config.php"
# Configure wp-config.php settings
configure_wpconfig_settings "${DOMAIN}" "/var/www/sites/${DOMAIN}/html/wp-config.php"
# Create robots.txt file
create_robots_txt "${DOMAIN}" "/var/www/sites/${DOMAIN}/html"
# WP File Permissions
find "/var/www/sites/${DOMAIN}" -type d -print0 | sudo xargs -0 chmod 0755
find "/var/www/sites/${DOMAIN}" -type f -print0 | sudo xargs -0 chmod 0644
chown -R www-data:www-data "/var/www/sites/${DOMAIN}"
chmod +x "/var/www/sites/${DOMAIN}/html/wp-cron.php"
chmod 600 "/var/www/sites/${DOMAIN}/html/wp-config.php"
# WP-CLI Finalizing Install
clear
echo "============================================="
echo "Finalizing ${DOMAIN} Install:"
echo "============================================="
# WP-CLI Install WordPress
cd "/var/www/sites/${DOMAIN}/html"
# Validate WordPress admin credentials before install
if [[ -z "${WP_ADMIN_USERNAME}" || -z "${WP_ADMIN_PASSWORD}" || -z "${WP_ADMIN_EMAIL}" ]]; then
echo "Error: WP admin credentials must not be empty (WP_ADMIN_USERNAME, WP_ADMIN_PASSWORD, WP_ADMIN_EMAIL)." >&2
exit 1
fi
# Username: 3-60 chars, must start with alphanumeric, letters/numbers/underscore/dot/hyphen
# Use explicit length checks for clarity and maintainability, then validate allowed characters.
if [[ ${#WP_ADMIN_USERNAME} -lt ${MIN_WP_ADMIN_USERNAME_LENGTH} || ${#WP_ADMIN_USERNAME} -gt ${MAX_WP_ADMIN_USERNAME_LENGTH} ]]; then
echo "Error: WP_ADMIN_USERNAME must be between ${MIN_WP_ADMIN_USERNAME_LENGTH} and ${MAX_WP_ADMIN_USERNAME_LENGTH} characters long." >&2
exit 1
fi
if [[ ! "${WP_ADMIN_USERNAME}" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*$ ]]; then
echo "Error: WP_ADMIN_USERNAME is invalid. Use letters, numbers, underscore, dot, or hyphen, and start with a letter or number." >&2
exit 1
fi
# Email: basic format validation (practical, non-RFC-complete).
# Accepts common addresses like [email protected] and [email protected].
EMAIL_REGEX='^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
if [[ ! "${WP_ADMIN_EMAIL}" =~ ${EMAIL_REGEX} ]]; then
echo "Error: WP_ADMIN_EMAIL is not a valid email address format." >&2
exit 1
fi
# Password: minimum complexity requirements
if [[ ${#WP_ADMIN_PASSWORD} -lt ${MIN_WP_ADMIN_PASSWORD_LENGTH} ]] || \
[[ ! "${WP_ADMIN_PASSWORD}" =~ [A-Z] ]] || \
[[ ! "${WP_ADMIN_PASSWORD}" =~ [a-z] ]] || \
[[ ! "${WP_ADMIN_PASSWORD}" =~ [0-9] ]] || \
[[ ! "${WP_ADMIN_PASSWORD}" =~ [^A-Za-z0-9] ]]; then
echo "Error: WP_ADMIN_PASSWORD must be at least ${MIN_WP_ADMIN_PASSWORD_LENGTH} characters and include uppercase, lowercase, number, and special character." >&2
exit 1
fi
wp core install --admin_user="${WP_ADMIN_USERNAME}" --admin_password="${WP_ADMIN_PASSWORD}" --admin_email="${WP_ADMIN_EMAIL}" --url="https://${DOMAIN}" --title='New Site' --skip-email --allow-root
# Install and activate required WordPress plugins
install_required_wp_plugins
# Install extra WordPress plugins if enabled
if [[ "${INSTALL_EXTRA_WP_PLUGINS}" == "1" ]]; then
install_extra_wp_plugins
else
echo "Skipping extra WordPress plugins installation (disabled in config)..."
fi
# Install EngineScript custom plugins if enabled
install_enginescript_custom_plugins "${DOMAIN}"
# Clear WordPress caches, transients, and rewrite rules
clear_wordpress_caches
# Enable Redis Cache via WP-CLI
if wp plugin is-active redis-cache --allow-root; then
echo "Enabling Redis object cache..."
wp redis enable --allow-root
else
echo "Warning: Redis Cache plugin not active. Skipping 'wp redis enable'."
fi
# WP-CLI set permalink structure for FastCGI Cache
wp option get permalink_structure --allow-root
wp option update permalink_structure '/%category%/%postname%/' --allow-root
flush_wordpress_rewrites
# Setting Permissions Again
# For whatever reason, using WP-CLI to install plugins with --allow-root reassigns
# the ownership of the /uploads, /upgrade, and plugin directories to root:root.
cd "/var/www/sites/${DOMAIN}"
chown -R www-data:www-data "/var/www/sites/${DOMAIN}"
chmod +x "/var/www/sites/${DOMAIN}/html/wp-cron.php"
find "/var/www/sites/${DOMAIN}" -type d -print0 | sudo xargs -0 chmod 0755
find "/var/www/sites/${DOMAIN}" -type f -print0 | sudo xargs -0 chmod 0644
chmod 600 "/var/www/sites/${DOMAIN}/html/wp-config.php"
clear
# Perform site backup
perform_site_backup "${DOMAIN}" "/var/www/sites/${DOMAIN}/html"
# Display final credentials summary
display_credentials_summary "${DOMAIN}" "${DB}" "${PREFIX}" "${USR}" "${PSWD}"
else
#----------------------------------------------------------------------------------
# Non-WordPress Installation Path (Placeholder Page)
#----------------------------------------------------------------------------------
echo "Installing placeholder page for ${DOMAIN}..."
# Install placeholder page
cp -f /usr/local/bin/enginescript/config/var/www/placeholder/index.html "/var/www/sites/${DOMAIN}/html/index.html"
sed -i "s|YOURDOMAIN|${DOMAIN}|g" "/var/www/sites/${DOMAIN}/html/index.html"
# Set file permissions
find "/var/www/sites/${DOMAIN}" -type d -print0 | sudo xargs -0 chmod 0755
find "/var/www/sites/${DOMAIN}" -type f -print0 | sudo xargs -0 chmod 0644
chown -R www-data:www-data "/var/www/sites/${DOMAIN}"
# Backup nginx vhost and SSL keys
BACKUP_DATE_HOUR=$(date +%m-%d-%Y-%H)
gzip -cf "/etc/nginx/sites-enabled/${DOMAIN}.conf" > "/home/EngineScript/site-backups/${DOMAIN}/nginx/${BACKUP_DATE_HOUR}-nginx-vhost.conf.gz"
tar -zcf "/home/EngineScript/site-backups/${DOMAIN}/ssl-keys/${BACKUP_DATE_HOUR}-ssl-keys.gz" "/etc/nginx/ssl/${DOMAIN}"
clear
echo ""
echo "-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-"
echo "|${BOLD} Domain Summary (No WordPress)${NORMAL} |"
echo "-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-"
echo ""
echo "${BOLD}URL:${NORMAL} ${DOMAIN}"
echo ""
echo "Site root: /var/www/sites/${DOMAIN}/html"
echo "Nginx vhost: /etc/nginx/sites-enabled/${DOMAIN}.conf"
echo "SSL certificates: /etc/nginx/ssl/${DOMAIN}/"
echo "Backups: /home/EngineScript/site-backups/${DOMAIN}/"
echo ""
echo "A placeholder page has been installed. Replace"
echo "/var/www/sites/${DOMAIN}/html/index.html with your own content."
echo "-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-"
echo ""
sleep 3
fi
# Restart Services
/usr/local/bin/enginescript/scripts/functions/alias/alias-restart.sh
echo ""
echo "============================================================="
echo ""
echo " Domain setup completed."
echo ""
echo " Your domain should be available now at:"
echo " https://${DOMAIN}"
echo ""
echo " Returning to main menu in 5 seconds."
echo ""
echo "============================================================="
echo ""
sleep 5