-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-installer.sh
More file actions
executable file
·168 lines (139 loc) · 5.66 KB
/
wp-installer.sh
File metadata and controls
executable file
·168 lines (139 loc) · 5.66 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
#!/bin/bash
set -euo pipefail
# Convert line endings (helpful for cross-platform)
command -v dos2unix >/dev/null 2>&1 && dos2unix "$0" 2>/dev/null || true
echo "Starting WordPress installation process..."
# Increase PHP memory limit for this process
export WP_CLI_PHP_ARGS='-d memory_limit=512M'
# First, download WordPress core files if they don't exist
echo "Checking for WordPress core files..."
if [ ! -f "/var/www/html/wp-config.php" ] && [ ! -f "/var/www/html/index.php" ]; then
echo "WordPress core files not found. Downloading..."
# Try to download with increased memory and timeout
timeout 180 wp core download --allow-root --force --locale=en_US || { # todo: is --allow-root needed here?
echo "wp-cli download failed, trying alternative method..."
# Alternative: download WordPress directly using curl
echo "Downloading WordPress directly..."
cd /var/www/html
curl -o latest.tar.gz https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz --strip-components=1
rm latest.tar.gz
chown -R www-data:www-data /var/www/html
}
fi
# Now test database connectivity using a different method first
echo "Testing basic database connectivity..."
MAX_TRIES=60
COUNT=0
# Function to test database connectivity
test_database_connection() {
php -r "exit(mysqli_connect('${WORDPRESS_DB_HOST}','${WORDPRESS_DB_USER}','${WORDPRESS_DB_PASSWORD}','${WORDPRESS_DB_NAME}') ? 0 : 1);" 2>/dev/null
return $?
}
echo "Waiting for database connection..."
until test_database_connection || [ $COUNT -eq $MAX_TRIES ]; do
echo "Database not ready yet, waiting... ($COUNT/$MAX_TRIES)"
sleep 5
COUNT=$((COUNT+1))
done
if [ $COUNT -eq $MAX_TRIES ]; then
echo "ERROR: Database connection timed out!"
echo "Attempting diagnostics..."
echo "Database host: ${WORDPRESS_DB_HOST}"
echo "Database name: ${WORDPRESS_DB_NAME}"
echo "Database user: ${WORDPRESS_DB_USER}"
# Try to ping the database host
echo "Testing host connectivity..."
if ping -c 1 "${WORDPRESS_DB_HOST}" > /dev/null 2>&1; then
echo "✓ Database host is reachable"
else
echo "✗ Database host is unreachable"
fi
exit 1
fi
echo "✓ Database connection successful!"
# Create wp-config.php if it doesn't exist
if [ ! -f "/var/www/html/wp-config.php" ]; then
echo "Creating wp-config.php..."
# Try wp-cli method first
if ! wp config create --allow-root \
--dbname="${WORDPRESS_DB_NAME}" \
--dbuser="${WORDPRESS_DB_USER}" \
--dbpass="${WORDPRESS_DB_PASSWORD}" \
--dbhost="${WORDPRESS_DB_HOST}" \
--extra-php <<PHP
// Custom WordPress configuration
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_AUTO_UPDATE_CORE', true);
PHP
then
echo "wp-cli config create failed, creating wp-config.php manually..."
# Manual wp-config.php creation
cat > /var/www/html/wp-config.php <<EOF
<?php
define('DB_NAME', '${WORDPRESS_DB_NAME}');
define('DB_USER', '${WORDPRESS_DB_USER}');
define('DB_PASSWORD', '${WORDPRESS_DB_PASSWORD}');
define('DB_HOST', '${WORDPRESS_DB_HOST}');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
\$table_prefix = 'wp_';
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_AUTO_UPDATE_CORE', true);
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
require_once ABSPATH . 'wp-settings.php';
EOF
chown www-data:www-data /var/www/html/wp-config.php
fi
fi
# Check if WordPress is already installed
echo "Checking WordPress installation status..."
if ! wp core is-installed --allow-root 2>/dev/null; then
echo "Installing WordPress..."
# Try wp-cli installation
if wp core install --allow-root \
--url="${WORDPRESS_SITE_URL:-http://localhost:8080}" \
--title="${WORDPRESS_SITE_TITLE:-WordPress Development Site}" \
--admin_user="${WORDPRESS_ADMIN_USER:-admin}" \
--admin_password="${WORDPRESS_ADMIN_PASSWORD:-password}" \
--admin_email="${WORDPRESS_ADMIN_EMAIL:-admin@example.com}" 2>/dev/null; then
echo "✓ WordPress installation successful!"
# Set proper file permissions
echo "Setting file permissions..."
chown -R www-data:www-data /var/www/html
find /var/www/html -type d -exec chmod 755 {} \; 2>/dev/null || true
find /var/www/html -type f -exec chmod 644 {} \; 2>/dev/null || true
# Install development plugins
wp plugin install query-monitor --activate --allow-root 2>/dev/null || echo "Note: Could not install query-monitor"
echo "WordPress installation complete!"
echo "Site: ${WORDPRESS_SITE_URL:-http://localhost:8080}"
echo "Admin: ${WORDPRESS_ADMIN_USER:-admin} / ${WORDPRESS_ADMIN_PASSWORD:-password}"
else
echo "wp-cli installation failed, but WordPress files are in place"
echo "You can complete the installation manually at: ${WORDPRESS_SITE_URL:-http://localhost:8080}"
fi
else
echo "WordPress is already installed."
echo "Site: ${WORDPRESS_SITE_URL:-http://localhost:8080}"
fi
echo "WordPress setup finished successfully!"
# Final check - make sure index.php exists
if [ -f "/var/www/html/index.php" ]; then
echo "✓ WordPress files are in place"
else
echo "⚠ Warning: index.php not found, there may be an issue with the installation"
fi