Solved Automated WordPress Installation on DirectAdmin script

Dwebb

Verified User
Joined
Mar 13, 2023
Messages
10
This automates the entire process of setting up a fresh WordPress installation upon account creation, when certain packages are selected.


This was created for web hosting providers that want be able to offer ordering of WordPress Hosting packages, so a customer has a seamless instant setup of not only their account but their entire base WordPress installation done for them.

Link to full article & setup (quite easy!)


The script, but please read the full article for explanation & prerequisites. This shouldn't take you more than maybe 5 minutes to setup.

Bash:
#!/bin/bash

# Constants
DIRECTADMIN_HOSTNAME="your.directadminurl.com"
DIRECTADMIN_PORT=2222
DIRECTADMIN_USERNAME="admin"
DIRECTADMIN_PASSWORD=$(cat /usr/local/directadmin/scripts/adminpassword) # assuming you stored your password in a secure file
DIRECTADMIN_HTTPS=true  # Set this to true if your DirectAdmin server uses HTTPS

# The protocol should be https if DIRECTADMIN_HTTPS is true, http otherwise
if [ "$DIRECTADMIN_HTTPS" = true ]; then
  PROTOCOL="https"
else
  PROTOCOL="http"
fi

# Directory to the user's public_html
USER_DIR=/home/${username}/domains/${domain}/public_html

# Admin username, password, and email
ADMIN_USER=${username} # use the username provided during account creation
ADMIN_PASSWORD=$(openssl rand -base64 12 | tr -dc 'a-zA-Z0-9') # use openssl to randomly generate a password
ADMIN_EMAIL=${email}

# Check the package name and install WordPress if it matches
if [[ "${package}" = "wordpress_10gb" ]] || [[ "${package}" = "wordpress_50gb" ]] || [[ "${package}" = "wordpress_100gb" ]]; then

# MySQL Database details
DB_NAME="wp"
DB_USER="wp"
DB_PASSWORD=$(openssl rand -base64 12 | tr -dc 'a-zA-Z0-9')

# DirectAdmin API URL for MySQL database creation
API_URL_DB="${PROTOCOL}://${DIRECTADMIN_HOSTNAME}:${DIRECTADMIN_PORT}/CMD_API_DATABASES"

# Create the MySQL database
RESPONSE_DB=$(curl -s -u "${DIRECTADMIN_USERNAME}|${username}:${DIRECTADMIN_PASSWORD}" -d "action=create&name=${DB_NAME}&user=${DB_USER}&passwd=${DB_PASSWORD}&passwd2=${DB_PASSWORD}" "${API_URL_DB}")

if [[ $RESPONSE_DB == *"error=0"* ]]; then
    echo "Database created successfully."

    # Download WordPress
    sudo -u ${username} wp core download --path=${USER_DIR} --allow-root

    # Full path to wp executable. Replace this with the actual path.
    WP_CLI_PATH="/usr/local/bin/wp"
   
    # Create the wp-config file
    sudo $WP_CLI_PATH config create --dbname=${username}_${DB_NAME} --dbuser=${username}_${DB_USER} --dbpass=${DB_PASSWORD} --dbhost=localhost --path=${USER_DIR} --allow-root
   
    # Change the ownership of the wp-config.php file
    sudo chown ${username}:${username} ${USER_DIR}/wp-config.php
   
    # Install WordPress
    sudo $WP_CLI_PATH core install --url="${domain}" --title="New WordPress Site" --admin_user=${ADMIN_USER} --admin_password="${ADMIN_PASSWORD}" --admin_email=${ADMIN_EMAIL} --path=${USER_DIR} --allow-root

else
    echo "Error creating database. $RESPONSE_DB"
fi
fi

exit 0;
 
Last edited:
Back
Top