Skip to content

Commit bf0f956

Browse files
authored
PR #6 from psaux-it/hosts
fix_hosts
2 parents a82d7df + 88cbee1 commit bf0f956

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

wordpress/entrypoint-wp.sh

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ for var in \
4444
NPP_GID \
4545
NPP_NGINX_CACHE_PATH \
4646
NPP_USER \
47-
NPP_HTTP_HOST \
48-
NPP_NGINX_IP \
4947
NPP_DEV_ENABLED \
5048
MOUNT_DIR; do
5149
if [[ -z "${!var:-}" ]]; then
@@ -95,39 +93,6 @@ chown -R root:root \
9593
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Permissions fixed successfully!" ||
9694
echo -e "${COLOR_RED}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Failed to fix permissions!"
9795

98-
# To enable NPP - Nginx Cache Preload action:
99-
# #####################################################################
100-
# For Development Environment:
101-
# - Cause HTTP_HOST is localhost,
102-
# - Map the WordPress container's 'localhost' to Nginx's IP.
103-
# - Note: This is a tricky hack and only used for the development environment!
104-
#
105-
# For Production Environments: (Nginx sits on host or container)
106-
# - I assume you use a publicly resolvable FQDN for WordPress (WP_SITEURL & WP_HOME);
107-
# - Ensure outgoing traffic is allowed from the container.
108-
# - Verify that /etc/resolv.conf in the container is correctly configured.
109-
# - Verify that the container has internet access.
110-
# + That's all for Cache Preload works like a charm.
111-
#######################################################################
112-
if [[ "${NPP_DEV_ENABLED}" -eq 1 ]]; then
113-
IP="${NPP_NGINX_IP}"
114-
LINE="${IP} ${NPP_HTTP_HOST}"
115-
HOSTS="/etc/hosts"
116-
117-
# Hack the hosts file
118-
chmod 644 "${HOSTS}"
119-
120-
# Check if the Nginx static IP defined
121-
if ! grep -q "${IP}" "${HOSTS}"; then
122-
# Map localhost to Nginx Static IP
123-
sed -i "1i${LINE}" "${HOSTS}"
124-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Mapped '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET}' to Nginx static IP '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET}' in ${COLOR_LIGHT_CYAN}${HOSTS}${COLOR_RESET}."
125-
else
126-
echo -e "${COLOR_YELLOW}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Mapping already exists: '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET}' -> '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET}'."
127-
fi
128-
fi
129-
#######################################################################
130-
13196
# Wait for the 'wordpress-db' to be ready
13297
until mysql -h wordpress-db -u"${WORDPRESS_DB_USER}" -p"${WORDPRESS_DB_PASSWORD}" "${WORDPRESS_DB_NAME}" -e "SELECT 1" > /dev/null 2>&1; do
13398
echo -e "${COLOR_YELLOW}${COLOR_BOLD}NPP-WP:${COLOR_RESET} The ${COLOR_LIGHT_CYAN}MySQL database${COLOR_RESET} is not available yet. Retrying..."

wordpress/wp-cli.sh

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,28 @@ wait_for_service() {
4444

4545
while ! nc -z "${host}" "${port}"; do
4646
if [[ "${retries}" -le 0 ]]; then
47-
echo -e "${COLOR_RED}${COLOR_BOLD}NPP-WP-CLI-FATAL:${COLOR_RESET} ${COLOR_CYAN}${host}:${port}${COLOR_RESET} is not responding. Exiting..."
47+
echo -e "${COLOR_RED}${COLOR_BOLD}NPP-WP-FATAL:${COLOR_RESET} ${COLOR_CYAN}${host}:${port}${COLOR_RESET} is not responding. Exiting..."
4848
exit 1
4949
fi
50-
echo -e "${COLOR_YELLOW}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} Waiting for ${COLOR_CYAN}${host}:${port}${COLOR_RESET} to become available..."
50+
echo -e "${COLOR_YELLOW}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Waiting for ${COLOR_CYAN}${host}:${port}${COLOR_RESET} to become available..."
5151
sleep "$wait_time"
5252
retries=$((retries - 1))
5353
done
5454

55-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} ${COLOR_CYAN}${host}:${port}${COLOR_RESET} is now available! Proceeding..."
55+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} ${COLOR_CYAN}${host}:${port}${COLOR_RESET} is now available! Proceeding..."
5656
}
5757

5858
# Display pre-entrypoint start message
59-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} ${COLOR_CYAN}${COLOR_BOLD}[POST-START]:${COLOR_RESET} Initialization of ${COLOR_CYAN}Core WordPress${COLOR_RESET} has started.."
59+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} ${COLOR_CYAN}${COLOR_BOLD}[POST-START]:${COLOR_RESET} Starting post-start operations for ${COLOR_CYAN}NPP Dockerized${COLOR_RESET}..."
6060

