Generating SSH keys for 'apache' user

Generating SSH keys for 'apache' user

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Generating SSH Keys for the apache User

When setting up a webhook to trigger code deployments, you often encounter a classic permission mismatch: your application runs as one user, but your deployment keys were generated by another. In your specific scenario, you created an SSH key pair using root, yet the PHP script executing git pull runs under the apache user (or www-data on some distributions). This results in a "Permission denied" error because the private key does not exist for the user attempting to authenticate with GitHub.

The core issue is that SSH keys are tied to the specific user account they reside in. A key generated by root lives in /root/.ssh, while the apache user looks for credentials in /home/apache/.ssh or /var/www/apache/.ssh. You cannot simply switch contexts inside a terminal session to generate keys for a service account; the shell environment determines which home directory is used.

To resolve this, you must generate the key pair explicitly for the apache user. The most straightforward approach is to use sudo to switch into that context without logging in as root. Run the following command:

sudo -u apache ssh-keygen -t ed25519 -C "apache-deploy-key"

This command invokes the ssh-keygen utility specifically for the apache user. The -t ed25519 flag selects the Ed25519 algorithm, which is currently preferred over RSA due to its smaller size and faster performance. The -C option adds a comment to the key file, helping you identify it later without having to inspect the raw content of id_ed25519.pub.

After running this command, ssh-keygen will prompt for a file location. Accept the default path (e.g., /home/apache/.ssh/id_ed25519) or specify a custom one if you prefer. You will also be prompted to set a passphrase. For automated deployment scripts like your webhook handler, it is standard practice to leave this empty by pressing Enter at the prompt. Encrypting the key with a passphrase prevents the private key from being used automatically, which would cause the script to hang waiting for manual input every time GitHub receives the push notification.

Once generated, you must copy the public key (id_ed25519.pub) and paste it into your GitHub account settings under Settings > SSH and GPG keys. If this is a new key, it will appear in the list immediately. You do not need to regenerate the private key for every deployment; that single pair serves as the long-term credential.

It is crucial to verify file permissions immediately after generation. The apache user must have exclusive read/write access to the private key. Run the following commands to ensure compliance:

sudo -u apache chmod 600 /home/apache/.ssh/id_ed25519
sudo -u apache chmod 700 /home/apache/.ssh
sudo chown -R apache:apache /home/apache/.ssh

Incorrect permissions, such as a world-readable private key, will cause SSH to refuse the connection for security reasons. If you previously generated keys as root in /root/.ssh, those keys remain useless for this specific deployment flow unless you configure your PHP script to run as root, which is generally discouraged due to security risks.

If you are using Laravel or building a web application on top of it, consider storing the public key in an environment variable or a configuration file rather than hardcoding it in your repository. The private key must remain secure and never committed to version control. By ensuring the apache user holds valid credentials, your webhook will successfully trigger git pull origin master, resolving the permission denied error without needing to compromise system security.

Our network