Script Tips: ssh-key For Apache
For security purposes, most servers are usually locked up to allow access only via SSH. Setting up ssh-key for remote access to servers without password is simple. But what if you want to set up ssh-key access with user apache which is a pseudo user on most Linux installations and doesn’t have shell access? Ever tried to set up your webserver so it could run some automated scripts on a remote server via a web interface? If yes, then here’s the answer.
I was basically doing the same thing on my local network where I have several Linux RedHat Enterprise boxes and Sun Solaris boxes sitting together. I had a webserver on one of the Linux boxes but most of the other services I required was sitting on the Solaris platform. I sort of needed a web interface to dumb down the service and make the back end operations transparent to the end user. So I did the unthinkable and broke all security controls using ssh-key for apache. Don’t try this unless you’re sure the security’s not an issue :)
First, let’s walk through the basic steps of setting up ssh-key for a normal user. For this example, we’ll be using the following parameters:
- Main Server Name: X
- Main Server User: dummyX
- Remote Server Name: Y
- Remote Server User: dummyY
- Login to the server as the user you would like to create the ssh-key for.
- Generate the ssh-key authentication key. Use the default file location and leave empty when prompted for passphrase.
[dummyX@X ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dummyX/.ssh/id_rsa):
Created directory '/home/dummyX/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/dummyX/.ssh/id_rsa.
Your public key has been saved in /home/dummyX/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 dummyX@X - Create a directory ~/.ssh as user dummyY on server Y. Enter dummyY’s password when prompted.
[dummyX@X ~]$ ssh dummyY@Y mkdir -p .ssh
dummyY@Y's password: - Append dummyX’s public key to dummyY@Y:.ssh/authorized_keys and enter dummyY’s password again:
[dummyX@X ~]$ cat .ssh/id_rsa.pub | ssh dummyY@Y 'cat >> .ssh/authorized_keys'
dummyY@Y's password: - Once done, test the set up. You’ll see that you can now run ssh commands without being prompted for the user password:
[dummyX@X ~]$ ssh dummyY@Y hostname
Y
In order to set up ssh-key for apache, you’ll do the same steps as a normal user but you’ll require root access on your webserver installation. Here’s what you do.
- Change user to root:
sudo su - root - Create a .ssh directory on the apache home (/var/www) and change ownership to apache user:
[root@X ~]$ mkdir /var/www/.ssh
[root@X ~]$ chown -R apache:nobody /var/www/.ssh - Generate the ssh-key authentication key as user apache using sudo. Use the default file location and leave empty when prompted for passphrase.
[root@X ~]$ sudo -u apache ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/www/.ssh/id_rsa):
Created directory '/var/www/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/www/.ssh/id_rsa.
Your public key has been saved in /var/www/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 apache@X - Append apache’s public key to dummyY@Y:.ssh/authorized_keys and enter dummyY’s password again:
[root@X ~]$ sudo -u apache cat .ssh/id_rsa.pub | ssh dummyY@Y 'cat >> .ssh/authorized_keys'
dummyY@Y's password: - Once done, test the set up. You’ll see that you can now run ssh commands without being prompted for the user password:
[dummyX@X ~]$ sudo -u apache ssh dummyY@Y hostname
Y
And to test your setup using php/shell combination. Create a php test file:
<?php // test.php:
echo "Running test.sh from test.php"
echo shell_exec("test.sh");
?>
And a test shell file (don’t forget to grant executable access to apache):
# test.sh:
echo "USER: `id -un`"
echo "HOST: `hostname`"
echo "REMOTE USER: `ssh dummyY@Y id -un`"
echo "REMOTE HOST: `ssh dummyY@Y hostname`"
If you point your browser to test.php, you should see the following output displayed:
Running test.sh from test.php using apache user
USER: apache
HOST: X
REMOTE USER: dummyY
REMOTE HOST: Y
With the authentication keys properly set up for ssh access, you’ll be able to perform ssh commands as well as other ssh based operations (e.g. sftp and scp) without being prompted for a user password. And if you set up the same for your apache user, you’ll be able to create a simple web interface to access all your automation scripts. Have fun :)
Related posts:
Wordpress On A Stick?
Tech: Photos of exploding laptop
PHP Script: Spamming A Website - PHP Spambot
More Friday The 13th
Movie: Percy Jackson And The Lightning Thief

ming tsai Said,
November 12, 2009 @ 7:52 pm
nice, thx for the reference! it works for mine RHELs.
rafa Said,
June 20, 2011 @ 11:17 pm
Thanks a lot for this! Worked really well! Although now user apache is www-data and the group is www-data. The rest remains just perfect!
Tabraiz Anwer Said,
October 20, 2011 @ 5:15 pm
good work my friend..!!
when i was trying before reading this link i was using user nobody:nobody..
so i have to clear one thing why this is [ apache : nobody ]why it is not [ nobody : nobody ] ?
gbyeow: The apache service is run by the user apache. Boils down to which user is the owner of your apache server process.
Shiv Said,
December 12, 2011 @ 2:36 pm
I am still facing the problem. In httpd.conf file i am having \’Group apache User apache\’.
Document Root /var/www/html/
I have create directory /var/www/html/.ssh and created rsa keys.
Done with other stuff too
But from browser it\’s not working. Although it\’s working from CLI
The errors m getting from error_log file is
[Mon Dec 12 12:03:10 2011] [error] [client 172.xx.x.xx] PHP Warning: ssh2_connect(): Unable to connect to 172.xx.xx.xx1 on port 22 in /var/www/html/connection.php on line 31
[Mon Dec 12 12:03:10 2011] [error] [client 172.xx.x.xx] PHP Warning: ssh2_connect(): Unable to connect to 172.xx.xx.xxx in /var/www/html/connection.php on line 31
gbyeow: I think you’re trying to use the ssh2_connect function in PHP. Try dumping whatever you need to do into a shell script and calling that shell using PHP shell_exec() instead. The instructions given above are for the client setup only. PHP has it’s own contained environment. It’s probably not able to pick up the keys that you created.
Matty C Said,
January 12, 2012 @ 5:51 am
Doing this for a regular user works like a charm. However, when I do the above steps for the apache user, it still asks and prompts for a password. I\’m trying to use this in conjunction with RSYNC. It appears it only works for the root user. Any advice?
Matty C Said,
January 12, 2012 @ 6:19 am
nm…the above comment no longer applies! Thanks!