Solved: WordPress asking for FTP credentials when upgrading

Problem

WordPress is asking me for FTP credentials when I was upgrading my WordPress to the latest version. The funny thing is my WordPress is installed in my localhost! Apache is installed in my laptop, and I have no FTP server here so it really doesn’t make any sense WordPress using FTP to upgrade itself.

Root Cause

All WordPress files should be owned by the Apache user. In Ubuntu, this is “www-data”.

It is NOT sufficient that the Apache user can access the files, so a CHMOD 777 just won’t work. It has to own them all.

Apparently, there are different “methods” of upgrading method. The choice of method is done in the background by WordPress by checking some environmental settings. The one that I need and want is the “direct” method, where it just downloads the file and extracts it onto the filesystem.

For WordPress to choose the “direct” method installation, it does the following checks:

  1. The directory where WordPress is installed must be writable by the Apache process;
  2. The UID of the Apache process is the same as the owner of the UID of a file temporarily created by the Apache user in the directory where you are installing WordPress.

Source Location

Observe these lines of code at /wp-admin/includes/file.php: get_filesystem_method(). It seems that not only does WordPress check if the directories are writable, but it checks if the Apache user OWNS the directories (or at least, if the Apache user owns the temporary file it creates)

    if ( $temp_handle ) {
        if ( getmyuid() == @fileowner($temp_file_name) )
            $method = 'direct';
        @fclose($temp_handle);
        @unlink($temp_file_name);
    }

Solution

You must give ownership of the whole site to the Apache user. You can do this by issuing the following command:

sudo chown -R www-data wordpress/

where www-data is the Apache user, and wordpress is the directory where you want your WordPress installation.

Solved: WordPress asking for FTP credentials when upgrading

Leave a comment