WebFaction
Community site: login faq
4
2

I am either going to create a new Wordpress website, or I have an existing Wordpress site. I would like to use git to manage the files for this site, so that I can roll back easily and also to serve as an effective backup of the code.

What's the best way to set up a Wordpress (or, really, any Static/CGI/PHP) site with a git repository, be it a WebFaction-hosted git repository, or an external repository such as one hosted on github?

asked 09 May '13, 19:57

ryans ♦♦
5.0k103460
accept rate: 43%

edited 29 Aug '13, 05:37


This is relatively straightforward. The main steps will be:

  1. Move the files into a "www" subdirectory and serve that directory as the webroot
  2. Commit the files into git
  3. Clone the repository locally and make updates to the site
  4. Apply those updates on the server

This guide will assume we are using a WebFaction git repository, but is otherwise completely neutral and these steps will work without modification with an external repository.

Create A WebFaction Wordpress Application

If you already have a wordpress website, you can skip this step.

Open https://my.webfaction.com/applications, click "Add new application", and create a new Wordpress application of the latest version. We will use "mywpapp" for the application name in this guide.

Create a WebFaction Git repository

If you are using an externally-hosted git repository, such as Github, you can skip this step.

Open https://my.webfaction.com/applications, click "Add new application", and create a new Git application of the latest version.

We will use "mywpapp_git" for the application name in this guide.

Move files into a "www" subdirectory

