-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall
executable file
·276 lines (236 loc) · 6.23 KB
/
install
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
#!/bin/bash
#
# This simple script is to help you get started quickly.
#
# Remove wp-content directory.
if [ -d app/cms/wp-content ]
then
rm -rf app/cms/wp-content
fi
# Remove default WordPress loader.
if [ -f app/cms/index.php ]
then
echo "<?php // Silence is golden." > app/cms/index.php
fi
# Ready! Start installation.
echo "Starting WordPress installation..."
# Get server name.
read -p "Server name: " SERVER_NAME
# Get database name.
read -p "Database name (DB_NAME): " DB_NAME
# Get database username.
read -p "Database username (DB_USER): " DB_USER
# Get database password.
read -s -p "Database password (DB_PASSWORD): " DB_PASSWORD
# Get database charset.
echo ""; read -p "Database charset (DB_CHARSET): " DB_CHARSET
if [ -z "$DB_CHARSET" ]
then
DB_CHARSET="utf8mb4"
fi
# Get database collation.
read -p "Database collation (DB_COLLATE): " DB_COLLATE
if [ -z "$DB_COLLATE" ]
then
DB_COLLATE="utf8mb4_general_ci"
fi
# Get database tables' prefix.
read -p "Database tables' prefix (\$table_prefix): " TABLE_PREFIX
# Get MySQL server address.
read -p "MySQL server address (DB_HOST): " DB_HOST
if [ -z "$DB_HOST" ]
then
DB_HOST="127.0.0.1"
fi
# Get MySQL client address.
read -p "MySQL client address: " USER_HOST
if [ -z "$USER_HOST" ]
then
USER_HOST="127.0.0.1"
fi
# Get MySQL server superior user.
read -p "MySQL server superior username. E.g. root: " MYSQL_USER
if [ -z "$MYSQL_USER" ]
then
MYSQL_USER="root"
fi
# Create database.
echo "Connecting to the MySQL server..."
echo "Creating MySQL user and database..."
mysql -h $DB_HOST -u $MYSQL_USER -p <<SHELL
create user '$DB_USER'@'$USER_HOST' identified by '$DB_PASSWORD';
create database if not exists $DB_NAME;
grant alter,create,delete,drop,index,lock tables,insert,select,update on $DB_NAME.* to '$DB_USER'@'$USER_HOST';
flush privileges;
quit
SHELL
# If there's MySQL failure.
if [ $? -eq 1 ]
then
echo -e "\033[31mWarning\033[0m: Failed to create MySQL database!"
exit 1
fi
# Database created.
echo -e "\033[32mSuccess\033[0m: Database has been created successfully!"
# Get secret keys and salts.
echo "Generating secret keys and salts..."
SALTS=$(curl -s -S https://api.wordpress.org/secret-key/1.1/salt/)
# Get environment.
echo "Which environment will be used to deploy? [1|2|3]"
echo "1 - Staging"
echo "2 - Production"
echo "3 - Development"
read OPTION
case $OPTION in
1) APP_ENV='staging';;
2) APP_ENV='production';;
3|*) APP_ENV='development';;
esac
# Inherit environment configurations.
if sed -i -E "s%^inherit:.*%inherit: config/env/$APP_ENV.yml%g" wp-cli.yml
then
echo -e "\033[32mSuccess\033[0m: $APP_ENV environment has been set up successfully!"
rm -f wp-cli.yml-E
else
echo -e "\033[33mNotice\033[0m: Failed to set up $APP_ENV environment!"
fi
# Get domain name and home url.
read -p "Site address (with http protocol, WP_HOME): " HOME_URL
WP_HOME=${HOME_URL%/}
DOMAIN_NAME=$(echo $WP_HOME | awk -F/ '{print $3}')
# Cat settings to config file.
echo "Generating config file..."
if cat > config/$DOMAIN_NAME.php <<SETTINGS
<?php
/**
* Site settings
*
* @see public/wp-config.php Bootstrap file. DRY!
*/
/**
* Database table prefix
*
* @see https://codex.wordpress.org/Editing_wp-config.php#table_prefix
*/
\$table_prefix = '$TABLE_PREFIX';
/**
* Homepage URL
*
* @see https://codex.wordpress.org/Editing_wp-config.php#Blog_address_.28URL.29
*
* @var string
*/
define('WP_HOME', '$WP_HOME');
/**
* Database host
*
* @see https://codex.wordpress.org/Editing_wp-config.php#Set_Database_Host
*
* @var string
*/
define('DB_HOST', '$DB_HOST');
/**
* Database username
*
* @see https://codex.wordpress.org/Editing_wp-config.php#Set_Database_User
*
* @var string
*/
define('DB_USER', '$DB_USER');
/**
* Database name
*
* @see https://codex.wordpress.org/Editing_wp-config.php#Set_Database_Name
*
* @var string
*/
define('DB_NAME', '$DB_NAME');
/**
* Database charset
*
* @see https://codex.wordpress.org/Editing_wp-config.php#Database_character_set
*
* @var string
*/
define('DB_CHARSET', '$DB_CHARSET');
/**
* Database collate
*
* @see https://codex.wordpress.org/Editing_wp-config.php#Database_collation
*
* @var string
*/
define('DB_COLLATE', '$DB_COLLATE');
/**
* Database password
*
* @see https://codex.wordpress.org/Editing_wp-config.php#Set_Database_Password
*
* @var string
*/
define('DB_PASSWORD', '$DB_PASSWORD');
/**
* Secret keys and salts
*
* @link https://api.wordpress.org/secret-key/1.1/salt/
*
* @var string
*/
$SALTS
/**
* Load environment.
*
* @var resource
*/
require APP_ROOT . 'config/env/$APP_ENV.php';
SETTINGS
then
echo -e "\033[32mSuccess\033[0m: Config file has been generated successfully!"
else
echo -e "\033[31mError\033[0m: Failed to generate config file. Please make sure that config folder is writable!"
exit 1
fi
# Get site title.
read -p "Site title: " SITE_TITLE
# Get admin username.
read -p "Admin username: " ADMIN_USERNAME
# Get admin pwssword.
read -s -p "Admin password: " ADMIN_PWD
# Get admin email address.
echo ""; read -p "Admin email: " ADMIN_EMAIL
# Do installation.
if ! type "wp" >> /dev/null
then
echo -e "\033[31mError\033[0m: wp-cli is not installed. Please install wp-cli!"
exit 1
else
wp core install --title="$SITE_TITLE" --admin_user="$ADMIN_USERNAME" --admin_password="$ADMIN_PWD" --admin_email="$ADMIN_EMAIL" --url="$WP_HOME" --skip-email
fi
# If failed to install WordPress.
if [ $? -ge 1 ]
then
echo -e "\033[31mError\033[0m: Failed to install WordPress!"
exit 1
fi
# Switch --url
if sed -i -E "s%^url:.*%url: $DOMAIN_NAME%g" wp-cli.yml
then
echo -e "\033[32mSuccess\033[0m: Switched to $DOMAIN_NAME"
rm -f wp-cli.yml-E
else
echo -e "\033[33mNotice\033[0m: Failed to switch to $DOMAIN_NAME"
fi
# Correct MySQL privileges after WordPress installed.
echo -e "\033[33mNotice\033[0m: ALTER and DROP were granted for $DB_USER@$USER_HOST while installing WordPress."
read -p "Do you want to revoke those MySQL privileges now? [y|n]" REVOKE_OR_NOT
if [ "y" = "$REVOKE_OR_NOT" ]
then
echo "Connecting to the MySQL server again..."
mysql -h $DB_HOST -u $MYSQL_USER -p << SHELL
revoke alter,drop on $DB_NAME.* from '$DB_USER'@'$USER_HOST';
flush privileges;
quit
SHELL
fi
# Done!
echo -e "\033[32mDone!\033[0m Now, access admin dashboard at $WP_HOME/cms/wp-admin"