Update WordPress on the command line (SSH) without downtime

WordPress installation failed: could not create directory

If you’re a webdeveloper and set up a lot of WordPress-websites, you get a lot of inquiries to update WordPress as well. And sometimes, for instance if the client is running on a VPS or poorly managed server, WordPress won’t update the easy way. Continue reading if you’re tired of these types of messages over and over again:

Unable to locate WordPress Content directory


Downloading update from https://downloads.wordpress.org/release/nl_NL/wordpress-4.4.zip…
Unpacking the update…
Could not create directory OR Could not copy file wp-mail.php
Installation Failed

Heck, you’ve even tried to define all sorts of other settings in wp-config.php like
define('FS_METHOD', 'ftpext');
define('FTP_BASE', '/domains/mydomain.com/public_html/');
define('FTP_CONTENT_DIR', FTP_BASE.'wp-content/' );
define('FTP_PLUGIN_DIR ', FTP_CONTENT_DIR.'plugins/' );
define('FTP_USER', 'myuser');
define('FTP_PASS', 'mypassword');
define('FTP_HOST', 'myhost.com');
define('FTP_SSL', false);

Update WordPress hosted on a VPS

If the above sounds familiar and you don’t want to waste anymore time on updating WordPress, connect to your site through SSH, become root, and do the following below (assuming you’ve made backups, didn’t do anything fancy/hacky in the WordPress-core files, etc. etc.).
Copy and paste the following into a text-editor, adjust path on line number 1, copy, paste in terminal. Your update is executed in about 5 seconds.

mkdir customupgrade/
mkdir customupgrade/old/
wget https://nl.wordpress.org/wordpress-latest-nl_NL.tar.gz -P customupgrade/
tar xfz customupgrade/wordpress-latest-nl_NL.tar.gz -C customupgrade/
rm -rf customupgrade/wordpress/wp-content/
yes | mv index.php customupgrade/old/
yes | mv wp-includes customupgrade/old/
yes | mv wp-admin customupgrade/old/
yes | mv wp-blog-header.php wp-login.php wp-signup.php wp-activate.php wp-comments-post.php wp-links-opml.php wp-mail.php wp-trackback.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php customupgrade/old/
yes | mv customupgrade/wordpress/* .
mv customupgrade/old/ oldwordpressbackup/
rm -Rf customupgrade/

Now login to your website (YOURDOMAIN/wp-admin) and WordPress will ask you to upgrade the database. And… you’re done.

If something fails, you have a copy of your old WordPress-installation in the “oldwordpressbackup” folder (or if one of the commands fails: it’s located in customupgrade/old).

Set the correct permissions to all WordPress-files and folders

Oh: if you’re at it, you need to set the correct permissions to all files again. For example: (credits: http://stackoverflow.com/a/18352747)

chown apache:apache -R *
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

Need more information on what we were doing? Here goes:

1) we go into the public_html folder
2) create a directory called “customupgrade” with subfolder “old” (we store the old files here)
3) download the new WordPress file (in this case the Dutch 4.4 pack) into the “customupgrade”-folder
4) untar the file
5) remove the wp-content directory from the downloaded archive (we are keeping our own themes and plugins).
6) move the index.php into a backup-location
7) create a custom message saying the site is under maintenance
8,9,10) move all old files into backup-location
11) move the new files in from the archive (the index.php is being overwritten at this point, ending the maintenance mode)
12) rename the backup-folder
13) cleanup the temporary upgrade folder

Geef een reactie