Skip to content

Commit b01ee24

Browse files
Merge pull request #13 from creativecommons/playbook-basic
Create an ansible playbook to configure wordpress over apache2
2 parents b3e4f82 + d68f59c commit b01ee24

File tree

13 files changed

+128
-50
lines changed

13 files changed

+128
-50
lines changed

ansible/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ RUN python3 -m venv /opt/ansible-venv --system-site-packages && \
4646

4747
# Create a directory for Ansible configuration
4848
RUN mkdir /etc/ansible/
49+
WORKDIR /etc/ansible/
4950

5051
# Set environment variables for Ansible
5152
ENV PATH="/opt/ansible-venv/bin:$PATH"
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
[defaults]
22
inventory = /etc/ansible/hosts
33
remote_user = sysadmin
4-
host_key_checking = True
4+
host_key_checking = False
55
retry_files_enabled = False
66
private_key_file = /home/sysadmin/.ssh/id_rsa
77

8+
[privilege_escalation]
9+
become=True
10+
become_password=''
11+
812
[web:vars]
9-
ansible_python_interpreter=/usr/bin/python3
13+
ansible_python_interpreter=/usr/bin/python3
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
- name: Add Apache2's www-data user to sudo group
2+
user:
3+
name: www-data
4+
groups: sudo
5+
append: yes
6+
7+
- name: Enable passwordless startup for www-data
8+
copy:
9+
dest: /etc/sudoers.d/www-data_startupservice
10+
content: "www-data ALL=(ALL) NOPASSWD:ALL"
11+
mode: '0440' # Correct mode for sudoers file
12+
13+
- name: Ensure wp directory exists and has correct permissions
14+
file:
15+
path: /usr/local/bin/wp
16+
state: directory
17+
owner: www-data
18+
group: www-data
19+
mode: '0755'
20+
21+
- name: Install WordPress CLI (WP-CLI)
22+
get_url:
23+
url: https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
24+
dest: /usr/local/bin/wp
25+
mode: '0755'
26+
27+
28+
- name: Create WP-CLI directory for www-data
29+
file:
30+
path: /var/www/.wp-cli
31+
state: directory
32+
owner: www-data
33+
group: www-data
34+
mode: '0755'
35+
36+
- name: Create the WordPress directory and set permissions
37+
file:
38+
path: /var/www/dev/wp-content/uploads
39+
state: directory
40+
owner: www-data
41+
group: www-data
42+
mode: '0755'
43+
44+
- name: Check Wordpress installed
45+
stat:
46+
path: /var/www/dev/wp-config.php
47+
register: wp_config
48+
49+
- name: Use WP_CLI to install WordPress
50+
shell: sudo -u www-data /usr/local/bin/wp/wp-cli.phar core download --version={{wp_version}}
51+
when: not wp_config.stat.exists
52+
args:
53+
chdir: /var/www/dev
54+
vars:
55+
wp_version: 6.3.4
56+
57+
# Download wp-config-docker.php for use as wp-config.php
58+
- name: WordPress basic configuration
59+
get_url:
60+
url: https://raw.githubusercontent.com/docker-library/wordpress/master/latest/php8.2/apache/wp-config-docker.php
61+
dest: /var/www/dev/wp-config.php
62+
owner: www-data
63+
group: www-data
64+
mode: '0644'
65+
66+
- name: Use awk to replace all instances of "put your unique phrase here" with a unique string
67+
shell: |
68+
awk '
69+
/put your unique phrase here/ {
70+
cmd = "head -c1m /dev/urandom | sha1sum | cut -d\\\\ -f1";
71+
cmd | getline str;
72+
close(cmd);
73+
gsub("put your unique phrase here", str);
74+
}
75+
{ print }
76+
' /var/www/dev/wp-config.php > /var/www/dev/wp-config.tmp \
77+
&& mv /var/www/dev/wp-config.tmp /var/www/dev/wp-config.php
78+
args:
79+
chdir: /var/www/dev
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- hosts: web
2+
roles:
3+
- wordpress

db/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ RUN mkdir -p /run/sshd
3030

3131
# Expose SSH port
3232
EXPOSE 22
33+
EXPOSE 3306
3334

34-
# Start SSH service
35-
CMD ["/usr/sbin/sshd", "-D"]
35+
# Add mariadb service startup script
36+
COPY ./db/startupservice.sh /startupservice.sh
37+
RUN chmod +x /startupservice.sh
38+
39+
ENTRYPOINT ["/startupservice.sh"]
40+
CMD ["mariadbd"]

