KNOWNHOST WIKI

User Tools

Site Tools


control-panels:directadmin:how-to-use-automation-via-directadmins-hooks

Automation Via DirectAdmin's Pre and Post Hooks


The degree to which DirectAdmin can be customized is boundless. Those that wish to truly nurture and develop a hosting environment with their own style and functionality should consider using DirectAdmin as the foundation. From personalized style via custom themes to specific DNS records by default via custom zone file templates, the possibilities are vast.

This article aims to explore DirectAdmin's pre- and post- hooks, which are used to perform desired actions following specified events. We will be using the account creation post hook to automatically install and configure WordPress for domains added via the panel.


How it Works

It is surprisingly simple to do. Here are the steps:

  1. Write your script and save it named accordingly for the pre or post hook you want to utilize
  2. Place the script in /usr/local/directadmin/scripts/custom/
  3. chmod 700
  4. chown diradmin.diradmin

With regards to naming the script, you must name it for the hook you want to use. Here are some examples taken from the file

  /usr/local/directadmin/scripts/custom/README

though there are many more hooks available:

  domain_create_pre.sh            - Runs BEFORE a domain is created
  domain_create_post.sh           - Runs AFTER a domain is created
  domain_destroy_pre.sh           - Runs BEFORE a domain is destroyed
  domain_destroy_post.sh          - Runs AFTER a domain is destroyed
  domain_change_pre.sh            - Runs BEFORE a domain is renamed.  A non-zero value will abort the change.
  domain_change_post.sh           - Runs AFTER a domain is renamed.
  subdomain_create_pre.sh         - Runs BEFORE a subdomain is created, but after it's confirmed.
                                    If this script returns a non-zero value, the creation is aborted.
  subdomain_create_post.sh        - Runs AFTER the subdomain is created.
  subdomain_destroy_pre.sh        - Runs BEFORE a subdomain is destroyed.  If this script returns
                                    a non-zero value, the destruction is aborted
  subdomain_destroy_post.sh       - Runs AFTER the subdomain is destroyed.
  user_create_pre.sh              - Runs BEFORE the user is created, but after it's confirmed. 
                                    If this script returns anything but zero, the creation is aborted
  user_create_post.sh             - Runs AFTER the user is created.
  user_destroy_pre.sh             - Runs BEFORE the use is destroyed.  If this script returns anything
                                    but zero, the destruction is aborted.
  user_destroy_post.sh            - Runs AFTER the user is destroyed.
  user_modify_post.sh             - Runs AFTER the user is modified.
  email_create_pre.sh             - Runs BEFORE the virtual email is created, but after it's confirmed.
                                   If this script returns anything but zero, the creation is aborted
  email_create_post.sh            - Runs AFTER the email is created
  email_destroy_pre.sh            - Runs AFTER virtual email account is deleted.
  email_change_pass_post.sh       - Runs BEFORE a virtual pop account password is changed.
                                   If this script returns anything but zero, the change is aborted.
  email_change_pass_post.sh       - Runs AFTER a virtual pop account password is changed.
  dns_write_post.sh               - Runs AFTER a dns zone is written (/var/named/domain.com.db)
  database_create_post.sh         - Runs AFTER a database is created.
  database_user_create_post.sh    - Runs AFTER a database user is created. 
  domain_pointer_destroy_pre.sh    -Runs BEFORE a domain pointer is destroyed. 
  domain_pointer_destroy_post.sh   -Runs AFTER a domain pointer is destroyed. 


Installing WordPress for Every Domain Created via DirectAdmin

Let's proceed with the steps noted above but using the WP-CLI WordPress installation upon account creation as an example.

First, you must write your script that you want to run upon domain creation. Do note that this script uses WP-CLI, so it must be installed on your server at /usr/local/bin/wp in order to use the script. WP_CLI is installed by default on KnownHost servers.

STEP 1 Write your script and save it named accordingly for the pre or post hook you want to utilize

Looking at the README located in /usr/local/directadmin/scripts/custom/, it shows what environmental variables are available to us. For the domain creation post hook, it shows this:


We can use these to construct our script, which I've already done. The script can be downloaded by clicking the link below. If you need assistance with installing WP-CLI, contact KnownHost support, or reference our article regarding WP-CLI.