6161
# Check if required environment variables are set
6262
for var in \
6363
NPP_USER \
6464
NPP_UID \
6565
NPP_GID \
6666
NPP_DEV_ENABLED \
67+
NPP_NGINX_IP \
68+
NPP_HTTP_HOST \
6769
NPP_DEV_PLUGIN_NAME \
6870
NPP_DEV_PLUGIN_DIR \
6971
NPP_DEV_TMP_CLONE_DIR \
@@ -88,6 +90,38 @@ done
8890
# We need to sure '/var/www/html' exists for 'wp-cli'
8991
wait_for_service "wordpress" 9001
9092

93+
# To enable NPP - Nginx Cache Preload action:
94+
# #####################################################################
95+
# For Development Environment:
96+
# - Cause HTTP_HOST is localhost,
97+
# - Map the WordPress container's 'localhost' to Nginx's IP.
98+
# - Note: This is a tricky hack and only used for the development environment!
99+
#
100+
# For Production Environments: (Nginx sits on host or container)
101+
# - I assume you use a publicly resolvable FQDN for WordPress (WP_SITEURL & WP_HOME);
102+
# - Ensure outgoing traffic is allowed from the container.
103+
# - Verify that /etc/resolv.conf in the container is correctly configured.
104+
# - Verify that the container has internet access.
105+
# + That's all for Cache Preload works like a charm.
106+
#######################################################################
107+
if [[ "${NPP_DEV_ENABLED}" -eq 1 ]]; then
108+
IP="${NPP_NGINX_IP}"
109+
LINE="${IP} ${NPP_HTTP_HOST}"
110+
HOSTS="/etc/hosts"
111+
112+
# Check if the Nginx static IP defined
113+
if ! grep -q "${IP}" "${HOSTS}"; then
114+
# Map localhost to Nginx Static IP
115+
echo -e "${LINE}\n$(cat ${HOSTS})" > /tmp/hosts.new
116+
cat /tmp/hosts.new > "${HOSTS}"
117+
rm -f /tmp/hosts.new
118+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Mapped '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET}' to Nginx IP '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET}' in ${COLOR_LIGHT_CYAN}${HOSTS}${COLOR_RESET}."
119+
else
120+
echo -e "${COLOR_YELLOW}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Mapping already exists: '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET}' -> '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET}'."
121+
fi
122+
fi
123+
#######################################################################
124+
91125
# Check ownership of webroot for consistency
92126
check_ownership() {
93127
while IFS=" " read -r owner group file; do
@@ -111,18 +145,18 @@ check_permissions() {
111145

112146
# Own website with Isolated PHP process owner user 'npp'
113147
if ! check_ownership; then
114-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} Setting ownership of ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} to user/group ${COLOR_LIGHT_CYAN}${NPP_USER}${COLOR_RESET} with UID ${COLOR_CYAN}${NPP_UID}${COLOR_RESET} and GID ${COLOR_CYAN}${NPP_GID}${COLOR_RESET}."
148+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Setting ownership of ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} to user/group ${COLOR_LIGHT_CYAN}${NPP_USER}${COLOR_RESET} with UID ${COLOR_CYAN}${NPP_UID}${COLOR_RESET} and GID ${COLOR_CYAN}${NPP_GID}${COLOR_RESET}."
115149
chown -R "${NPP_UID}":"${NPP_GID}" "${NPP_WEB_ROOT}"
116150
else
117-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} Ownership of ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} is already properly set."
151+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Ownership of ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} is already properly set."
118152
fi
119153

120154
# Set proper permission to restrict environment for 'others'
121155
if ! check_permissions; then
122-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} Setting permissions for ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} to completely isolate the environment."
156+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Setting permissions for ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} to completely isolate the environment."
123157
chmod -R u=rwX,g=rX,o= "${NPP_WEB_ROOT}"
124158
else
125-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} Permission for ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} is already properly set."
159+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Permission for ${COLOR_LIGHT_CYAN}${NPP_WEB_ROOT}${COLOR_RESET} is already properly set."
126160
fi
127161

128162
# Install core WordPress
@@ -321,7 +355,7 @@ if [[ "${NPP_DEV_ENABLED}" -eq 1 ]]; then
321355
fi
322356

323357
# Listen on dummy port for 'nginx' container health check
324-
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP-CLI:${COLOR_RESET} Starting to listen on dummy port ${COLOR_CYAN}9999${COLOR_RESET}..."
358+
echo -e "${COLOR_GREEN}${COLOR_BOLD}NPP-WP:${COLOR_RESET} Starting to listen on dummy port ${COLOR_CYAN}9999${COLOR_RESET}..."
325359
if ! nc -zv 127.0.0.1 9999 2>/dev/null; then
326360
nc -lk -p 9999 >/dev/null 2>&1 &
327361
fi

0 commit comments

Comments
 (0)