-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-scratch-box.sh
231 lines (188 loc) · 7.32 KB
/
wp-scratch-box.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
#!/bin/bash -
#title :wp-scratch-box.sh
#description :provisioning script for wp-scratch-box
#author :Cristovao Verstraeten
#date :20151120
#version :see CHANGELOG.md
#usage :vagrant up --provision, vagrant provision
#notes :
#bash_version :4.3.39(3)-release
#============================================================================
set -euo pipefail
IFS=$'\n\t'
PHPVERSION=${PHPVERSION:-"8.2"}
main() {
# set -x
base_packages
lamp_install
memcached_install
mailpit_install
composer_install
wpcli_install
wordpress
if [ -d "/home/vagrant/public" ];
then
echo "Symlink exists."
else
ln -s /var/www/public_html/ public_html
fi
# set +x
}
base_packages() {
echo '* libraries/restart-without-asking boolean true' | sudo debconf-set-selections
sudo apt-get update && sudo apt-get install -y -qq \
apt-transport-https \
curl \
git-core \
imagemagick \
jq \
ntp \
software-properties-common \
unzip \
vim \
zip
cp -f /home/vagrant/.profile /home/vagrant/.bash_profile
}
lamp_install() {
apache_install
mariadb_install
phpfpm_install
# Post LAMP installation.
apache_configurations
}
apache_install() {
# Add repository.
sudo add-apt-repository ppa:ondrej/apache2
# Update apt source and install quietly.
sudo apt-get update && sudo apt-get install -y -qq \
apache2
}
apache_configurations() {
sudo a2enmod expires headers proxy proxy_fcgi rewrite setenvif
sudo a2enconf php${PHPVERSION}-fpm
sudo service apache2 restart
sudo cp -f /vagrant/resources/apache/wp-scratch-box.conf /etc/apache2/sites-available/000-default.conf
sudo service apache2 reload
sudo usermod -a -G www-data vagrant
}
mariadb_install() {
local root_password="root"
# First import the release signing key.
sudo mkdir -p /etc/apt/keyrings
sudo curl -o /etc/apt/keyrings/mariadb-keyring.pgp 'https://mariadb.org/mariadb_release_signing_key.pgp'
# Add source file.
sudo cp -f /vagrant/resources/mariadb/mariadb.sources /etc/apt/sources.list.d/mariadb.sources
# Set default password.
echo "maria-db-10.6 mysql-server/root_password password $root_password" | sudo debconf-set-selections
echo "maria-db-10.6 mysql-server/root_password_again password $root_password" | sudo debconf-set-selections
# Update apt source and install quietly.
sudo apt-get update && sudo apt-get install -y -qq mariadb-server
# Revert to the old mysql_native_password authentication method. Needed from 10.4 on.
local revert_auth=$( cat <<-SQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('root');
FLUSH PRIVILEGES;
SQL
)
sudo mysql -u root -proot -e "${revert_auth}"
# Run MySQL without passwords for convenience.
cp -f /vagrant/resources/mariadb/my.cnf /home/vagrant/.my.cnf
}
phpfpm_install() {
# Add repository.
sudo add-apt-repository ppa:ondrej/php
# Update apt source and install quietly.
sudo apt-get update && sudo apt-get install -y -qq php${PHPVERSION}-fpm \
php${PHPVERSION}-cli php${PHPVERSION}-common php${PHPVERSION}-bcmath php${PHPVERSION}-curl \
php${PHPVERSION}-gd php${PHPVERSION}-imagick php${PHPVERSION}-imap php${PHPVERSION}-intl \
php${PHPVERSION}-mbstring php${PHPVERSION}-mysql php${PHPVERSION}-pcov php${PHPVERSION}-soap \
php${PHPVERSION}-ssh2 php${PHPVERSION}-xdebug php${PHPVERSION}-xml php${PHPVERSION}-xmlrpc php${PHPVERSION}-zip php-pear \
php${PHPVERSION}-memcache php${PHPVERSION}-memcached
sudo cp -f /vagrant/resources/php/php-fpm.conf /etc/php/${PHPVERSION}/fpm/php-fpm.conf
sudo cp -f /vagrant/resources/php/pool-www.conf /etc/php/${PHPVERSION}/fpm/pool.d/www.conf
sudo cp -f /vagrant/resources/php/custom-php.ini /etc/php/${PHPVERSION}/fpm/conf.d/php-custom.ini
sudo cp -f /vagrant/resources/php/opcache.ini /etc/php/${PHPVERSION}/fpm/conf.d/opcache.ini
sudo cp -f /vagrant/resources/php/mailpit.ini /etc/php/${PHPVERSION}/mods-available/mailpit.ini
sudo cp -f /vagrant/resources/php/xdebug.ini /etc/php/${PHPVERSION}/mods-available/xdebug.ini
# sudo phpenmod custom-php
sudo phpenmod mailpit
sudo phpdismod xdebug
sudo phpdismod pcov
# Explicitly restart PHP.
sudo service php${PHPVERSION}-fpm restart &> /dev/null
}
memcached_install() {
# Update apt source and install quietly.
sudo apt-get update && sudo apt-get install -y -qq memcached
# Copy memcached configuration.
sudo cp -f /vagrant/resources/memcached/memcached.conf /etc/memcached.conf
# Restart memcached service.
sudo systemctl restart memcached
}
mailpit_install() {
# Install mailpit from install script.
sudo bash < <(curl -sL https://raw.githubusercontent.com/axllent/mailpit/develop/install.sh)
# Copy mailpit service file.
sudo cp -f /vagrant/resources/mailpit/mailpit.service /etc/systemd/system/mailpit.service
# Restart mailpit service.
sudo systemctl enable --now mailpit
}
composer_install() {
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
composer --version
# Fix 'permission denied' with vagrant-cachier symlink.
if [ -d "/home/vagrant/.composer" ];
then
composer --version
echo "Composer directory exists."
else
mkdir /home/vagrant/.composer
sudo chown -R vagrant:vagrant /home/vagrant/.composer
fi
}
wpcli_install() {
mkdir -p /home/vagrant/wp-cli
(
cd /home/vagrant/wp-cli
curl -O -s -S https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
curl -O -s -S https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
)
printf "\n%s\n" "source /home/vagrant/wp-cli/wp-completion.bash" >> .bash_profile
}
wpcli_error_handler() {
printf "WordPress files seem to already be present here. Moving on...\n"
exit 0
}
wordpress() {
local parser
parser=$(jq -r 'if .Project.wordpress then .Project.wordpress|to_entries|map("\(.key)=\(.value|tostring)")| .[] else empty end' /vagrant/wp-scratch-box.json)
declare -A wp_custom
while IFS="=" read -r key value; do
wp_custom[$key]="$value";
done < <(echo "$parser")
local root_directory=${wp_custom[root_directory]:-public_html}
local core_directory=${wp_custom[core_directory]:-.}
local mysql_database=${wp_custom[mysql_database]:-wp_dummy}
local mysql_user=${wp_custom[mysql_user]:-wp}
local mysql_password=${wp_custom[mysql_password]:-wp}
local mysql_prefix=${wp_custom[mysql_prefix]:-wp_}
mysql -u root -e "CREATE DATABASE IF NOT EXISTS $mysql_database;"
mysql -u root -e "GRANT ALL PRIVILEGES ON $mysql_database.* TO $mysql_user@localhost IDENTIFIED BY '$mysql_password';"
mkdir -p "/var/www/$root_directory"
(
trap 'wpcli_error_handler' ERR
cd "/var/www/$root_directory"
wp cli version
wp core download --path="$core_directory/" 2> /dev/null
wp core config --path="$core_directory/" --dbname="$mysql_database" --dbuser="$mysql_user" --dbpass="$mysql_password" --dbprefix="$mysql_prefix"
wp core version --path="$core_directory/" --extra
)
}
main
printf "\n========\n"
printf "\n\tProvisioning script finished without errors.\n"
printf "\tVisit http://192.168.56.4 for the 5 minute install!\n"
printf "\tIf you would like to access the site from a custom URL, add it to your hosts file.\n"
printf "\t\tHave a pleasant view!\n"
echo " "