domain_create_post.sh
  #!/bin/bash
  exec 2>/usr/local/directadmin/customscripts.error.log
 
  #set up DB variables
  dbpass=$(openssl rand -hex 6) > /dev/null
  ext=$(openssl rand -hex 2) > /dev/null
  dbuser="wp${ext}"    # do not include the username_ for dataskq here as DA adds this 
  wpconfigdbuser="${username}_wp${ext}"
  wpadminpass=$(openssl rand -hex 6) > /dev/null
 
  # install wp
  if [ -f /home/$username/domains/$domain/public_html/index.php ]; then 
    echo "WARNING: There appears to be an index file already located in this directory, which indicates that an installation is already present! Empty the directory before running the script again."
    exit 
  else
 
  #Disable DAs index.html file
  if [ -f /home/$username/domains/$domain/public_html/index.html ]; then
    mv /home/$username/domains/$domain/public_html/index.html{,.bak}
  fi
 
  #Create DB
  /usr/bin/mysqladmin -uda_admin -p$(cat /usr/local/directadmin/conf/mysql.conf | grep pass | cut -d\= -f2 ) create ${wpconfigdbuser};
  echo "CREATE USER ${wpconfigdbuser} IDENTIFIED BY '${dbpass}';" | mysql -uda_admin -p$(cat /usr/local/directadmin/conf/mysql.conf | grep pass | cut -d\= -f2 );
  echo "GRANT ALL PRIVILEGES ON ${wpconfigdbuser}.* TO ${wpconfigdbuser} IDENTIFIED BY '${dbpass}';" | mysql -uda_admin -p$(cat /usr/local/directadmin/conf/mysql.conf | grep pass | cut -d\= -f2);
 
  #Download WP
  cd /home/$username/domains/$domain/public_html/
  su -c "/usr/local/bin/wp core download" $username
 
  #set database details in the config file
  su -c "/usr/local/bin/wp config create --dbname=$wpconfigdbuser --dbuser=$wpconfigdbuser --dbpass=$dbpass --dbhost=localhost" 
  $username
 
  #Make symlinks for https and configure wp-config.php and rewrite rules
  if [[ $ssl == 'ON' ]]; then
    su -c "/usr/local/bin/wp core install --url=https://$domain/ --admin_user=admin --admin_password=$wpadminpass --title= --admin_email=admin@$domain " $username
    su -c "/usr/local/bin/wp rewrite structure '/%postname%/'" $username
    printf "\n\nADMIN LOGIN CREDENTIALS:\nURL:https://$domain/wp-admin/\nUSERNAME:admin\nPASSWORD:$wpadminpass\n\n"
  if [[ ! -h /home/$username/domains/$domain/private_html ]]; then
    echo "Making a symlink for https..." 
    cd /home/$username/domains/$domain/
    rm -rf private_html
    su -c "ln -s public_html private_html" $username
  fi
  else
    su -c "/usr/local/bin/wp core install --url=http://$domain/ --admin_user=admin --admin_password=$wpadminpass --title= --admin_email=admin@$domain " $username
    su -c "/usr/local/bin/wp rewrite structure '/%postname%/'" $username
    printf "\n\nADMIN LOGIN CREDENTIALS:\nURL:http://$domain/wp-admin/\nUSERNAME:admin\nPASSWORD:$wpadminpass\n\n"
  fi
 
  #Create .htaccess
  cat << EOF > /home/$username/domains/$domain/public_html/.htaccess
  # BEGIN WordPress
 
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
 
  # END WordPress
  EOF
  chown $username. /home/$username/domains/$domain/public_html/.htaccess
  fi

STEP 2 Place the script in /usr/local/directadmin/scripts/custom/

The script will need to be placed in the custom scripts directory for DirectAdmin to find it. You will need to be logged in as the root user via SSH to access this directory.

STEPS 3 & 4 Make the script executable and accessible to DirectAdmin:

  chmod 700 /usr/local/directadmin/scripts/custom/domain_create_post.sh
  chown diradmin.diradmin /usr/local/directadmin/scripts/custom/domain_create_post.sh

Now that all is done, log into DirectAdmin and create a domain to test. You should see output similar to the following once the domain is created and the script is ran:


Allow a minute for DirectAdmin to process the task queue, and then go ahead and proceed to the URL printed in the output and log in using the credentials provided.


Now, every time you create a new domain in DirectAdmin, WordPress will automatically be installed for you! You can fully automate WordPress hosting!

Note that the script is written so that error logging is enabled and the error log exists at the following location:

  /usr/local/directadmin/customscripts.error.log

