Certainly! Below is a bash script that automates the installation of Docker and Docker Compose, sets up a WordPress site with a MariaDB database, and configures Caddy as a reverse proxy with automatic HTTPS using Let's Encrypt.
Please Note: This script is intended for Ubuntu/Debian-based systems. If you're using a different Linux distribution, you may need to adjust some commands.
Script: setup_wordpress_caddy.sh
#!/bin/bash
# Exit script on any error
set -e
# Update package lists
echo "Updating package lists..."
sudo apt-get update
# Install prerequisites
echo "Installing prerequisite packages..."
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
# Add Docker's GPG key
echo "Adding Docker's official GPG key..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add Docker's APT repository
echo "Adding Docker's APT repository..."
sudo add-apt-repository \
"deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
# Install Docker Engine
echo "Installing Docker Engine..."
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# Install Docker Compose
echo "Installing Docker Compose..."
DOCKER_COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)
sudo curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Set permissions for Docker Compose
sudo chmod +x /usr/local/bin/docker-compose
# Confirm installations
echo "Docker version:"
docker --version
echo "Docker Compose version:"
docker-compose --version
# Create a project directory
echo "Setting up project directory..."
mkdir -p ~/wordpress_caddy && cd ~/wordpress_caddy
# Create docker-compose.yml
echo "Creating docker-compose.yml..."
cat <<'EOF' > docker-compose.yml
version: '3.8'
services:
db:
image: mariadb:latest
container_name: wordpress_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_db_root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: your_db_user_password
volumes:
- db_data:/var/lib/mysql
wordpress:
image: wordpress:latest
container_name: wordpress_app
depends_on:
- db
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress_user
WORDPRESS_DB_PASSWORD: your_db_user_password
WORDPRESS_DB_NAME: wordpress
volumes:
- wp_data:/var/www/html
caddy:
image: caddy:latest
container_name: caddy_server
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
depends_on:
- wordpress
volumes:
db_data:
wp_data:
caddy_data:
caddy_config:
EOF
# Prompt user to set environment variables
echo "Updating environment variables with your credentials..."
sed -i 's/your_db_root_password/StrongRootPassw0rd/' docker-compose.yml
sed -i 's/your_db_user_password/StrongUserPassw0rd/' docker-compose.yml
# Create Caddyfile
echo "Creating Caddyfile..."
cat <<'EOF' > Caddyfile
yourdomain.com {
encode zstd gzip
reverse_proxy wordpress:80
}
EOF
# Prompt user to update the Caddyfile
echo "Please update the Caddyfile with your domain name."
read -p "Press Enter to open the Caddyfile in nano editor..."
nano Caddyfile
# Start Docker services
echo "Starting Docker containers..."
sudo docker-compose up -d
echo "Setup complete! Your WordPress site should be accessible at your domain."
Instructions to Use the Script
-
Save the Script
Save the script above to a file named setup_wordpress_caddy.sh:
nano setup_wordpress_caddy.sh
Paste the script content into the editor, then save and exit (Ctrl+O, Enter, Ctrl+X).
-
Make the Script Executable
chmod +x setup_wordpress_caddy.sh
-
Run the Script
./setup_wordpress_caddy.sh
-
Update the Caddyfile
When prompted, the script will open the Caddyfile using nano. Replace yourdomain.com with your actual domain name.
example.com {
encode zstd gzip
reverse_proxy wordpress:80
}
Save and exit the editor (Ctrl+O, Enter, Ctrl+X).
-
Access Your WordPress Site
- Ensure your domain's DNS records are correctly pointed to your server's IP address.
- Open a web browser and navigate to your domain to finish setting up WordPress.
Understanding the Script
Security Considerations
-
Passwords
- Replace
StrongRootPassw0rd and StrongUserPassw0rd with your own secure passwords.
- Use a password manager to generate and store strong passwords.
-
Domain and SSL
- Ensure your domain name is correctly configured in DNS to point to your server.
- Caddy will automatically obtain SSL certificates from Let's Encrypt.
-
Firewall Settings
Troubleshooting
Additional Recommendations
-
Regular Updates
-
Backups
- Regularly back up your database and WordPress content.
- Consider adding backup services or volumes in your
docker-compose.yml.
-
Customization
- Customize the
docker-compose.yml and Caddyfile to suit your needs.
- You can add additional Caddy configurations for caching, headers, etc.
Need Further Assistance?
Feel free to ask if you have any questions or need help with customizing the setup!