Update, 2011-11-16: Added some troubleshooting info
There are many reasons why you would want to install ssh2 for PHP on your server, mine was to allow me to use SFTP on a WordPress installation because my web host does not allow the use of FTP. Even if your host allows FTP I strongly urge you to use SFTP instead, since it uses SSH to encrypt all your data transfers. FTP sends your username and password as plain text!
Requirements for this tutorial
- PHP 5.2.x (I have used this on 5.2.9)
- LINUX (probably works on Windows too, but no details about that are described here, sorry)
- WordPress 3.0.1 (somewhat older versions should work too)
A general overview of ssh2 for PHP can be found at php.net. If you look at the requirements section on php.net you will see that two other libraries are required for this to work. They are OpenSSL and libssh2. I explain here how to install both of these as well.
First log in as root on your server on the command line and find out if OpenSSL is already installed by running the following command:
If you see something similar to the two lines below (might differ in version numbers) then OpenSSL is installed and you can skip to the next section.
If it is not installed, then you can do so by running yum or apt-get (depending on your system) as follows:
RedHat based systems
Debian based systems
You can verify if it is already on your system by searching for libssh2.so like this
If it is not installed then you will have to install it from source.
Create a temporary directory to download the source files to and then cd to the newly created temporary directory
1 2 3 4 5 6
Once these steps have been successfully completed delete the previously created directory.
Installing the ssh2 module for php
This is available via PECL and can be installed as follows
This should have placed the ssh2.so file in your php extensions directory. Now add
to your php.ini or whatever file your extensions are specified in. Depending on your installation this might be in a separate file instead of your php.ini. Consult you php installation documentation for more information. The way this is done varies depending on your distribution. Also worth mentioning is that you will need the zlib.so extension enabled as well. Make sure this is the case while you are editing the extension ini file and then restart apache.
Red Hat, CentOS, Fedora
Now make sure that ssh2.so is really loaded by executing the following command on the command line:
You should see something like
If you do not, then the ssh2.so file might have been placed in the wrong directory or you did not correctly configure your ini file to load it.
Generating the ssh keys
Wordpress needs to know the location of your ssh public and private keys. This is how you generate them: On the command line on your server login as the user that has access to the site. In my case I have the site installed as a virtual host and have a specific user that owns it.
1 2 3 4 5 6 7 8 9 10 11 12 13
Create an “authorized_keys” file by copying the public key in place
1 2 3 4 5
Now, when you log into the WordPress admin dashboard and perform an action that requires a file transfer operation, such as installing or updating a plugin, you should be presented with more options for the connection type. Aside from FTP, which was there before, you should also have SSH2. Select SSH2 and it will add a few fields to the form and it should look something like the screenshot below.
The FTP/SSH Username here is the user name for SSH for which you created the ssh keys earlier, NOT your ftp user. And the password is the passphrase that you created during the key generation step earlier, not the users password.
To avoid having to enter this information every time a file transfer is executed you can define a few constants in your WordPress configuration file wp-config.php:
1 2 3 4 5
Important: Be sure to move your wp-config.php file out of the doc root for added security.
Pecl installation of ssh2 fails
Check if autoconf is on your system but not in the path:
If it is there then
Pecl does not have write permissions to the tmp directories and fails with
Temporarily change permissions on the following folders.
Run the install again and then change the permissions back