5 ways to deploy PHP applications

Success
In this article, we compare 5 types of workflows that let you deploy a PHP application, so that you can choose the one which works best for you.

PHP delivery overview

The deployment process of web apps depends on a wide range of factors: back and front-end architecture, type of server or IaaS platform (AWS, GCP, Azure Portal, etc.), infrastructure limitations, even team preferences towards specific tools and frameworks. A most generic type of workflow for delivering PHP apps usually involves the following steps:

  1. Run unit tests on PHP application
  2. Run composer install to download dependencies
  3. Compile assets with Vite, Webpack, or another bundler
  4. Upload to web server with an FTP client or PHP deployment tool

Of course, different web applications may require different steps. For example, you may want to configure required app services (libraries, databases, etc.), populate environment variables, run post-deployment scripts on the server – everything depends on the specifications of your PHP app, company workflow, and hardware at disposal.

Stop everything and read this now

Before we proceed with deploying PHP apps, there are two things that need to be emphasized every single time the word "deploy" comes into play:

I. Keep everything in version control!

Application source code, configuration files, documentation, db migration scripts — all of them should be kept safe and sound in Git, be it a GitHub repository, Bitbucket, GitLab, or any other provider.

Of course, to every rule there are exceptions (especially in IT), which brings us to the second thing:

II. Never store dependencies and compiled apps in version control!

Keeping dependencies and artifacts in the Git repository will cause it to bloat and sooner or later will mess up your entire project. This leads to one thing that developers hate doing: solving conflicts in code.

Info
More reasons why you shouldn't store dependencies in your local Git repository (or remote) have been covered in our guide to building and deploying frontend apps.

How to deploy PHP projects: 5 workflows

Now that we know what not to do, let's proceed to the 5 ways of proper PHP deployment.

#1. Deploy repo w/o dependencies and artifacts and build your app on server

  1. First, we need to build and test the app. We can do it either on a local machine (CI server), or in a cloud service
  2. Once the tests pass, we can upload the source code to the server via FTP, SFTP or Rsync (in this case without dependencies and artifacts).
  3. The final step is downloading the dependencies and building the app by running Composer on the server via SSH.

Translated into a deployment tool pipeline, the whole process looks like this:

Image loading...A pipeline which builds the web app on the server

Pros & Cons

Success
- The build environment of the PHP app is exactly the same as the running environment. - Dependencies will download faster as they're fetched from the closest mirror.
Warning
- If you don't employ any deployment practice to minimize the downtime, the time required to download the dependencies and build the application may extend it. - Even on a small change, the build time may be long and impact the performance of the production server.

#2. Deploy repo with dependencies and artifacts

In this workflow, PHP applications are first compiled and tested, then uploaded to the web server with dependencies and artifacts using a deployment tool:

Image loading...A pipeline which builds the web app locally before the deployment

Pros & Cons

Success
- The production server is not stressed with the build. - The application on production is the same as the application from the test server. - You don't need SSH access to the server to run scripts = any old school FTP will do.
Warning
- You must provide the same build environment as the running environment for the application. - Since we deploy everything from the source code repository, the upload time can be long.

#3. The Git variation

This deployment strategy is basically the same as #1, but requires Git installed on the production server. The deployment from the source code repository is made with git push instead of regular file upload. Then, in Git, the post-receive hook triggers building the application code.

Image loading...A pipeline which builds the web app and pushes the files to a Git server

Pros & Cons

Success
- Git deployment is faster, because only changesets are deployed - You don't need to run SSH scripts, because the webhook will call them on the server
Warning
- If you don't use any mechanism to minimize the downtime (e.g. atomic deployment), the time required to download the dependencies and build the application may extend the downtime - The build time may be long and impact the performance of the production server

#4. Zero-downtime / Atomic deployment

The workflows above have one flaw: the downtime. This means your web app will not be available for the client during the deployment. The solution to that is very simple: deploy and build the application in a different folder than the one from which it's served.

Image loading...Atomic deployment

