KNOWNHOST WIKI

User Tools

Site Tools


control-panels:directadmin:custom-package-item-creation-example

DirectAdmin's Custom Package Item Creation Example - Install WordPress


Having WordPress automatically installed could save a lot of time for busy WordPress-only resellers, allowing them to focus on ways to optimize their server and their installations instead. However, there are those users that require something different, too. Some may require Joomla, DokuWiki, Drupal, or other CMSs and you wouldn't want WordPress installed automatically upon creating those accounts. A solution that would allow one to automate WordPress hosting while maintaining the ability to offer alternative CMSs on demand would be to create a custom WordPress package that would install WordPress upon account creation unless you selected an option instructing the server not to. This would allow you to install the requested CMS after account creation, but still automate the majority of your hosting service by installing WordPress by default for other accounts.

You could take this setup even further and create a Joomla package item, a Drupal package item, a Magento package item, and so on and so forth. This article explores how to use DirectAdmin's custom package item creation feature coupled with post-domain creation scripting to install WordPress for newly created domains that are created with the custom package item "Install WordPress" selected.


Create the Custom Package Item Configuration

First, create the following file:

  /usr/local/directadmin/data/admin/custom_package_items.conf

Add the following contents to the file:

  installWP=type=checkbox&string=Install WordPress&desc=Installs WordPress for domains automatically upon creation&checked=yes

Note that the above includes 'checked=yes', which means that this feature will be checked and enabled by default for new packages and for new users created under the package. You will have to manually remove the check if you don't want WordPress installed for the new package or for the new user. If you'd prefer to manually enable the option to install WordPress automatically, then change this to 'checked=no' instead.

Correct the ownership:

  chown diradmin. /usr/local/directadmin/data/admin/custom_package_items.conf


Add the WordPress Installation Script as a Post Domain Creation Hook

Now, create the post domain creation script,

  /usr/local/directadmin/scripts/custom/domain_create_post.sh

with the following contents,

  chmod 700

and chown it to diradmin.diradmin.

domain_create_post.sh
#!/bin/bash
exec 2>/usr/local/directadmin/customscripts.error.log
 
#check custom pkg item installwp
 
if [[ $installwp == 'OFF' ]]; then
  exit 0;
else
 
 #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 -s /bin/bash -c "/usr/local/bin/wp core download" $username
 
  #set database details in the config file
  su -s /bin/bash -c "/usr/local/bin/wp config create --dbname=$wpconfigdbuser --dbuser=$wpconfigdbuser --dbpass=$dbpass --dbhost=localhost" $username
 
  if [[ $ssl == 'ON' ]]; then
   su -s /bin/bash -c "/usr/local/bin/wp core install --url=https://$domain/ --admin_user=admin --admin_password=$wpadminpass --title= --admin_email=$username@$domain " $username
   su -s /bin/bash -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 -s /bin/bash -c "ln -s public_html private_html" $username
   fi
  else
   su -s /bin/bash -c "/usr/local/bin/wp core install --url=http://$domain/ --admin_user=admin --admin_password=$wpadminpass --title= --admin_email=$username@$domain " $username
   su -s /bin/bash -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
fi


The Result Thus Far

These two files will result in the following:

  1. A custom package checkbox, "Install WordPress"
  2. All domains created under this package will have WordPress automatically installed and configured for them as long as the "Install WordPress" checkbox is checked.

NOTE: A separate script is required for subdomains. See our article discussing post- and pre- scripts available in DirectAdmin for an example script for installing WordPress for a subdomain automatically upon subdomain creation. 1) Automation Via DirectAdmin's Pre and Post Hooks


Create a Package with the Custom Package Item

Now, log into your DirectAdmin panel and create a new package. You should see your custom package item displayed as an option.



Add a User to the Package

You can now create a new user or add an existing user under the package (make sure the "Install WordPress" option is checked if you want to use it). If you add an existing user, the existing domains will not have WordPress installed, but new domains will. If you create a new user under this package, the domain you create along with the user will have WordPress installed and configured, as will any subsequent domains created under the user.


You can click "Customize" next to the "User Package" option to make sure the custom package option is enabled.

You can expect to see output like the following upon creating the user:


One could do the same for any other CMS whose installation and configuration can be automated. You would be able to automate hosting many types of CMSs and have them installed automatically depending on the type of package you assign to the account.

You could offer other custom package items, such as malware scanning/monitoring, site uptime monitoring, dynamic DNS, and whatever other services you can code solutions for. If you can write a script to accomplish the task, you can create a custom package item for it, which will be passed along to the pre- and post- script hooks, and thus, automatically execute the script upon specified actions. The possibilities are endless when you are permitted to create as you please via such features.

control-panels/directadmin/custom-package-item-creation-example.txt · Last modified: 2020/06/16 14:33 by Karson N.