How I Deploy Websites

Posted by jtopjian on February 14, 2009 under Development | Be the First to Comment

I’ve gotten into the habit of using a Virtual Machine to develop my websites. The VM mimics the production server as much as possible so there are little-to-no changes that need made when the site is moved to production.

To move a site from the development server to the production one, I wanted a solution that was simple and not obtrusive to my normal workflow.

I tried Capistrano and was able to get it working with a few of my PHP projects. It worked great and I really liked how extendible it was, but I thought it took too much work integrate it into a new project.

My Solution

In the end, I settled on Git and a simple shell script:

#!/bin/sh
LOCALHOME=/local/path/to/project
REMOTEHOME=/remote/path/to/project
WEBROOT=public_html
SERVER=remote_server
USERNAME=jtopjian
GITREPO=/path/to/project.git

ssh $USERNAME@$SERVER "cd $GITREPO; git archive master | tar -x -C $REMOTEHOME"

This script assumes that your remote Git repo is on the same server as the website you’re creating. If it isn’t, you’ll need to make a few minor changes.

I use git archive because this ensures that only the latest version of the project is deployed to production and doesn’t copy any meta-data.

If you use Zend or any other framework where you specify DEVELOPMENT or PRODUCTION configuration settings, you can easily switch from DEV to PROD with sed. Append this to the last line:

$ ssh $USERNAME@$SERVER "cd $REMOTEHOME/$WEBROOT; sed -i -e 's/development/production/' index.php"

The Whole Workflow

So now, my whole workflow moves like this:

  • Write code
  • Update git:

    • git add .
    • git commit -m "Summary"
    • git push
  • Deploy:

    • ./deploy.sh

Conclusion

I’m curious to know other types of deployment methods. If you have one, please leave a comment!

About Joe:
Joe Topjian is the owner of Terrarum -- a system administration company that specializes in Linux Infrastructure and Automation. More information about Terrarum can be found here.
Website:http://terrarum.net
Jabber:joe@topjian.net

Add A Comment