The process involves creating a couple of directories on the server:

  • /current - a symbolic link to the web app's current version in the releases directory to which your web server points.
  • /releases - contains the history of uploaded releases. For each version a directory with the name of the revision tag is created.
  • /deploy-cache - used for storing new files during the deployment. Once the upload has finished, its contents are copied to a new directory in the /releases directory.

Here's how it works:

  1. A new version of the web app is uploaded to deploy-cache
  2. The contents of deploy-cache are copied to /releases/${revision}
  3. The current symbolic link is switched to /releases/${revision}

Image loading...An atomic deployment template which creates and switches the upload directories on the server

Pros & Cons

Success
- Downtime reduced basically to zero (the time required to create a symlink) - Instant rollback – the previous version of application remains on the server after the deployment; all you need to do is switch back the symlink
Warning
- More space on the web server is required to keep the previous revisions - A lot of scripts to write (unless you use a preconfigured template)

#5. Docker deployment

The last method to deploy a PHP application involves containerization with Docker – a virtualization method that allows you to define the working environment of your application in a single text file called Dockerfile. The file is then used to build a Docker image with your app that can be launched in any environment supporting Docker (Linux/MacOS/Windows).

Here's a PHP Docker example written down to a Dockerfile:

docker
FROM php:8.3-cli
RUN apt-get update -y && apt-get install -y zip unzip git
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
RUN docker-php-ext-install pdo_mysql
WORKDIR /app
COPY . /app
RUN composer install --no-dev --optimize-autoloader

CMD ["php", "-S", "0.0.0.0:8181", "-t", "public"]
EXPOSE 8181
Info
The CMD line uses PHP's built-in web server, which is fine for a demo. In production, base the image on php:8.3-fpm or php:8.3-apache and serve the app with a proper web server.

The file can be used to build a Docker image:

bash
docker build -t my-image .
$

Once the image is built, you can launch your application in a Docker container isolated from its host:

bash
docker run -p 8181:8181 my-image
$

On top of that, Docker images can be pushed and pulled from a Docker registry. This way you can easily build an image on one server or PC, and run it on another. In this case, the whole delivery process will look like this:

  1. Test application
  2. Build Docker image with application
  3. Push application to Docker registry
  4. SSH to live server and run
bash
docker pull your-username/my-image:latest
docker run -d -p 80:8181 your-username/my-image:latest
$$

Image loading...A pipeline which builds a Docker image and runs it on a server

Pros & Cons

Success
- The application works on every type of setup which eliminates the "strange, it works for me" error - Easy rollback – you just run the previous version of the image - Build configuration and application environment are documented in the Dockerfile in the repository
Warning
- Yet another tech that adds to the software stack - You need a container registry and a server with Docker installed

Example: PHP deployment pipeline in YAML

Every workflow above can be set up in the visual pipeline editor or defined as code in YAML. The pipeline below follows workflow #2: on every push to main, it installs the Composer dependencies, runs the unit tests, removes the dev dependencies, uploads the files to a server added as an SSH target named production-server, and runs a post-deployment command over SSH:

yaml
- pipeline: php-deploy
  name: Test and deploy PHP app
  events:
  - type: PUSH
    refs:
    - main
  actions:
  - action: Install dependencies and run tests
    type: BUILD
    docker_image_name: library/composer
    docker_image_tag: "2"
    commands: |-
      composer validate
      composer install
      vendor/bin/phpunit
      composer install --no-dev --optimize-autoloader
  - action: Upload files to the server
    type: TRANSFER
    input_type: BUILD_ARTIFACTS
    local_path: /
    targets:
    - production-server
  - action: Run post-deployment commands
    type: SSH_COMMAND
    targets:
    - production-server
    commands: |-
      cd /var/www/app
      php artisan migrate --force
Tip
The last action runs Laravel database migrations as an example - replace it with the commands your app needs. To avoid downtime during the upload, use the atomic deployment template (workflow #4). The YAML helper generates the YAML for any action, and the full list of fields is in the YAML schema.

Additional resources

Jarek Dylewski

Jarek Dylewski

Customer Support

A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.

Nov 9, 2026
Share