Update WordPress & Plugins over SSH

For security reasons, we don’t typically enable FTP on any of our servers. WordPress downloads core and plugins via FTP, so updating usually involves doing it locally and then deploying the changes. In most cases WordPress core isn’t included in the repo, so that has to be done manually : ssh into the server, wget the new version of WordPress, rm -rf the wp-contents directory, symlink in the one we just deployed, etc.

Instead why not just hit the update button right on the server (after testing for breakage locally of course) and forget about it?

Add this to your wp-config.php. I stick it at the top for easy access

1
2
3
4
5
6
7
8
define('FS_METHOD', 'direct'); // 'ssh' is also an option, but did not work for my setup
define('FTP_BASE', dirname(__FILE__)); // set this to your docroot
define('FTP_CONTENT_DIR', FTP_BASE.'/wp-content/');
define('FTP_PLUGIN_DIR ', FTP_CONTENT_DIR.'/wp-content/plugins/');
define('FTP_PUBKEY', $_ENV['HOME'].'/.ssh/id_rsa.pub'); // make sure you've added this as a deploy key before you do this
define('FTP_PRIKEY', $_ENV['HOME'].'/.ssh/id_rsa');
define('FTP_USER', `whoami`);
define('FTP_HOST', 'localhost:22'); // or whatever port you're using for SSH

Originally found here and modified to be a little more reusable.

Comments

    Caleb |

    Sweet, I never knew about this. I just updated some blogs and it’s a lot easier to update the plugins.

    Val |

    What is the way to copy all text from the snippet? Can do it only copying line by line in Safari.