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.

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 mysite.com

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 deploy.sh. You would then modify lines 4-11

Simple Version

Simple Version

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#!/bin/bash

# Settings. Modify these lines to match your settings

codeDir=mycodedir;

now=`date+"%m_%d_%Y.%H%M"`

tarname="mysite$now.tar"

cloneUrl="my ssh clone url"

host="mywebhost"

siteHome="/home/myuser/public_html/mysite.com"

sshPort=11208;

sshKeyPath=~/.ssh/id_rsa;

# 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-psite

tar-xzf deploy/$tarname-Csite;

cp site-bk/.htaccess site/

cp site-bk/index.php site/

cp-Rsite-bk/wp-content/uploads/site/wp-content/

EOF

Explanation

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.

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.