Advertisement

Quick Tip: Upgrade Your WordPress Site via SSH

by
Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

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

Advertisement