How to make Wordpress work under linux (solve the FTP login request)

Problem

WordPress cannot install, delete or update plugins or themes. WordPress asks you for your FTP server login credentials.

WordPress cant install or remove plugins or themes:

The reason for this, may be that wordpress checks if the userid it is running under, is the same as the owner of the file or folder it tries to edit.

The problem seems to be located at line 876 in the method get_filesystem_method in wordpress/wp-admin/includes/file.php:

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

This means, when wordpress is running, its userid is www-data (apache2), but the file owner may be Chris, so it gives an error.

You want to own your websites files, you don’t want to change the owner to www-data (apache2) although that would work:

You can do that by executing chown www-data:www-data * -R when you are in the wordpress directory. And then add yourself to www-data group by executing: usermod -a -G www-data Chris

Solution

If you are using apache2 virtualhosts on your server, you can make a vhost run under a different user with Apache2 mpm-itk.

mpm-itk is an apache2 module that lets you change the user and group id the vhost is running under!

There is no need to use suPHP when you do it this way.

Installation

As root, install the module:

apt-get install apache2-mpm-itk

Configuration

You configure this on each of your virtualhosts. The vhosts configuration files are located in the /etc/apache2/sites-available/ folder.

Edit your vhost config file, and inside the  <VirtualHost *:80> tag, add this:

<IfModule mpm_itk_module> AssignUserId Chris www-data </IfModule>

Important: Remember to substitute “Chris” with the username you want to run your vhost (and  wordpress installation) under.

Then restart apache2 with:

/etc/init.d/apache2 restart

Verification

To verify which user your vhost is running under, you can create a file: user.php with the code:

<?php system("id"); ?>

Open the php file in your webbrowser, and that will output which is the owner and group of the running vhost.

Before, when wordpress asked for your ftp credentials,  it would output something like:

uid=(www-data) gid=(www-data) groups=(www-data)

But after fixing it, it should now display something like:

uid=1000(Chris) gid=(www-data) groups=(www-data)

So your vhost should now be running under your user, and apaches group.

Permissions

Now we just need to set the right permissions for the wordpress files, set yourself as owner, and apache as group recursively on all wordpress files like this:

chown Chris:www-data wordpress_folder -R

Then set the right permissions on directories and files like this:

find wordpress_folder -type d -exec chmod 750 {} \;  
find wordpress_folder -type f -exec chmod 640 {} \;

That should set the lowest needed permissions for wordpress!

Please share this article.

comments powered by Disqus