If in your web travels you install or maintain web applications, than you proabbly know the pain updates, patches and upgrading can bring. It’s not always a ‘cinch’, espically if you have custom code or integration between other applications.
Wordpress is a great tool that I use to power this blog, amoung other things. The development team is great and release updates frequently. Common to uprading, however, is incompatibility. If you have a medium-to-large amount of plugins or even custom code, you may have been bitten by the upgrade bug already.
In addition to incompatibility, most Wordpress powered blogs incur downtime during the upgrade while deleting folders, replacing files, and even troubleshooting a problem (eek!). This may not be a huge issue if you’re small, but for high traffic blogs, it can be a serious concern.
Even futher, I’m somewhat of a nut when structuring a webserver, website… or anything! This method gives good organization and lots of flexibility (in case you change your mind about your original organization plans).
Advantages to This Method
- no down time during upgrades - symlinks are updated instantly
- never mistakenly overwrite a file
- archive previous versions
- easy to rollback - just update you symlink to the previous folder
- easy to test new versions
- easy to integrate into your workflow
Upgrade Wordpress using Symlinks
What is a Symlink?
On a linix system, a symlink is like a shortcut, or pointer, to another location. This is true for files and folders. In addition, a symlink acts like the file or folder its pointing to.
For example, we could have created a symlink like /var/www/mysite-com/html/blog that actually points to /var/www/mysite-com/wp/v252/. In this case, when we point our browser to mysite.com/blog/ we are shown the contents of where the symlink points to (which is /var/www/mysite-com/wp/v252/.
See where we’re going with this? The basic idea is to use a symlink in our public html folder (because that never changes) that points to a folder that contains the the updated version of wordpress (which changes each time we upgrade. eg: wp/v252/).
Putting it into practice
Step 1: Create Folders and Symlinks
Open up your terminal and login. Switch users if necessary, but keep in mind that you want the owner of the symlink to be the same as the owner of your other html files and folders.
Next, browse to the location where you will be keeping the target folders your symlink will point to. For security, ensure this folder is outside of your publicly accessible html folder (eg: /var/www/html/).
$ cd /var/www/mysite-com
Create a logically named folder to contain your wordpress versions.
$ mkdir symlinked/wp/v252
Finally, browse to your root html folder where you will create the symlink. In the example below, we are creating a symlink called blog that points to the versioned folder created previously.
$ ln -s /var/www/mysite-com/symlinked/wp/v252 blog
Linked! Go ahead and test it out by placing a simple “Hello World” index.html file in your version folder and pointing your browser to your new symlink.
If you are already using a regular folder for your Wordpress blog and want to keep it the same, you will need to do a little re-arranging of files and create your symlink when you’re ready to go live.
Step 2: Copy Live Wordpress Files to Versioned Folder
Now that we have a new home for our files to live, we’ll need to copy our current live Wordpress files into it.
$ cp -R /var/www/mysite-com/html/old-blog /var/www/mysite-com/symlinked/wp/v252
Step 3: Version Wordpress Database
Dump a version of your live database into a new, logically named database using your method of choice (eg: phpMyAdmin, MySQL admin, command line, etc). For example, the live version might be named wp_v250 and the new named wp_v252.
NOTE: A phing script is a great way to automate this process. I haven’t been able to employ this method yet.
Step 4: Update Wordpress
Using your preferred method, update your Wordpress version following the official update instructions. Remember, the files you want to update are in your new versioned folder (eg: /var/www/mysite-com/symlinked/wp/v252).
Don’t forget to update your Wordpress configuration (wp-config.php) file to reflect the new database.
Update Symlinks
Next time you’re ready to switch over to a new version, you’ll need to update the live symlink that points to your versioned wordpress folder. In order to do that, we can use something like the following
ln -s /var/www/mysite-com/symlinked/wp/v253 blog_tmp && mv -Tf blog_tmp blog
This will do a few things for us at once so that users will see little to no down time.
- Creats a new symlink called “blog_tmp” that points to our new versioned folder
- Renames the symlink “blog_tmp” to “blog”
In Conclusion
Admittedly, the idea is not a new one and is probably used frequently among Linux pros and enthusiasts… but I haven’t found it being used often in small or medium web projects.
Things to Note About Symlinks
- symlinks do not have permissions; instead, the target’s permissions are used
- the owner of the symlink will be the user that created it
