-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpdock
289 lines (248 loc) · 9.82 KB
/
wpdock
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
#!/bin/bash
# @see https://github.com/wpgitupdater/wpdock
RED='\033[0;31m'
NC='\033[0m'
read -r -d '' ENV_CONTENT << CONTENT
COMPOSE_PROJECT_NAME=wordpress
WORDPRESS_SITE_HOST=wordpress.wpdock
WORDPRESS_PORT=8080
PMA_PORT=8181
MAILHOG_PORT=8282
MYSQL_PORT=33060
WORDPRESS_SITE_TITLE="WP Dock Environment"
WORDPRESS_ADMIN_USERNAME=test
WORDPRESS_ADMIN_PASSWORD=test
WORDPRESS_IMAGE=wordpress:latest
WORDPRESS_CLI_IMAGE=wordpress:cli
MYSQL_IMAGE=mysql:5.7
WORDPRESS_DB_HOST=db:3306
WORDPRESS_DB_NAME=wordpress
WORDPRESS_DB_USER=wordpress
WORDPRESS_DB_PASSWORD=wordpress
WORDPRESS_TABLE_PREFIX=wp_
WORDPRESS_DEBUG=true
MYSQL_ROOT_PASSWORD=password
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress
MYSQL_DATABASE=wordpress
CONTENT
read -r -d '' COMPOSE_CONTENT << CONTENT
version: "3.3"
services:
wordpress:
image: \${WORDPRESS_IMAGE:-wordpress:latest}
container_name: \${COMPOSE_PROJECT_NAME:-wordpress}_wordpress
restart: always
depends_on:
- db
env_file: .env
ports:
- \${WORDPRESS_PORT:-8080}:80
volumes:
- wp:/var/www/html
- ./:/var/www/html/wp-content
extra_hosts:
- \${WORDPRESS_SITE_HOST:-wordpress.wpdock}:172.17.0.1
install:
image: \${WORDPRESS_CLI_IMAGE:-wordpress:cli}
container_name: \${COMPOSE_PROJECT_NAME:-wordpress}_install
restart: on-failure
user: "33:33"
depends_on:
- db
- wordpress
env_file: .env
volumes:
- wp:/var/www/html
working_dir: /var/www/html
command: wp core install --url="localhost:\${WORDPRESS_PORT:-8080}" --title="\${WORDPRESS_SITE_TITLE:-"WP Dock Environment"}" --admin_user=\${WORDPRESS_ADMIN_USERNAME:-test} --admin_password=\${WORDPRESS_ADMIN_PASSWORD:-test} --admin_email=\${WORDPRESS_ADMIN_EMAIL:[email protected]}
cli:
image: \${WORDPRESS_CLI_IMAGE:-wordpress:cli}
container_name: \${COMPOSE_PROJECT_NAME:-wordpress}_cli
user: "33:33"
depends_on:
- db
- wordpress
env_file: .env
volumes:
- wp:/var/www/html
- ./:/var/www/html/wp-content
working_dir: /var/www/html
command: wp --info
db:
image: \${MYSQL_IMAGE:-mysql:5.7}
container_name: \${COMPOSE_PROJECT_NAME:-wordpress}_db
restart: always
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
env_file: .env
ports:
- \${MYSQL_PORT:-33060}:3306
volumes:
- db:/var/lib/mysql
phpmyadmin:
image: phpmyadmin:latest
restart: always
depends_on:
- db
ports:
- \${PMA_PORT:-8181}:80
environment:
- PMA_USER=root
- PMA_PASSWORD=\${MYSQL_ROOT_PASSWORD}
mailhog:
container_name: \${COMPOSE_PROJECT_NAME:-wordpress}_mailhog
image: mailhog/mailhog
ports:
- \${MAILHOG_PORT:-8282}:8025
- 10255:1025
caddy:
container_name: \${COMPOSE_PROJECT_NAME:-wordpress}_caddy
image: caddy:latest
volumes:
- ./.wpdock/certs:/root/certs
- ./.wpdock/Caddyfile:/etc/caddy/Caddyfile
ports:
- 80:80
- 443:443
depends_on:
- wordpress
volumes:
wp:
db:
CONTENT
if [[ -f ".env" ]]; then
source .env
fi
composefile="$HOME/.wpdock-docker-compose.v1.yml"
case $1 in
help|--help)
echo -e "\n\n"
echo -e "Available Commands:"
echo -e "\n\ninit\n\nCreates '.env' file in the current folder when not present.\nAdds 'db.sql' and '.wpdock' to '.gitignore', and '.htaccess' rules to prevent dot files being accessed."
echo -e "\n\nfile [name](.env)\n\nDisplays the contents of the generated files should you prefer a manual installation."
echo -e "\n\nup|start --https\n\nRuns 'docker-compose up -d' and applies $(basename $0) specific configurations. When --https is provided a caddy server is used to proxy requests from your WORDPRESS_SITE_HOST env to the wordpress container."
echo -e "\n\nstop\n\nRuns 'docker-compose stop'."
echo -e "\n\ndown\n\nRuns 'docker-compose down'."
echo -e "\n\ndestroy\n\nRuns 'docker-compose down -v' Removing volumes."
echo -e "\n\nexec [name] ...\n\nProxies commands to the named container using 'docker-compose exec name ...'.\nFor example: "$(basename $0)" exec wordpress sh opens a session on the wordpress container."
echo -e "\n\nrun [name] ...\n\nProxies commands to the named container using 'docker-compose run name ...'.\nFor example: "$(basename $0)" run wordpress sh opens a session on a new copy of the wordpress container."
echo -e "\n\nlogs [name]\n\nTails container logs. When no name is provided output contains all running containers."
echo -e "\n\ncompose ...\n\nProxies commands directly to docker-compose."
echo -e "\n\ndump\n\nExports the database to 'dump.sql' using wp db export and the database root user."
echo -e "\n\nimport\n\nImports the database from the 'dump.sql' file."
echo -e "\n\n...\n\nProxies commands to the wpcli container 'docker-compose run --rm cli wp ...'.\nFor example: "$(basename $0)" user list >> wpcli user list"
echo -e "\n\n"
;;
init)
if [[ -f ".env" ]]; then
echo -e "$RED** .env already exists **$NC\n"
echo -e "Review the contents of the 'file .env' command\n\n"
else
echo -e "Creating .env file.\n\n"
echo "$ENV_CONTENT" > ".env"
fi
if ! [[ -f ".gitignore" ]] || ! grep -q "db.sql" ".gitignore"; then
echo -e "Adding db.sql to .gitignore file.\n\n"
echo "db.sql" >> ".gitignore"
fi
if ! [[ -f ".gitignore" ]] || ! grep -q ".wpdock" ".gitignore"; then
echo -e "Adding .wpdock to .gitignore file.\n\n"
echo ".wpdock" >> ".gitignore"
fi
if ! [[ -f ".htaccess" ]] || ! grep -q "<FilesMatch \"^\\\.\">" ".htaccess"; then
echo -e "Adding dot file access prevention to .htaccess file.\n\n"
echo -e "<FilesMatch \"^\.\">\nOrder allow,deny\nDeny from all\n</FilesMatch>" >> ".htaccess"
else
echo -e "Dot file access prevention already added to .htaccess file.\n\n"
fi
;;
file)
if [[ "$2" == ".env" ]]; then
printf "$ENV_CONTENT"
fi
;;
up|start)
echo "$COMPOSE_CONTENT" > $composefile
echo "Starting services..."
$(docker-compose -f $composefile --project-directory $PWD up -d install mailhog phpmyadmin) || exit $?
echo "Updating mailhog configuration..."
docker-compose -f $composefile --project-directory $PWD exec wordpress bash -c "
if ! [[ -f /usr/local/bin/mhsendmail ]]; then
curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64
chmod +x /usr/local/bin/mhsendmail
echo 'sendmail_path=\"/usr/local/bin/mhsendmail --smtp-addr=mailhog:1025\"' >> /usr/local/etc/php/conf.d/mailhog.ini
echo 'sendmail_from=\"${WORDPRESS_ADMIN_EMAIL:-noreply@wpdock.local}\"' >> /usr/local/etc/php/conf.d/mailhog.ini
service apache2 restart
fi
"
if [[ "$2" == "--https" ]]; then
host="${WORDPRESS_SITE_HOST:-${COMPOSE_PROJECT_NAME:-wordpress}.wpdock}"
mkdir -p ".wpdock/certs"
touch ".wpdock/Caddyfile"
if ! [[ -f ".wpdock/certs/$host.pem" ]]; then
echo "Generating certificates..."
mkcert --cert-file=.wpdock/certs/$host.pem --key-file=.wpdock/certs/$host-key.pem $host
fi
echo "Updating Caddyfile..."
echo -e "$host {\n\ttls /root/certs/$host.pem /root/certs/$host-key.pem\n\treverse_proxy wordpress:80\n}" > ".wpdock/Caddyfile"
echo "Starting Caddy Proxy Container..."
$(docker-compose -f $composefile --project-directory $PWD up -d caddy) || exit $?
if ! grep -q "$host" "/etc/hosts"; then
echo -e "\n\n$host doesnt appear in your hosts file, you may need to add it:"
echo "127.0.0.1 $host"
fi
for i in {1..10}; do
docker-compose -f $composefile --project-directory $PWD run --rm cli wp option update home "https://$host" 2> /dev/null && break || sleep $i
done
docker-compose -f $composefile --project-directory $PWD run --rm cli wp option update siteurl "https://$host"
docker-compose -f $composefile --project-directory $PWD run --rm cli wp search-replace "http://localhost:${WORDPRESS_PORT:-8080}" "https://$host" --skip-columns=guid
echo -e "\n\nWordPress: http://localhost:${WORDPRESS_PORT:-8080} | https://$host"
else
echo -e "\n\nWordPress: http://localhost:${WORDPRESS_PORT:-8080}"
fi
echo "PHPMyAdmin: http://localhost:${PMA_PORT:-8181}"
echo "Mailhog: http://localhost:${MAILHOG_PORT:-8282}"
echo "MySQL: localhost:${MYSQL_PORT:-33060}"
;;
stop)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD stop
;;
down)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD down
;;
destroy)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD down -v
;;
exec)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD exec ${@:2}
;;
run)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD run --rm ${@:2}
;;
logs)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD logs -f ${@:2}
;;
compose)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD ${@:2}
;;
dump)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD run --rm cli /bin/sh -c "wp db export 'wp-content/dump.sql' --dbuser=root --dbpass=\$MYSQL_ROOT_PASSWORD"
;;
import)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD run --rm cli wp db import 'wp-content/dump.sql'
;;
*)
echo "$COMPOSE_CONTENT" > $composefile
docker-compose -f $composefile --project-directory $PWD run --rm cli wp ${@:1}
;;
esac