-
Notifications
You must be signed in to change notification settings - Fork 0
/
installwordpress.sh
executable file
·371 lines (307 loc) · 11.2 KB
/
installwordpress.sh
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
#!/bin/bash
# Copyright Rimuhosting.com
#
# script to install wordpress
#
#
## Detect distro version
if [ -e /etc/redhat-release ]; then
DISTRO="centos"
elif [ -e /etc/debian_version ]; then
DISTRO="debian"
fi
set_global_default_env(){
## Wordpress domain
WP_DOMAIN=$(hostname)
## Wordpress location
if [ $DISTRO = "debian" ]; then
WP_LOCATION="/var/www/wordpress"
APACHE_USER="www-data"
fi
if [ $DISTRO = "centos" ]; then
WP_LOCATION="/var/www/html/wordpress"
APACHE_USER="apache"
fi
WP_LOCATION_USER_OWNER=$APACHE_USER
## Database credentials for postfixadmin
WP_DB_PASS=$(</dev/urandom tr -dc A-Za-z0-9 | head -c8)
WP_DB_USER="wordpress"
WP_DB_DATABASE="wordpress"
TASKS="all"
FORCE="no"
if ! getent passwd | grep -q "^$APACHE_USER:"; then
echo "#################################################################"
echo "# error: apache user $APACHE_USER does not exist"
echo "#################################################################"
exit 1
fi
if ! getent passwd | grep -q "^$WP_LOCATION_USER_OWNER:"; then
echo "#################################################################"
echo "# error: wordpress location user owner $WP_LOCATION_USER_OWNER does not exist"
echo "#################################################################"
exit 1
fi
}
install_deps(){
## Install required packages and configure global enviroment
if ! ps aux | grep -q '^mysql.*mysqld'; then
echo "#################################################################"
echo "# mysql server not running, attempt to install? (Ctrl-c to abort)"
echo "#################################################################"
[ $FORCE = "no" ] && read
MYSQL_INSTALL_SCRIPT_URL='http://proj.ri.mu/installmysql.sh'
wget $MYSQL_INSTALL_SCRIPT_URL -O /root/installmysql.sh
if [ $FORCE = "no" ]; then
bash /root/installmysql.sh --noperl --noapache --nophp
export MYSQL_ROOT_PASS=$(cat /root/.mysqlp)
else
bash /root/installmysql.sh --noprompt --adminpass $MYSQL_ROOT_PASS --noperl --noapache --nophp
fi
fi
## Specific distribution packages
if [ $DISTRO = "debian" ]; then
a2enmod rewrite
/etc/init.d/apache2 restart
fi
#if [ $DISTRO = "centos" ]; then
#fi
return 0
}
install_wordpress(){
GENERIC_PACKAGE_LOCATION='http://wordpress.org/latest.tar.gz'
wget $GENERIC_PACKAGE_LOCATION -O /tmp/wordpress.tar.gz
tar -xz -C /tmp -f /tmp/wordpress.tar.gz
rm -f /tmp/wordpress.tar.gz
if [ -d $WP_LOCATION ]; then
echo "#################################################################"
echo "# Directory $WP_LOCATION already exists, move away and proceed? (Ctrl-c to abort)"
echo "#################################################################"
[ $FORCE = "no" ] && read
mv -v $WP_LOCATION $WP_LOCATION.$(date '+%s')
fi
mv /tmp/wordpress $WP_LOCATION
# http://codex.wordpress.org/Hardening_WordPress
find $WP_LOCATION -type d -exec chmod 755 {} \;
find $WP_LOCATION -type f -exec chmod 644 {} \;
chown -R $WP_LOCATION_USER_OWNER: $WP_LOCATION
# make specific locations writeable by the apache user
touch $WP_LOCATION/.htaccess
touch $WP_LOCATION/robots.txt
chown -R $APACHE_USER: $WP_LOCATION/.htaccess $WP_LOCATION/wp-content $WP_LOCATION/robots.txt
return 0
}
configure_wordpress_database(){
echo <<EOFMW "
#################################################################
#
# $0 is about to create the mysql database for wordpress
# called '$WP_DB_DATABASE', and also will setup a mysql database
# user '$WP_DB_USER'.
#
# Warning: if the database exists it will be dropped, if the user
# exists the password will be reset. (Ctrl-c to abort)
#
# Please provide the mysql root password if required
#################################################################
"
EOFMW
[ $FORCE = "no" ] && read
mysql -f -u root -p$MYSQL_ROOT_PASS -e <<EOSQL "DROP DATABASE IF EXISTS $WP_DB_DATABASE ;
CREATE DATABASE $WP_DB_DATABASE;
GRANT ALL PRIVILEGES ON $WP_DB_DATABASE.* TO '$WP_DB_USER'@'localhost' IDENTIFIED BY '$WP_DB_PASS';
FLUSH PRIVILEGES;"
EOSQL
}
configure_wordpress(){
WP_CONFIG=$WP_LOCATION/wp-config.php
cp $WP_LOCATION/wp-config-sample.php $WP_CONFIG
## Edits wordpress config:
#
sed -i "s/^define('DB_NAME'.*);/define('DB_NAME', '$WP_DB_DATABASE');/g" $WP_CONFIG
sed -i "s/^define('DB_USER'.*);/define('DB_USER', '$WP_DB_USER');/g" $WP_CONFIG
sed -i "s/^define('DB_PASSWORD'.*);/define('DB_PASSWORD', '$WP_DB_PASS');/g" $WP_CONFIG
SALTSLIST="AUTH_KEY SECURE_AUTH_KEY LOGGED_IN_KEY NONCE_KEY AUTH_SALT SECURE_AUTH_SALT LOGGED_IN_SALT NONCE_SALT"
for s in $SALTSLIST; do
sed -i "s/^define('"$s".*);/define('"$s"', '"$(</dev/urandom tr -dc A-Za-z0-9 | head -c64)"');/g" $WP_CONFIG
done
return 0
}
configure_apache(){
if [ $DISTRO = "debian" ]; then
APACHE_CONFIG="/etc/apache2/sites-available/$WP_DOMAIN"
APACHE_INIT="/etc/init.d/apache2"
APACHE_ERROR_LOG="\${APACHE_LOG_DIR}/$WP_DOMAIN.error.log"
APACHE_ACCESS_LOG="\${APACHE_LOG_DIR}/$WP_DOMAIN.access.log"
if [ -f $APACHE_CONFIG ]; then
echo "#################################################################"
echo "# Virtual host configuration file $APACHE_CONFIG already exists, move away and proceed? (Ctrl-c to abort)"
echo "#################################################################"
[ $FORCE = "no" ] && read
mv -v $APACHE_CONFIG $APACHE_CONFIG.$(date '+%s')
fi
fi
if [ $DISTRO = "centos" ]; then
APACHE_CONFIG="/etc/httpd/conf/httpd.conf"
APACHE_INIT="/etc/init.d/httpd"
APACHE_ERROR_LOG="/var/log/httpd/$WP_DOMAIN.error.log"
APACHE_ACCESS_LOG="/var/log/httpd/$WP_DOMAIN.access.log"
if grep -q "DocumentRoot $WP_LOCATION" $APACHE_CONFIG; then
echo "#################################################################"
echo "# There seems to be a virtual host pointing to $WP_LOCATION at $APACHE_CONFIG, proceed adding the config anyways? (Ctrl-c to abort)"
echo "#################################################################"
[ $FORCE = "no" ] && read
fi
echo "#################################################################"
echo "# Saving httpd config backup"
echo "#################################################################"
cp -v $APACHE_CONFIG $APACHE_CONFIG.$(date '+%s')
fi
echo <<EOFVH >>$APACHE_CONFIG "
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName $WP_DOMAIN
DocumentRoot $WP_LOCATION
<Directory $WP_LOCATION>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog $APACHE_ERROR_LOG
LogLevel warn
CustomLog $APACHE_ACCESS_LOG combined
</VirtualHost>
"
EOFVH
if [ $DISTRO = "debian" ]; then
a2ensite $WP_DOMAIN
fi
$APACHE_INIT restart
return 0
}
usage(){
echo <<USAGE "
Usage: $(basename $0) [OPTION...]
$(basename $0) will attempt to install all configurations for wordpress by default,
it will generate random passwords and the relevant ones will be informed.This script
is provided as it is, no warraties implied.
Options:
-d <domain> domain where wordpress will operate. DEFAULT: $WP_DOMAIN
-l <path> location path where to install wordpress. DEFAULT: $WP_LOCATION
-j <dbuser> mysql database username to be setup. DEFAULT: $WP_DB_USER
-k <dbpass> password to be assigned to the mysql database user. DEFAULT: RANDOM
-i <dbname> mysql Database name. DEFAULT: $WP_DB_DATABASE
-u <system_user> set default install location path ownership to the defined user. DEFAULT: $WP_LOCATION_USER_OWNER
-a <apache_user> set ownership in specific paths in the location path to webserver user (wordpress requires to write here). DEFAULT: $APACHE_USER
-f force the install, prompts in error/warnings are disabled.
-h this Help
Advanced Options:
-t <task1,task2> Comma separated of tasks to execute manually, may depend on the above
options. DEFAULT: all
Possible Tasks:
install_deps installs wordpress dependencies
install_wordpress downloads and installs wordpress package
configure_wordpress_database configures wordpress database
configure_wordpress configures wordpress
configure_apache configures apache virtual host
"
USAGE
}
set_global_default_env
## Parse args and execute tasks
while getopts 'd:l:j:k:i:u:a:t:fh' option; do
case $option in
d) WP_DOMAIN=$OPTARG;;
l) WP_LOCATION=$OPTARG;;
j) WP_DB_USER=$OPTARG;;
k) WP_DB_PASS=$OPTARG;;
i) WP_DB_DATABASE=$OPTARG;;
u) WP_LOCATION_USER_OWNER=$OPTARG
if ! getent passwd | grep -q "^$WP_LOCATION_USER_OWNER:"; then
echo "#################################################################"
echo "# error: wordpress location user owner $WP_LOCATION_USER_OWNER does not exist"
echo "#################################################################"
exit 1
fi
;;
a) APACHE_USER=$OPTARG
if ! getent passwd | grep -q "^$APACHE_USER:"; then
echo "#################################################################"
echo "# error: apache user $APACHE_USER does not exist"
echo "#################################################################"
exit 1
fi
;;
t) TASKS=$OPTARG;;
f) FORCE="yes";;
h) usage
exit 0;;
[?]) usage
exit 1;;
esac
done
shift $(($OPTIND - 1))
echo <<EOF "
#################################################################
#
# Using the following enviroment:
#
# Wordpress domain: $WP_DOMAIN
# Wordpress location: $WP_LOCATION
# Wordpress Mysql username: $WP_DB_USER
# Wordpress Mysql password: $WP_DB_PASS
# Wordpress Mysql database: $WP_DB_DATABASE
# Wordpress location owner: $WP_LOCATION_USER_OWNER
# Wordpress apache user: $APACHE_USER
#
#################################################################
"
EOF
if [ $TASKS = "all" ]; then
echo <<EOF "
$(basename $0) will attempt to install all configurations for wordpress by default,
it will generate random passwords and the relevant ones will be informed. This script
is provided as it is, no warraties implied. (Ctrl-c to abort)
"
EOF
[ $FORCE = "no" ] && read
install_deps
[ $? -ne "0" ] && exit 1
install_wordpress
[ $? -ne "0" ] && exit 1
configure_wordpress_database
[ $? -ne "0" ] && exit 1
configure_wordpress
[ $? -ne "0" ] && exit 1
#configure_apache
[ $? -ne "0" ] && exit 1
else
for t in $( echo $TASKS | tr ',' ' '); do
$t
done
fi
echo <<EOF "
#################################################################
#
# Used the following enviroment:
#
# Wordpress domain: $WP_DOMAIN
# Wordpress location: $WP_LOCATION
# Wordpress Mysql username: $WP_DB_USER
# Wordpress Mysql password: $WP_DB_PASS
# Wordpress Mysql database: $WP_DB_DATABASE
# Wordpress location owner: $WP_LOCATION_USER_OWNER
# Wordpress apache user: $APACHE_USER
#
# Make sure you have a DNS record $WP_DOMAIN pointing to the server ip.
# Finish the setup by going to http://$WP_DOMAIN and complete the famous five
# minute WordPress installation process.
#
# Note: In case the $WP_DOMAIN is matching the server hostname (overlaping default site config),
# the site may not work, you may need to disable the default site in debian based systems or check
# apache configuration, for example
# a2dissite default && service apache2 restart
#################################################################
"
EOF
exit 0