WebFaction
Community site: login faq

I am either going to create a new Wordpress website, or I have an existing Wordpress site. I would like to use svn 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 svn repository, be it a WebFaction-hosted svn repository, or an externally-hosted repository?

asked 29 Aug '13, 05:23

ryans ♦♦
5.0k73159
accept rate: 43%

edited 29 Aug '13, 18:55

Hi ryans: this tutorial can be used for any web app or its only specific for WP?

(26 Sep '13, 08:43) lacunza

You may use it to manage any Static/CGI/PHP based app.

(26 Sep '13, 17:02) johns ♦♦

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 svn
  3. Checkout 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 Subversion 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 subversion repository

If you are using an externally-hosted svn repository, you can skip this step.

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

We will use "mywpapp_svn" 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.

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
SVNHOST=myusername.webfactional.com
APP=mywpapp
APP_SVN=mywpapp_svn

cd $HOME/webapps/${APP}
svn co svn+ssh://${WFUSERNAME}@${SVNHOST}/home/${WFUSERNAME}/webapps/${APP_SVN}/

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 (twice). 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):

Checked out revision 0.

Once that's done, proceed to commit the files using the following:

cd $HOME/webapps/${APP}
mv ./${APP_SVN}/.svn .
rmdir ./${APP_SVN}
mkdir -p $(readlink -f www/wp-content/uploads)
svn add www

# This tells subversion 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. Needless to say, this is not recommended.
svn rm --keep-local $(readlink -f www/wp-content/uploads)
svn propset svn:ignore dirname $(readlink -f www/wp-content/uploads)

svn commit -m'initial commit'

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 TortoiseSVN):

WFUSERNAME=myusername
SVNHOST=myusername.webfactional.com
APP_SVN=mywpapp_svn

svn co svn+ssh://${WFUSERNAME}@${SVNHOST}/home/${WFUSERNAME}/webapps/${APP_SVN}/

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, before updating. In general, only make changes in the checked-out repositores, and not in $HOME/webapps/$APP_SVN/www.

In order to update the site, simply type (on the server, not locally):

APP=mywpapp

cd $HOME/webapps/${APP}
svn up

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}
svn up
EOF
chmod 755 update_server.bash

svn add update_server.bash
svn commit -m'add update_server.bash'
./update_server.bash

The update_server.bash script isn't meant to be run 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 svn deployment.

Create a post-commit 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 svn 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-commit hook

APP=mywpapp
APP_SVN=mywpapp_svn

cd $HOME/webapps/${APP_SVN}
cat << EOF > post-commit
#!/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 29 Aug '13, 05:25

ryans ♦♦
5.0k73159
accept rate: 43%

edited 29 Aug '13, 05:35

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:

×242
×40

question asked: 29 Aug '13, 05:23

question was seen: 3,571 times

last updated: 26 Sep '13, 17:02

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 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