Advertisement
Tips

Quick Tip: Upgrade Your WordPress Site via SSH

by

I have already written about how to install WordPress via the shell. I am taking it further today by showing how you can upgrade it as well using shell access. Though you can always upgrade from within WordPress using the 1-click method, the shell method is more secure as it encrypts all the data transfer. There are two ways of doing it: You can do it using an SSH client or enable WordPress to use SSH Transfers for all types of upgrades whether its a core upgrade or plugin/theme upgrade/installs. I will discuss both methods here.


Method 1 Upgrading Using an SSH Client

Important Note: Always ensure you do a complete backup of your WordPress install's files and database before performing an upgrade.

Now I won't write again about how to connect to SSH as I have already discussed that in my previous tutorial. Now all the commands here are being done assuming that you are in your blog's directory(blogdemo in this case). First of all download the latest version of WordPress and extract it.

wget http://wordpress.org/latest.tar.gz
tar xfvz latest.tar.gz

Now we need to delete the old wp-admin and wp-includes directories.

rm -rf ./wp-admin
rm -rf ./wp-includes

Now go to the wordpress directory where we extracted the latest WordPress files and move them back to the blogdemo directory.

cd wordpress
mv * ../blogdemo/

You will be asked to overwrite certain files and some directories as well inside wp-content. Just press y and proceed. Now exit from the wordpress directory and delete the directory and the file latest.tar.gz. We're done with them now, and don't need them taking up space.

cd ../
rm -rf ./wordpress/
rm -f latest.tar.gz

In the final step just login to your dashboard and run the database upgrade.


Method 2 Enabling SSH2 for WordPress Upgrade/Installs

Did I mention... Always ensure you do a complete backup of your WordPress install's files and database before performing an upgrade.

In this method, you can enable WordPress to use SSH for upgrading and plugin/theme installations. First of all you need to check with your webhost if they have the SSH2 extension installed for PHP or not. To check for that run the following PHP command

var_dump( extension_loaded( 'ssh2' ) );

If this command returns true, that means it's installed, otherwise it's not. If your webhost doesn't offer it, ask them to install it for you or if you are running a VPS/dedicated, you can install it yourself. Follow the method to install from here.

First of all we need to generate a working set of public and private keys which will be used for authenticating the user. To do that, use the following command in your SSH client:

ssh-keygen

You will be asked for the name of the file. You can leave it blank or enter any custom name. If you leave it blank, the file names will be id_rsa.pub and id_rsa. Also you can set a passphrase to add additional security or can leave it blank.

Time to add keys needed to be added to the authorized_keys file. In our case, SSH keys were generated and kept in the .ssh directory in the root directory.

cd .ssh
cp id_rsa.pub authorized_keys

Now change permissions so that WordPress can access those keys.

cd ../
chmod 755 .ssh
chmod 644 .ssh/*

You may need to change the user of the directory if you are running PHP as a different user.

Now that you have got SSH2 setup and working, you should see the following screen while performing any upgrade/plugin or theme install.

Here user is the SSH username you used to login and perform all the commands and password is the passphrase you were asked to set during the ssh-keygen command. If you didn't choose any passphrase, you should keep the password field blank. To avoid entering this data repeatedly, enter the following code in your wp-config.php

define('FTP_PUBKEY','/home/user/.ssh/id_rsa.pub');
define('FTP_PRIKEY','/home/user/.ssh/id_rsa');
define('FTP_USER','user');
define('FTP_PASS','passphrase');
define('FTP_HOST','domain.com');

From now on WordPress will perform all file transfer operations using SSH.


References

Related Posts
  • Code
    Tips
    New wp-config Tweaks You Probably Don't Know8 new wp config tweaks you probably didnt know about 400
    The wp-config.php file: One of the most loved WordPress feature for some, one of the worst nightmares for others. There are countless tips and tricks in a plethora of articles, and you can't get enough of them; however, this article is aiming to be different. In this article, we're going to get familiar with eight new wp-config tricks that are less known than functionality such as turning off post revisions, increasing the memory limit, or other similar features.Read More…
  • Code
    Web Development
    How to Use New Relic With PHP & WordPressRelic retina preview
    Today we will look at how to monitor a PHP application using New Relic. More specifically we will set up a basic WordPress installation and get some performance data about it, in the New Relic dashboards.Read More…
  • Code
    WordPress
    An Introduction To Deploying WordPress with MinaImage400
    As a PHP application, WordPress is usually deployed by a very old method: uploading files via FTP. We have some deployment tools, but they often requires some type of Ruby skill. For example, one popular, powerful tool is Capistrano, but it's also very heavy with many Ruby/Rails related features. I also think that it's little bit tricky to install Capistrano for a PHP developer without any Ruby knowledge. So what options do we have as WordPress developers? In this tutorial, I will introduce you Mina: A small, light tool aims to fast deployment and server automation.Read More…
  • Computer Skills
    Electronics
    How to Use a Raspberry Pi as a Local Web ServerThumb
    In this tutorial I will show you how to set up a Raspberry Pi to be used as a Local Web Server with SSH and FTP functionality.Read More…
  • Code
    Articles
    Coding With KodingCoding with koding retina preview
    Cloud IDEs have been around for a little while now, and they have been pretty good for things like pair programming, or cases where you want to code consistently no matter where you are. Koding just came out of private beta, and they would like to take this notion a couple steps further, with their "cloud ecosystem". In this article we will take a look at what Koding is, as-well as some of the benefits you can get from using it.Read More…
  • Code
    PHP
    Deploying a Laravel Application Using CapistranoLaravel 4 auth retina preview
    So you've just built a fancy web application and you're planning to put it online. This can be done in many ways. In this article I'd like to cover one approach to deploy your backend system to your production server. We'll go through the following steps by example of a Laravel application but this can be applied to any other language or technology.Read More…