1. Home
  2. DirectAdmin
  3. Automatically Create WordPress on DirectAdmin account creation.

Automatically Create WordPress on DirectAdmin account creation.

Automated WordPress Installation on DirectAdmin

WordPress is an open-source content management system (CMS) based on PHP and MySQL. It is used by millions of websites and blogs to build and manage their content.

  • It is an open-source CMS, meaning the core software is free and publicly available to use and modify. It is developed and maintained by a community of contributors.
  • WordPress allows users to easily build and manage websites and blogs without needing to know coding. It has an intuitive dashboard and theming system that makes creating professional looking sites simple.
  • At its core, WordPress is focused on content creation and publishing. It provides tools for authors and editors to write posts and pages, add media, and manage comments. The admin area makes site management easy.
  • WordPress is highly extensible through plugins and themes. Thousands of plugins extend functionality from SEO to ecommerce to security. Themes control the appearance and customize the site design.
  • It is used by over 40% of all websites worldwide, from personal blogs to huge news sites.
  • WordPress can be hosted on regular web hosts or specialized WordPress hosting services. It can be installed on your own server or used through hosted solutions like WordPress.com.

 

Prerequisites

Install WordPress-CLI (WP-CLI)

The recommended way to install WP-CLI is by downloading the Phar build (archives similar to Java JAR files, see this article for more detail), marking it executable, and placing it on your PATH.

First, download wp-cli.phar using wget or curl. For example:

 

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

 

Then, check if it works:

php wp-cli.phar --info

 

To be able to type just wp, instead of php wp-cli.phar, you need to make the file executable and move it to somewhere in your PATH. For example:

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

 

Now try running wp –info. If WP-CLI is installed successfully, you’ll see output like this:

OS:     Linux 5.4.0-90-generic #101-Ubuntu SMP Fri Oct 15 20:00:55 UTC 2021 x86_64
Shell:  /bin/bash
PHP binary:     /usr/local/php74/bin/php74
PHP version:    7.4.33
php.ini used:   /usr/local/php74/lib/php.ini
MySQL binary:   /usr/local/bin/mysql
MySQL version:  mysql  Ver 15.1 Distrib 10.6.12-MariaDB, for linux-systemd (x86_64) using readline 5.1
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /root
WP-CLI packages dir:
WP-CLI cache dir:       /root/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.8.1

 

Since wp-cli defaults to /tmp/mysql.sock, and there’s no way to put a flag or command line variable, we will simply symlink DirectAdmin’s mysql.sock to /tmp.

This will in no way affect any of your databases, or DirectAdmin’s functionality.

This way wp-cli can create a database, user, and password for the new WP site automatically.

ln -s /usr/local/mysql/data/mysql.sock /tmp/mysql.sock

Automated WordPress Install on DirectAdmin Script

Create install_wp.sh here: /usr/local/directadmin/scripts/custom/user_create_post/install_wp.sh

Create a file, /usr/local/directadmin/scripts/adminpassword

Put in your DA admin password here, nothing else.

Then chown the file:

chown diradmin:diradmin /usr/local/directadmin/scripts/adminpassword

There will be a few parts that you will have to manually change.

# Constants
DIRECTADMIN_HOSTNAME
DIRECTADMIN_PORT
DIRECTADMIN_USERNAME
DIRECTADMIN_PASSWORD

Change the DIRECTADMIN_HOSTNAME to your server DA URL, without any http:// or trailing slashes.

 

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

Change the package names here to whatever you want to use.

#!/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;

 

That’s it, no more editing.

How the script works

Whenever an account is created, the script will run via DirectAdmin’s hook system. This particular WordPress auto-installer script will check the packages you have set in the script, if they are found, it will proceed to install the WordPress site on the account. If the package is not used, or found, the script will simply stop. So you won’t have to worry about other account types accidentally having WordPress auto-installed on them.

Screenshots of it working

Creating the user account manually.

Please note, if you use WHMCS or another billing solution this will still work as it’s run via DirectAdmin’s hook system.

create user

 

User created successfully message, with additional WordPress installation information.

create user finished

 

WordPress site will automatically email the user information to the account owner.

wordpress email auto

Automatically works with DirectAdmin’s WordPress Manager

wordpress manager
wordpress manager admin
Updated on January 23, 2024

Was this article helpful?

Related Articles

Add A Comment