A Free Quick Install Script for WordPress

As an SEO company that does not do any web design, we initiate a shockingly large number of WordPress installations. As the Director of Technology, that job largely falls to me. A while ago I realized that this is a repetitive process that can be largely automated saving quite a bit of time…not to mention mouse clicks…on each installation. To do this, I wrote a simple script that handles a lot of the file movement for me.

Two things about this before we get into the procedure:

  • You need root access to the cPanel / WHM server (Dedicated or Virtual Private Server) on which you plan to do the WordPress install.
  • Yes, I know Scriptalicious can do a nice job of installing WordPress quite quickly. When I initially wrote my own script, Scriptalicious would mess things up more than it would make things right. Also, I’m a little bit of a control freak so I like to do things my way and know exactly what’s happening behind the scenes.

The Process

  1. Use WHM to create a new account for your new domain. We’ll use mydomain.com and simply mydomain as the account username for our discussion.
  2. SSH into your server / VPS as root (or su to root if you have to log in with lesser privileges).
  3. Place this script in /home on the server and make it executable with the command:
    chmod +x /home/do-wp.sh

    (This assumes you’ve named your script “dowp” like I have.)

  4. Run the script by typing:
    /home/do-wp.sh mydomain

If everything goes right, the script will run through in about 0.5 seconds and you’ll be ready to create your WordPress database, modify your wp-config.php file, and get to WordPressing!

The Script

Here’s the script and below I’ll go through what it does.

#!/bin/bash          
#
#  This file "dowp" is designed to be run as 'root' on a *nix server, take an
#  already-created account name as an input, download the latest copy of WordPress,
#  move files around, change permissions, and put things in place to minimize
#  the amount of input from a user to get WordPress up and running.
#
#  Created: February / March 2016
#  Last Modified:  08 March 2016
#  Author:  Jerod Karam (with lots of helps from the internets)
#

start=`date +%s%3N`

#  Make sure the user has supplied an account name as a command line argument.
if [ -z "$1" ]
  then
    echo "###############################################################"
    echo "#                                                             #"
    echo "#  The account name must be supplied as an argument for this  #"
    echo "#  script.  For example: do-wp.sh mydomain                    #"
    echo "#                                                             #"
    echo "###############################################################"
    exit
fi

#  Make sure that the account name the user supplied is a valid account name.
if [ ! -d "$1" ]
  then
    echo "##############################################################"
    echo "#                                                            #"
    echo "#  This account name does not appear to exist.  Please       #"
    echo "#  check the spelling and/or account list in WHM and try to  #"
    echo "#  run this script again.                                    #"
    echo "#                                                            #"
    echo "##############################################################"
    exit
fi

#  Download the latest version of WordPress and put it in the right directory.
echo "Downloading WordPress..."
wget http://wordpress.org/latest.tar.gz
mv latest.tar.gz /home/$1/public_html
cd /home/$1/public_html

#  Unzip WordPress
echo "Unzipping Tarball..."
tar xfz latest.tar.gz

#  Move WP files into the account root, get rid of extraneous files, create the wp-config file.
echo "Moving files into place and cleaning up..."
mv wordpress/* ./
rmdir ./wordpress/
rm -f latest.tar.gz
cp wp-config-sample.php wp-config.php

#  Change file ownership so WP installation and auto-updating works correctly
echo "Changing file ownership..."
chown -R $1:$1 *
cd /home

#  Inform user that script is complete, display elapsed time, exit cleanly
echo "Complete!"
end=`date +%s%3N`
diff=`expr $end - $start`
echo "Elapsed Time: $diff milliseconds."
exit

The Explanation

The top commented sections (lines 1-12) are not necessary but give information about the script we’re using. It’s nice to include this for your own reference as well as the benefit of others.

Line 14 records the current time into a variable called “start.” We will use this later to calculate elapsed time.

Lines 16-26 verifies that an account name has been provided to the script so it knows into which account/directory to install WordPress. An error here will allow the script to exit cleanly and display a message to the user.

Lines 28-39 verifies that the provided account name/directory exists. Again, any errors result in a clean exit with notification to the user.

Lines 41- 45 download the latest version of WordPress and put it into the appropriate directory.

Lines 47-49 unzip the downloaded file.

Lines 51-56 move the WordPress files from the /accountName/wordpress directory to the /accountName directory. This section also deletes the /wordpress directory, deletes the downloaded zipped up file, and makes the appropriate copy of the wp-config-sample.php file.

Lines 58-61 change the ownership of all the files to the account name. If you don’t do this step then all the files will be owned by root and the WordPress automatic updating system won’t work right. (This was a booger to figure out, BTW.)

Lines 63-68 finish up, display the elapsed time, and exit the script cleanly.

Closing Thoughts

I’ve been using the concept and core of this script for several years and it’s served me well. Of course, I’ve had to tweak it here and there for various reasons and overall I’m quite proud of the results it’s provided. These days, I have to admit, the Scriptalicious WordPress installation is a cakewalk and does a great job…but out of pride, I still sometimes run back to my old homebrew buddy that has been by my side for so many years.

If you’ve got your own special way of installing WordPress I’d love to know about it.  Drop me a comment!

Jerod Karam

Jerod Karam is Director of Technology at Netvantage Marketing, an online marketing company specializing in SEO, PPC and social media. Jerod consults with internal and external clients on all matters technical and is responsible for most of the technical work and custom online tools the company uses.

Leave a Reply

Your email address will not be published. Required fields are marked *