Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

I need to install a secure production ready wordpress environment using caddy can you…

  1. User

    I need to install a secure production ready wordpress environment using caddy can you please provide a script or commands for this on a debian 12 vm

  2. Assistant

    Certainly! Below is a step-by-step guide to install a secure, production-ready WordPress environment using Caddy on a Debian 12 VM. This guide includes all the necessary commands and explanations to help you set up the environment efficiently.


    Prerequisites

    • A Debian 12 VM with sudo privileges.
    • A domain name pointed to your server's public IP address.
    • Basic familiarity with command-line operations.

    Step 1: Update the System

    Keep your system packages up-to-date:

    sudo apt update && sudo apt upgrade -y
    

    Step 2: Install Required Packages

    Install essential packages:

    sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https ufw
    

    Step 3: Install and Secure MariaDB

    Install MariaDB Server

    sudo apt install -y mariadb-server
    

    Secure MariaDB Installation

    sudo mysql_secure_installation
    
    • Set root password? Yes
    • Remove anonymous users? Yes
    • Disallow root login remotely? Yes
    • Remove test database and access to it? Yes
    • Reload privilege tables now? Yes

    Create Database and User for WordPress

    Log in to MariaDB:

    sudo mysql -u root -p
    

    Enter the MariaDB root password you just set.

    Create a database and user:

    CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strong_password_here';
    GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    

    Note: Replace 'strong_password_here' with a secure password.


    Step 4: Install PHP and Extensions

    sudo apt install -y php-fpm php-mysql php-curl php-gd php-xml php-mbstring
    

    Step 5: Install Caddy Web Server

    Add Caddy Repository

    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
    

    Install Caddy

    sudo apt update
    sudo apt install -y caddy
    

    Step 6: Configure PHP-FPM

    Ensure PHP-FPM is listening on a Unix socket.

    Edit the PHP-FPM pool configuration file:

    sudo nano /etc/php/8.2/fpm/pool.d/www.conf
    

    Note: Replace 8.2 with your PHP version if different.

    Find the listen directive and set it to:

    listen = /run/php/php8.2-fpm.sock
    

    Save and exit the editor.

    Restart PHP-FPM:

    sudo systemctl restart php8.2-fpm
    

    Step 7: Download and Configure WordPress

    Download WordPress

    Navigate to the web root directory:

    sudo mkdir -p /var/www
    cd /var/www
    

    Download the latest WordPress package:

    sudo wget https://wordpress.org/latest.tar.gz
    

    Extract the files:

    sudo tar -xzvf latest.tar.gz
    

    Set proper ownership:

    sudo chown -R www-data:www-data wordpress
    

    Step 8: Configure Caddy

    Create a new Caddyfile:

    sudo nano /etc/caddy/Caddyfile
    

    Add the following configuration:

    your_domain.com {
        root * /var/www/wordpress
        php_fastcgi unix//run/php/php8.2-fpm.sock
        encode gzip
        file_server
    
        header {
            Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
            X-Content-Type-Options "nosniff"
            X-Frame-Options "DENY"
            Referrer-Policy "no-referrer-when-downgrade"
            Content-Security-Policy "default-src 'self';"
            Permissions-Policy "geolocation=(), microphone=(), camera=()"
        }
    }
    

    Note: Replace your_domain.com with your actual domain name. Ensure the PHP socket path matches your PHP version.

    Save and exit the editor.


    Step 9: Adjust Firewall Settings

    Allow necessary services through the firewall:

    sudo ufw allow 'OpenSSH'
    sudo ufw allow 'HTTP'
    sudo ufw allow 'HTTPS'
    sudo ufw enable
    

    Confirm firewall status:

    sudo ufw status
    

    Step 10: Restart and Enable Services

    Restart Caddy to apply the new configuration:

    sudo systemctl restart caddy
    

    Enable Caddy to start on boot:

    sudo systemctl enable caddy
    

    Ensure PHP-FPM is running:

    sudo systemctl status php8.2-fpm
    

    Step 11: Complete WordPress Setup via Web Browser

    Open your web browser and navigate to https://your_domain.com.

    • Select Language: Choose your preferred language.
    • Database Details:
      • Database Name: wordpress
      • Username: wpuser
      • Password: The password you set earlier.
      • Database Host: localhost
      • Table Prefix: wp_ (or customize if desired)
    • Run the Installation: Follow the prompts to set up your WordPress site title and admin account.

    Additional Security Measures

    • Regular Updates: Keep your system and applications updated.
    • Strong Passwords: Use complex passwords for all accounts.
    • SSL/TLS Encryption: Caddy automatically handles SSL certificates via Let's Encrypt.
    • File Permissions: Verify file permissions to prevent unauthorized access.
      • Folders: 755
      • Files: 644
    • Limit Login Attempts: Consider using a plugin to prevent brute-force attacks.
    • Disable XML-RPC if Unused: Disable if not required to reduce attack surface.

    Optional: Automate Installation with a Script

    Below is a bash script that automates the above steps. Review and modify the variables before running.

    #!/bin/bash
    
    # Variables - Modify these
    DB_NAME="wordpress"
    DB_USER="wpuser"
    DB_PASS="strong_password_here"
    DOMAIN="your_domain.com"
    PHP_VER="8.2"
    
    # Update and upgrade the system
    sudo apt update && sudo apt upgrade -y
    
    # Install required packages
    sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https ufw
    
    # Install MariaDB and secure installation
    sudo apt install -y mariadb-server
    sudo mysql_secure_installation
    
    # Create WordPress database and user
    sudo mysql -u root <<EOF
    CREATE DATABASE ${DB_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
    GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';
    FLUSH PRIVILEGES;
    EOF
    
    # Install PHP and extensions
    sudo apt install -y php-fpm php-mysql php-curl php-gd php-xml php-mbstring
    
    # Install Caddy
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo apt-key add -
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
    sudo apt update
    sudo apt install -y caddy
    
    # Configure PHP-FPM
    sudo sed -i "s|listen = .*|listen = /run/php/php${PHP_VER}-fpm.sock|" /etc/php/${PHP_VER}/fpm/pool.d/www.conf
    sudo systemctl restart php${PHP_VER}-fpm
    
    # Download and configure WordPress
    sudo mkdir -p /var/www
    cd /var/www
    sudo wget https://wordpress.org/latest.tar.gz
    sudo tar -xzvf latest.tar.gz
    sudo chown -R www-data:www-data wordpress
    
    # Configure Caddy
    sudo bash -c "cat > /etc/caddy/Caddyfile" <<EOL
    ${DOMAIN} {
        root * /var/www/wordpress
        php_fastcgi unix//run/php/php${PHP_VER}-fpm.sock
        encode gzip
        file_server
    
        header {
            Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
            X-Content-Type-Options "nosniff"
            X-Frame-Options "DENY"
            Referrer-Policy "no-referrer-when-downgrade"
            Content-Security-Policy "default-src 'self';"
            Permissions-Policy "geolocation=(), microphone=(), camera=()"
        }
    }
    EOL
    
    # Adjust firewall settings
    sudo ufw allow 'OpenSSH'
    sudo ufw allow 'HTTP'
    sudo ufw allow 'HTTPS'
    sudo ufw --force enable
    
    # Restart and enable services
    sudo systemctl restart caddy
    sudo systemctl enable caddy
    sudo systemctl restart php${PHP_VER}-fpm
    
    echo "Installation complete! Navigate to https://${DOMAIN} to complete the WordPress setup."
    

    Important:

    • Replace variables at the top of the script with your actual database credentials, domain name, and PHP version.
    • Ensure that you have pointed your domain's DNS records to your server's IP address.

    Final Notes

    • Backup Regularly: Implement a regular backup strategy for your WordPress files and database.
    • Monitoring: Consider setting up system and application monitoring.
    • Plugins and Themes: Only install plugins and themes from reputable sources.
    • Security Plugins: Utilize security plugins like Wordfence or Sucuri for additional protection.

    If you encounter any issues during installation or have further questions, feel free to ask for assistance!