You can check this log to see if any fatal errors occurred when running the script.

Also, you may need to allow up to one to two minutes to pass before attempting to access your WordPress admin login page as DirectAdmin may require up to one minute to process the task queue.

Do keep in mind that this script will need to be re-written for subdomains if you want WordPress to be installed upon subdomain creation as well. You would rewrite the script using the appropriate paths and name it 'subdomain_create_post.sh'.

I've rewritten the script here, but there are a few considerations with this script. First of all, There is no SSL environment variable to check whether or not SSL is supported. Because it is best practice to use https and because of the ease and availability of free SSLs thanks to Let's Encrypt, we will assume SSL is enabled for the user.

subdomain_create_post.sh
#!/bin/bash
exec 2>/usr/local/directadmin/customscripts.error.log
 
#set up DB variables
dbpass=$(openssl rand -hex 6) > /dev/null
ext=$(openssl rand -hex 2) > /dev/null
dbuser="wp${ext}"    # do not include the username_ for dataskq here as DA adds this 
wpconfigdbuser="${username}_wp${ext}"
wpadminpass=$(openssl rand -hex 6) > /dev/null
 
# install wp
if [ -f /home/$username/domains/$domain/public_html/$subdomain/index.php ]; then 
 echo "WARNING: There appears to be an index file already located in this directory, which indicates that an installation is already present! Empty the directory before running the script again."
 exit 
else
 
 #Disable DAs index.html file
 if [ -f /home/$username/domains/$domain/public_html/$subdomain/index.html ]; then
  mv /home/$username/domains/$domain/public_html/$subdomain/index.html{,.bak}
 fi
 
 #Create DB
 /usr/bin/mysqladmin -uda_admin -p$(cat /usr/local/directadmin/conf/mysql.conf | grep pass | cut -d\= -f2 ) create ${wpconfigdbuser};
 echo "CREATE USER ${wpconfigdbuser} IDENTIFIED BY '${dbpass}';" | mysql -uda_admin -p$(cat /usr/local/directadmin/conf/mysql.conf | grep pass | cut -d\= -f2 );
 echo "GRANT ALL PRIVILEGES ON ${wpconfigdbuser}.* TO ${wpconfigdbuser} IDENTIFIED BY '${dbpass}';" | mysql -uda_admin -p$(cat /usr/local/directadmin/conf/mysql.conf | grep pass | cut -d\= -f2);
 
 #Download WP
 cd /home/$username/domains/$domain/public_html/$subdomain/
 su -c "/usr/local/bin/wp core download" $username
 
 #set database details in the config file
 su -c "/usr/local/bin/wp config create --dbname=$wpconfigdbuser --dbuser=$wpconfigdbuser --dbpass=$dbpass --dbhost=localhost" $username
 su -c "/usr/local/bin/wp core install --url=https://$subdomain.$domain/ --admin_user=admin --admin_password=$wpadminpass --title= --admin_email=$username@$domain " $username
 su -c "/usr/local/bin/wp rewrite structure '/%postname%/'" $username
 printf "\n\nADMIN LOGIN CREDENTIALS:\nURL:https://$subdomain.$domain/wp-admin/\nUSERNAME:admin\nPASSWORD:$wpadminpass\n\n"
 
 #Create .htaccess
 cat << EOF > /home/$username/domains/$domain/public_html/$subdomain/.htaccess
# BEGIN WordPress
 
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
 
# END WordPress
EOF
 chown $username. /home/$username/domains/$domain/public_html/$subdomain/.htaccess
fi

Download the script to /usr/local/directadmin/scripts/custom/ and ensure it is properly named 'subdomain_create_post.sh', then do the following:

  chmod 700 /usr/local/directadmin/scripts/custom/subdomain_create_post.sh
  chown diradmin.diradmin /usr/local/directadmin/scripts/custom/subdomain_create_post.sh

Now, test! :)


Available Hooks

Hooks are being added continuously to DirectAdmin. The best way to check for the hooks you need is by checking the DirectAdmin version system.

1) Version Search _post.sh

2) Version Search _pre.sh

To find examples, you can look at the many different help articles that utilize hooks.

3) _post.sh hooks

4) _pre.sh hooks

control-panels/directadmin/how-to-use-automation-via-directadmins-hooks.txt · Last modified: 2020/06/16 14:40 by Karson N.