PHP: Deploying PHP Applications with GitHub Actions

How to deploy your PHP code using GitHub Actions

👋 Welcome to the Stackhero documentation!

Stackhero offers managed PHP hosting designed for fast and reliable deployments:

  • Deploy your PHP application to production in seconds with a simple git push.
  • Use your own domain, with automatic HTTPS certificates configured for you. No manual steps required.
  • Take advantage of automatic backups, one-click updates, and clear, predictable pricing. Stackhero takes care of these details so you can focus on your code.
  • Enjoy strong performance and security on a private, dedicated infrastructure built specifically for your applications.

Streamline your workflow and save time. Getting started with Stackhero's PHP hosting for Laravel and Symfony only takes a few minutes.

GitHub Actions is a powerful tool that allows you to automate workflows, including deploying your PHP code directly to your production servers. In this guide, we will walk you through how to securely deploy your PHP application to both staging and production environments using GitHub Actions.

The recommended approach is to maintain two branches in your repository: staging and production. Whenever you push code to either of these branches, it will be automatically deployed to the corresponding Stackhero instance.

Having a staging instance is not mandatory. However, using both staging and production instances is strongly recommended to ensure smooth deployments and greater confidence when deploying to production. This practice aligns with industry standards and helps minimize potential risks.

To get started, you will need a GitHub account with your PHP code hosted in a repository.

First, log in to your Stackhero dashboard and create two PHP services, one for staging and one for production. For clarity, you may want to rename these services to "Production" and "Staging".

If you do not have a Stackhero account yet, you can create one for free in just two minutes and set up your managed PHP hosting with just a few clicks.

Example of production and staging servicesExample of production and staging services

SSH keys are the recommended method for allowing GitHub Actions to securely connect to your PHP service during deployment. In this guide, you will create a dedicated SSH key pair for this purpose.

To generate new SSH keys, run the following command on your computer:

ssh-keygen -C "" -f /tmp/ssh_key -N ""

Next, retrieve the public key by running:

cat /tmp/ssh_key.pub

Go to your Stackhero dashboard and select your "Production" PHP service. Click on "Configure" and follow these steps:

  1. Under SSH public keys, click on "Add a public key".
  2. Set the Description to "GitHub Action".
  3. Paste the public key you just retrieved into the Key field.

Get service settingsGet service settings

Now, add the private key to your GitHub project so it can be used during deployment:

  • Go to Settings, then select Environments and click on New environment.

    Configuring GitHub environmentsConfiguring GitHub environments

  • In the Name field, enter "production" and confirm.

    Setting the environmentSetting the environment

  • Click on the No restriction button and select Selected branches and tags.

    Setting environment restrictionsSetting environment restrictions

  • Click on Add deployment branch or tag rule, enter "production" in the Name pattern field, and click on Add rule.

    Setting environment branchSetting environment branch Setting environment branchSetting environment branch

  • Under Environment secrets, click on Add secret.

    Add secretAdd secret

Retrieve the private key you generated earlier by running:

cat /tmp/ssh_key

In the GitHub environment settings:

  • For Name, enter STACKHERO_SSH_PRIVATE_KEY and paste your private key into the Value field.

    Setting the SSH private key secretSetting the SSH private key secret

  • In the Environment variables section, click on Add variable.

    Setting variablesSetting variables

  • For Name, enter STACKHERO_ENDPOINT and paste your PHP service endpoint into the Value field. You can find this endpoint in your Stackhero dashboard.

    Setting the endpoint variableSetting the endpoint variable

If you have customized your service's domain name, use the customized version instead of <XXXXXX>.stackhero-network.com.

For security reasons, it is recommended to delete the generated SSH keys from your computer after setup:

rm /tmp/ssh_key /tmp/ssh_key.pub

Now you will set up a GitHub Actions workflow file to automate your deployments. Inside your Git repository, create a directory named .github/workflows and add a file called deploy-to-stackhero.yml with the following content:

# File: .github/workflows/deploy-to-stackhero.yml

name: Deploy to Stackhero
run-name: Deploy branch "${{ github.ref_name }}" to Stackhero

on:
  push:
    # List of branches that will trigger the deploy action following a git push
    # Do not forget to create a corresponding environment in GitHub (under "Settings" -> "Environments") for each branch
    # Then add the secret "STACKHERO_SSH_PRIVATE_KEY" and variable "STACKHERO_ENDPOINT" in that environment
    branches: [ "production", "staging" ]

jobs:
  Deploy:
    environment: ${{ github.ref_name }}
    runs-on: ubuntu-latest
    steps:
    - uses: stackhero-io/github-actions-deploy-to-stackhero@v1
      with:
        # The secret "STACKHERO_SSH_PRIVATE_KEY" and the variable "STACKHERO_ENDPOINT" should be defined in the corresponding GitHub environment (under "Settings" -> "Environments")
        ssh_private_key: ${{ secrets.STACKHERO_SSH_PRIVATE_KEY }}
        endpoint: ${{ vars.STACKHERO_ENDPOINT }}

Once you have added this file, you can commit your changes by running the following commands in your terminal:

git add -A .
git commit -m "Add GitHub Actions to deploy to Stackhero"

To create the production branch, you can run:

git checkout -b production

Then push your changes to GitHub:

git push --set-upstream origin production

This will push your code to the production branch on GitHub. GitHub Actions will then automatically run and deploy your code to the associated Stackhero instance. To check the deployment status, go to your GitHub project and click on Actions.

GitHub Actions that deployed to productionGitHub Actions that deployed to production

Congratulations! Your project is now set up for automatic deployment to production using GitHub Actions.

You can set up your staging environment in the same way as the production environment. Simply repeat the steps above, replacing production with staging where appropriate.

Once the settings are configured, you can create your staging branch as follows:

git checkout -b staging

Then push it to GitHub:

git push --set-upstream origin staging

GitHub Actions will automatically deploy the code from your staging branch to the PHP service you have set up for staging.

To help ensure your application remains reliable and secure, it is a good idea to protect your production and staging branches against direct pushes. For example, you can require that changes go through pull requests, with only those who have deployment rights allowed to review and merge the staging branch. Once your changes are validated in staging, authorized users can merge them into the production branch.

This approach ensures that only validated code reaches production, and allows you to test new features in staging before they go live, giving you and your team extra peace of mind.