Deploy a PHP App (2024)

This guide explains how to deploy a PHP application to Koyeb using Git-driven or Docker-based deployment. The application can be built using either native buildpacks or from a Dockerfile.

To successfully follow this documentation, you will need to have a Koyeb account (opens in a new tab). You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.

You can deploy and preview the PHP application from this guide using the Deploy to Koyeb button below.

Deploy a PHP App (1) (opens in a new tab)

You can consult the repository on GitHub (opens in a new tab) to find out more about the example application that this guide uses.

Create the PHP app

Get started by creating a basic PHP application that we will deploy on Koyeb. The buildpack for PHP uses the composer.json file to identify PHP repositories, so you can install Composer (opens in a new tab) if your application requires it, but this demonstration will not need it. If you'd like to test your PHP locally, you can optionally install PHP (opens in a new tab) locally as well.

To begin, create a new project directory and a web subdirectory that will hold the actual PHP files. Navigate into the web directory:

mkdir -p example-php/webcd example-php/web

Create a new file called index.php with the following contents:

web/index.php

<!DOCTYPE html><html> <head> <title>Example PHP page</title> </head> <body> <?php echo '<h1>Hello world!</h1>'; echo '<p>This page uses PHP version ' . phpversion() . '.</p>'; ?> </body></html>

Save and close the file when you are finished.

If you have PHP installed locally, you can test this page by executing the following command in the web directory:

php -S 127.0.0.1:8000

If you visit 127.0.0.1:8000 in your web browser, you should see a page that includes the "Hello world!" message as well as information about the version of PHP that is executing the PHP code block.

Create the project management files

Next, we'll create a few files that will help Koyeb deploy the project.

First, create a composer.json file. The PHP buildpack (opens in a new tab) that Koyeb uses looks for a composer.json file to identify PHP projects. While many PHP projects use Composer, our project has no dependencies, so we can use an empty JSON object.

Navigate back into the root directory of your project and add a composer.json file with the following contents:

composer.json

{}

This example application should run on any modern version of PHP. If your application requires a specificPHP version, you can set the PHP version in the composer.json file as described in the build with PHPpage.

Next, create a Procfile in the root directory. This will be used to define what web server to use to serve the project (Apache (opens in a new tab) or Nginx (opens in a new tab)) and to direct the server to the project's "document root" (the directory where our PHP files are located).

Create a Procfile with one of the two lines depending on which server you prefer.

For Apache:

Procfile

web: heroku-php-apache2 web/

For Nginx:

Procfile

web: heroku-php-nginx web/

Create a Dockerfile for the project (Optional)

We can build and run our PHP project on Koyeb using the native PHP buildpack, but we can also optionally build from a Dockerfile for more control. To make it possible to build a container image for our application, we just need to create the Dockerfile. We'll also define a .dockerignore file to tell the builder what files to skip when creating the image.

First, define a .dockerignore file in your main project directory. Inside, paste the following contents:

.dockerignore

.git.gitignoreDockerfile.dockerignore/vendor/composer.phar

This file tells Docker to not include Git files, the Docker files themselves, and any artifacts that Composer might produce when managing dependencies. This helps ensure that the image we build is not bloated and that the build completes faster.

Next, create a new file called Dockerfile within the main project directory. Inside, paste the following contents:

Dockerfile

FROM php:apache WORKDIR /var/www/htmlCOPY web . ENV PORT=8000EXPOSE ${PORT} RUN sed -i 's/Listen 80/Listen ${PORT}/' /etc/apache2/ports.conf

This Dockerfile is based on the Apache 2 version of the official PHP Docker image (opens in a new tab). The instructions copy contents of the web directory to the document root folder. Afterwards, it sets a default port modifiable by the PORT environment variable and changes the configuration file so that the server listens accordingly.

If you have Docker installed locally, you can build and test the image on your computer and optionally upload it to a registry. You can deploy container images from any container registry to Koyeb.

We can also build the Dockerfile directly from the repository when we deploy, which is useful as a way of automatically deploying when changes occur. We will demonstrate this method as one of the options in this guide.

Push the project to GitHub

In the project directory, initialize a new git repository by running the following command:

git init

Next, download a basic .gitignore file designed for Composer projects from GitHub:

curl -L https://raw.githubusercontent.com/github/gitignore/main/Composer.gitignore -o .gitignore

Next, add the project files to the staging area and commit them. If you don't have an existing GitHub repository to push the code to, you can create a new one and run the following commands to commit and push changes to your GitHub repository:

git add :/git commit -m "Initial commit"git remote add origin [email protected]:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.gitgit push -u origin main

Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

Deploy to Koyeb using git-driven deployment

Once the repository is pushed to GitHub, you can deploy the PHP application to Koyeb. Any changes in the deployed branch of your codebase will automatically trigger a redeploy on Koyeb, ensuring that your application is always up-to-date.

To deploy the PHP app on Koyeb using the control panel (opens in a new tab), follow the steps below:

  1. Click Create Web Service on the Overview tab of the Koyeb control panel.
  2. Select GitHub as the deployment option.
  3. Choose the GitHub repository and branch containing your application code. Alternatively, you can enter our public PHP example repository (opens in a new tab) into the Public GitHub repository at the bottom of the page: https://github.com/koyeb/example-php
  4. Choose the Builder for your project. We can use either a Dockerfile or buildpack for this repository.
  5. Name the App and Service, for example example-php.
  6. Click the Deploy button.

A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy to Koyeb from a container registry

If you chose to build a container image for the PHP application, you can optionally deploy the application from a container registry instead of from GitHub.

To deploy a pre-built PHP container image on Koyeb using the control panel (opens in a new tab), follow the steps below:

  1. Click Create Web Service on the Overview tab of the Koyeb control panel.
  2. Select Docker as the deployment option.
  3. Choose the container image and tag from your registry and click Next to continue.
  4. Name the App and Service, for example example-php.
  5. Click the Deploy button.

A Koyeb App and Service will be created. The container image will be pulled from the container registry and a container will be deployed from it. Once the initialization is complete, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app.

Deploy a PHP App (2024)
Top Articles
What is the Cash App Bitcoin Purchase Limit, and How Do I Increase It?
Guide to Freight and Cargo Insurance | BJP Insurance
Is Paige Vanzant Related To Ronnie Van Zant
360 Training Alcohol Final Exam Answers
Emmalangevin Fanhouse Leak
Espn Expert Picks Week 2
Sitcoms Online Message Board
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
Dumb Money, la recensione: Paul Dano e quel film biografico sul caso GameStop
Directions To Advance Auto
Publix Super Market At Rainbow Square Shopping Center Dunnellon Photos
Dallas Craigslist Org Dallas
Dover Nh Power Outage
The Blind Showtimes Near Amc Merchants Crossing 16
Aerocareusa Hmebillpay Com
Plaza Bonita Sycuan Bus Schedule
THE FINALS Best Settings and Options Guide
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
Rs3 Ushabti
Weve Got You Surrounded Meme
Busted Mugshots Paducah Ky
Farm Equipment Innovations
Taylored Services Hardeeville Sc
Toonkor211
Jail Roster Independence Ks
Noaa Marine Forecast Florida By Zone
Planned re-opening of Interchange welcomed - but questions still remain
Duke Energy Anderson Operations Center
Kristen Hanby Sister Name
Chicago Pd Rotten Tomatoes
Rund um die SIM-Karte | ALDI TALK
Elanco Rebates.com 2022
Melissa N. Comics
Palmadise Rv Lot
Xemu Vs Cxbx
Synchrony Manage Account
Usf Football Wiki
Hell's Kitchen Valley Center Photos Menu
Michael Jordan: A timeline of the NBA legend
Wunderground Orlando
SF bay area cars & trucks "chevrolet 50" - craigslist
Dwc Qme Database
The Wait Odotus 2021 Watch Online Free
The power of the NFL, its data, and the shift to CTV
Sofia Franklyn Leaks
Yourcuteelena
17 of the best things to do in Bozeman, Montana
Online TikTok Voice Generator | Accurate & Realistic
91 East Freeway Accident Today 2022
211475039
Fishing Hook Memorial Tattoo
Coors Field Seats In The Shade
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 5466

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.