SSH into the server and run the following commands (replace "mywpapp" with your app's name):

APP=mywpapp

cd $HOME
mkdir gGx4roR5DECcySZs    # I'm assuming this dir doesn't already exist.
shopt -s dotglob
mv $HOME/webapps/${APP}/* ./gGx4roR5DECcySZs/
shopt -u dotglob
mv ./gGx4roR5DECcySZs $HOME/webapps/${APP}/www

Create a Symbolic Link to Static/CGI/PHP application to serve the www directory

Open https://my.webfaction.com/applications, click "Add new application", and create a new "Symbolic Link to Static/CGI/PHP-5.4" application.

We will use "mywpapp_www" for the application name in this guide.

The Extra Info field must be the absolute path to the new "www" directory we created. In our case, that is:

/home/myusername/webapps/mywpapp/www/

You must replace "myusername" with your username and "mywpapp" with your application name. Also, be sure to include the trailing slash on the end.

Create a website record to serve the new symbolic link

If you have an existing website record, you would edit it to replace the application being served from "mywpapp" to "mywpapp_www".

If you don't have an existing website record, you would open https://my.webfaction.com/websites, click "Add new website". Under "Contents", we choose "Reuse an existing application".

We will use "mywpsite" for the website name, and "mydomain.com" for the domain in this guide.

Create a .gitignore file to ignore the www/wp-content/uploads directory

Still within an SSH session as before, enter the following commands:

APP=mywpapp

cd $HOME/webapps/${APP}
cat << EOF > .gitignore
www/wp-content/uploads
EOF

This creates a new ".gitignore" file which tells git to ignore all of the uploaded files for the site. They can be large binary files which we don't want as part of our code repository, and should be backed up separately.

If you really want to include your uploads into the repository, then you can skip this step (or delete the .gitignore file). Needless to say, this is not recommended.

Create a git repository for this website

Create a new git repository. If you are using an external repository, such as github, you would skip this step but ensure that you have created a new repository to work with.

Create a new repository within the git application which we created earlier, and we will name it "mywpapp_repo" in this example. This is described in our documentation and copied here for convenience (replace "mywpapp_git" with your git app's name and "mywpapp_repo" with your desired repo name):

APP_GIT=mywpapp_git
APP_GIT_REPO=mywpapp_repo

cd $HOME/webapps/${APP_GIT}/repos
git init --bare ${APP_GIT_REPO}.git
cd ${APP_GIT_REPO}.git
git config http.receivepack true

Clone and Commit the wordpress files into the repo

This step will differ a bit if you are using an external repository. We will assume that we are using the Webfaction repository created earlier.

However, we will clone this over SSH which means that the same clone command would work on your local machine or from another server (if you are using multiple servers and load-balancing, for example)

WFUSERNAME=myusername
GITHOST=myusername.webfactional.com
APP=mywpapp
APP_GIT=mywpapp_git
APP_GIT_REPO=mywpapp_repo

cd $HOME/webapps/${APP}
git clone ${WFUSERNAME}@${GITHOST}:webapps/${APP_GIT}/repos/${APP_GIT_REPO}

This may ask you to accept a host key (The authenticity of host ... cannot be established), and then you may need to enter your SSH password as well. You can remove the need to enter a password by setting up SSH Keys if you prefer.

In any case, once you've cloned the repository, you should see this (that means it worked):

warning: You appear to have cloned an empty repository.

Once that's done, proceed to commit the files using the following. Be sure to replace "MYNAME" and "MYEMAIL" (git needs to know who you are):

MYNAME="John Doe"
MYEMAIL="john@example.com"

cd $HOME/webapps/${APP}
mv ./${APP_GIT_REPO}/.git .
rmdir ./${APP_GIT_REPO}
git config user.name "${MYNAME}"
git config user.email "${MYEMAIL}"    
git add .
git commit -m'initial commit'
git push origin master

Clone the repository locally

At this point, the website should be up and running. Now we need to clone the repository down for development work. Therefore, this would be done on your local machine (for Windows users, see msysgit):

WFUSERNAME=myusername
GITHOST=myusername.webfactional.com
APP_GIT=mywpapp_git
APP_GIT_REPO=mywpapp_repo

git clone ${WFUSERNAME}@${GITHOST}:webapps/${APP_GIT}/repos/${APP_GIT_REPO}

You could at this point make changes to the code and commit them.

Update the website from the repository

A group of people may all want to commit a series of changes before making those live on the website (yes, using branches and setting up separate development and live versions of the site is better for larger projects).

In order to make your changes "live", you need to pull down those changes on the server side. Note that if you do have any uncommitted changes, it is absolutely essential that you merge them in as well. Therefore, the procedure is as follows (run this on the server, not locally):

APP=mywpapp

cd $HOME/webapps/${APP}
git add .
git commit -m 'pre-deploy commit'
git pull
git push
git checkout -f master
git reset --hard

Create update script

It's easy to bundle the above into an update script which you can run on the server. Here are the commands to make this script:

APP=mywpapp

cd $HOME/webapps/${APP}
cat << EOF > update_server.bash
#!/bin/bash
cd $HOME/webapps/${APP}
git add .
git commit -m 'pre-deploy commit'
git pull
git push
git checkout -f master
git reset --hard
EOF
chmod 755 update_server.bash
./update_server.bash

You're actually running the update_server.bash in order to commit it to the repository, but don't run it from your local machine - it's meant to be run from the WebFaction server. That means in the future you can just log in and run the command:

APP=mywpapp
$HOME/webapps/${APP}/update_server.bash

To update the server to the latest git deployment.

Create a post-receive hook for automatic deployment

This step is optional. Only do this if you want to have the site update automatically when committing changes to the repository.

First, set up SSH Keys so that you can check out the repository from the website server without needing a password. If the application is hosted on the same server as the git repository, these commands should work:

mkdir -p $HOME/.ssh
cd $HOME/.ssh
if [ ! -f $HOME/.ssh/id_dsa ]; then ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -N ''; fi
cat $HOME/.ssh/id_dsa.pub >> $HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/id_dsa $HOME/.ssh/authorized_keys
chmod 700 $HOME/.ssh
ssh -oStrictHostKeyChecking=no localhost
exit

Then, create the post-receive hook

APP=mywpapp
APP_GIT=mywpapp_git
APP_GIT_REPO=mywpapp_repo

cd $HOME/webapps/${APP_GIT}/repos/${APP_GIT_REPO}/hooks
cat << EOF > post-receive
#!/bin/bash
$HOME/webapps/${APP}/update_server.bash
EOF
chmod 755 post-commit

Hope that helps!

permanent link
This answer is marked "community wiki".

answered 09 May '13, 23:01

ryans ♦♦
5.0k103460
accept rate: 43%

edited 29 Aug '13, 05:43

Hi,

Please could you explain why the first part of this answer is necessary? Is it not possible to use the webroot created by the dashboard when adding a Wordpress application?

(02 Jan '14, 12:35) johnkingeu

It's not necessary, and yes - you can use the webroot created by the dashboard when adding a Wordpress application.

However, I do definitely prefer it this way, because it offers a lot more flexibility. In other words, what if "APP_GIT" and "APP_GIT_REPO" were the same thing? The parent directory of the "www" directory would itself be a git repository, and using a Symbolic Link application means that your ".git" directory won't be exposed as it's still outside of the webroot.

In short, writing the guide this way introduces a lot of flexibility in the way the guide is used in the future. It's a better general solution, and makes it easier for you to directly apply it to many different configurations and for may different types of applications, including frameworks (such as Laravel) that by must run from a "public" subdirectory under the main app, and require a Symbolic Link to Static/CGI/PHP app to run.

Hope that clarifies!

(02 Jan '14, 21:06) ryans ♦♦

Thanks Ryan, it's a great answer and a useful clarification.

(05 Jan '14, 10:54) johnkingeu
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×245
×111

question asked: 09 May '13, 19:57

question was seen: 7,604 times

last updated: 05 Jan '14, 10:54

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM