Deploying WordPress via Bash Script


Deploying your site should be easy. There are many ways to accomplish this, and I decided to take a bash script approach for one project. This will appeal to the more hands-on developers, but don’t worry everything has been explained here.

Tools/Technologies Used:

  1. Source Control (I use git, but you can use any.)
  2. Bash Shell Script
  3. SSH (your host would have to have this enabled)
  4. SCP (file transfer)

The site uses shared hosting, but fortunately this shared host gives SSH access. They’re pretty awesome.

This assumes that you have your WordPress site installed in a subdirectory and not your root directory for your site. I find it much cleaner to install in a subdirectory than root directory. And your site can still behave where the users see

So your site files will be stored in a place like home/public_html/
But visitors can load your site by going to If your setup is not like this it can be adapted very easily using this guide:

Now on to the deploy script

Warning: It’s best to make backups before trying to run this for the first time. You can also comment sections to see what each one does.

You would simply just paste the code below into a file. I called mine You would then modify lines 4-11

Simple Version


# Settings. Modify these lines to match your settings
now=`date +"%m_%d_%Y.%H%M"`
cloneUrl="my ssh clone url"

# Settings
rm -rf $codeDir

git clone --recursive $cloneUrl;

tar -czf $tarname -C $codeDir . 

scp -P $sshPort -i $sshKeyPath $tarname $host:$siteHome/deploy

ssh -t -t -i $sshKeyPath -p $sshPort $host << EOF
	cd $siteHome;
	rm -rf site-bk;
	mv site site-bk;
	mkdir -p site
	tar -xzf deploy/$tarname -C site;
	cp site-bk/.htaccess site/
	cp site-bk/index.php site/
	cp -R site-bk/wp-content/uploads/ site/wp-content/


This script

Line 14: Remove old folder so that you only get new files that you clone
Line 16: This gets your code from your source control system. –recursive is optional, but should be included if you use submodules
Line 18: Create a tar archive of the files you just cloned. These will be transferred to the server using SCP.
Line 20: Upload tarfile to the server
Line 22: This looks a little funny, but this is used to pass the commands unto the server through a SSH connection.
Line 23: Go to site home
Line 24: Delete your old backup of your site (it’s okay if it doesn’t exist on your first run)
Line 25: Copy your /site folder to /site-bk. This serves as a backup incase you need to do a quick rollback.
Line 26: Recreate your site directory
Line 27: Extract to your site directory
Lines 28,29: Copy important files into your site home. This is a requirement when your site is installed in a subdirectory but loads from main url.
Line 30: Copy over your uploads. I choose to omit these from my source code repository, so these need to be copied.

If your site does not use source control, you can delete the clone information and tar the folder directly.

If you are using windows for your development, you can get a terminal that executes shell commands by installing git bash.

Leave a Reply

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