db/startupservice.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set -o errexit
3+
set -o nounset
4+
5+
# https://en.wikipedia.org/wiki/ANSI_escape_code
6+
E0="$(printf "\e[0m")" # reset
7+
E1="$(printf "\e[1m")" # bold
8+
9+
echo "${E1}Starting mariadb: http://127.0.0.1:3306${E0}"
10+
11+
# Start mariadb in the background
12+
docker-entrypoint.sh "$@"
13+
14+
# Start SSH service
15+
/usr/sbin/sshd -D

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ services:
5757
- '22002:22'
5858
restart: on-failure
5959
volumes:
60+
- ./web/etc-apache2-sites-available:/etc/apache2/sites-available
6061
- wp-data:/var/www/dev
6162
- ./sysadmin-ssh-keys/rsa_sysadmin:/home/sysadmin/.ssh/id_rsa:ro
6263
- ./sysadmin-ssh-keys/rsa_sysadmin.pub:/home/sysadmin/.ssh/id_rsa.pub:ro

web/Dockerfile

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# https://docs.docker.com/engine/reference/builder/
2-
# https://hub.docker.com/_/debian
1+
# Use the Debian Bookworm Slim base image
32
FROM debian:bookworm-slim
43

54
# Configure apt not to prompt during docker build
@@ -47,6 +46,9 @@ RUN useradd -m -s /bin/bash sysadmin && \
4746
echo "sysadmin:sysadmin" | chpasswd && \
4847
usermod -aG sudo sysadmin
4948

49+
# Copy the sudoers file for sysadmin user to the appropriate directory
50+
COPY ./web/etc-sudoers.d/sysadmin_all_nopass /etc/sudoers.d/sysadmin_all_nopass
51+
5052
# Ensure SSH directory exists with correct permissions
5153
RUN mkdir -p /home/sysadmin/.ssh && \
5254
chown sysadmin:sysadmin /home/sysadmin/.ssh && \
@@ -57,10 +59,10 @@ RUN mkdir -p /run/sshd
5759

5860
# Add Apache2's www-data user to sudo group and enable passwordless startup
5961
RUN adduser www-data sudo
60-
COPY web/config-web/www-data_startupservice /etc/sudoers.d/www-data_startupservice
62+
COPY ./web/etc-sudoers.d/www-data_startupservice /etc/sudoers.d/www-data_startupservice
6163

6264
# Add Apache2 service startup script
63-
COPY web/config-web/startupservice.sh /startupservice.sh
65+
COPY ./web/startupservice.sh /startupservice.sh
6466
RUN chmod +x /startupservice.sh
6567
CMD ["sudo", "--preserve-env", "/startupservice.sh"]
6668

@@ -76,50 +78,13 @@ RUN a2enmod php8.2
7678
RUN a2enmod rewrite
7779

7880
# Configure PHP
79-
COPY web/config-web/90-local.ini /etc/php/8.2/apache2/conf.d/
80-
81-
# Install WordPress CLI (WP-CLI)
82-
# https://wp-cli.org/#installing
83-
RUN curl -L \
84-
https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
85-
-o wp-cli.phar \
86-
&& chmod +x wp-cli.phar \
87-
&& mv wp-cli.phar /usr/local/bin/wp
81+
COPY ./web/etc-php-8.2-apache2-conf.d/90-local.ini /etc/php/8.2/apache2/conf.d/
8882

89-
# Create WP-CLI directory for www-data
90-
RUN mkdir /var/www/.wp-cli
91-
RUN chown -R www-data:www-data /var/www/.wp-cli
83+
# Create the wp directory and set permissions
84+
RUN mkdir -p /usr/local/bin/wp/
85+
RUN chown -R www-data:www-data /usr/local/bin/wp/
9286

9387
# Create the dev directory and set permissions
9488
RUN mkdir -p /var/www/dev/wp-content/uploads
9589
RUN chown -R www-data:www-data /var/www/dev
9690

97-
# Use WP-CLI to install WordPress
98-
USER www-data
99-
WORKDIR /var/www/dev
100-
ARG WP_VERSION
101-
RUN wp core download --version=$WP_VERSION
102-
103-
# Add WordPress basic configuration
104-
# 1) Download wp-config-docker.php for use as wp-config.php. Friendly view at:
105-
# https://github.com/docker-library/wordpress/blob/master/latest/php8.2/apache/wp-config-docker.php
106-
RUN curl -L \
107-
https://raw.githubusercontent.com/docker-library/wordpress/master/latest/php8.2/apache/wp-config-docker.php \
108-
-o /var/www/dev/wp-config.php
109-
110-
# 2) Use awk to replace all instances of "put your unique phrase here" with a
111-
# properly unique string (for AUTH_KEY and friends to have safe defaults if
112-
# they aren't specified with environment variables)
113-
# Based on:
114-
# https://github.com/docker-library/wordpress/blob/master/latest/php8.2/apache/docker-entrypoint.sh
115-
RUN awk ' \
116-
/put your unique phrase here/ { \
117-
cmd = "head -c1m /dev/urandom | sha1sum | cut -d\\ -f1"; \
118-
cmd | getline str; \
119-
close(cmd); \
120-
gsub("put your unique phrase here", str); \
121-
} \
122-
{ print } \
123-
' /var/www/dev/wp-config.php > /var/www/dev/wp-config.tmp \
124-
&& mv /var/www/dev/wp-config.tmp /var/www/dev/wp-config.php
125-

web/config-web/etc-apache2-sites-available/000-default.conf renamed to web/etc-apache2-sites-available/000-default.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ ServerName localhost:8080
7171

7272
</VirtualHost>
7373

74-
# vim: ft=apache ts=4 sw=4 sts=4 sr et
74+
# vim: ft=apache ts=4 sw=4 sts=4 sr et
File renamed without changes.

0 commit comments

Comments
 (0)