Deploy PHP application

I am trying to deploy a PHP-application to Fly.io, but with no luck. I just get the “Hello from Fly”-message.

I used to have a Heroku instance, added the fly.toml and I get no error messages when doing fly deploy.

My repository is here? GitHub - vih/vih-viggo-standalone: Stand alone app for getting calendar items from Viggo

Is it because I am missing a Dockerfile? - and how should the file look to be able to deploy this very simple PHP-app?

Via the Builders and Fly documentation:

Per the App Configuration documentation, build.image is the builder option to skip the building process by specifying an image that has already been built.

[build]
  image = "flyio/hellofly:latest"

So, in your configuration, Fly is ignoring your application entirely and deploying flyio/hellofly:latest which is the source of the “Hello from Fly” message you’re seeing.

I haven’t used the buildpacks before, but as I understand it from the documentation, your configuration is almost correct. The changes that are required are:

  1. Remove the build.image
  2. Select a php buildpack
  3. Select a builder that is compatible with the php buildpack

I found paketo-buildpacks/php, which says in the README that it’s compatible with 2 builders: paketobuildpacks/builder-jammy-full and paketobuildpacks/builder:full. Unfortunately, PHP 7.4 is at end of life, so it’s not supported by either of these builders: you’ll need to upgrade your app to PHP 8 if you wish to use these builders. The good news is, based on a quick scan of your code; there’s no reason you need PHP 7.4, so if you modify your composer.json to allow for PHP 8, it should work fine:

    "require": {
-        "php": "7.4.*",
+        "php": "~8",

Run composer update php to apply those changes to your composer.lock. Then you can update your fly.toml to use the correct builder and buildpack:

[build]
- builder = "paketobuildpacks/builder:base"
+ builder = "paketobuildpacks/builder:full"
- buildpacks = ["gcr.io/paketo-buildpacks/nodejs"]
+ buildpacks = ["gcr.io/paketo-buildpacks/php"]
- image = "flyio/hellofly:latest"
-  [build.args]
-   PHP_VERSION = "7.4"
-   NODE_VERSION = "14"

You’ll also need to change from port 80 to port 8080. Your final fly.toml will be:

# fly.toml file generated for vih-kalendersiden on 2022-12-09T08:21:47+01:00

app = "vih-kalendersiden"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
  builder = "paketobuildpacks/builder-jammy-full"
  buildpacks = ["gcr.io/paketo-buildpacks/php"]

[env]
APP_URL = "https://vih-kalendersiden.fly.dev"
PORT = 8080

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 8080

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

The final change is to delete the Procfile, then run fly deploy and your application should be good to go :slight_smile: I have submitted this all as a Pull Request to your repository: fix: Build application for Fly deployment using buildpack by shrink · Pull Request #9 · vih/vih-viggo-standalone · GitHub

1 Like

Hi there!

I have checked the solution and can verify it works. Thanks @shrink for contributing!
If you have any other questions, don’t hesitate to reach out.

Johannes

Thanks @shrink. It was a really good starting point.

Do you know how I can enable php-extensions in the buildpack? One of my dependencies has some dependencies that is not satisfied.

Paketo Buildpack for Composer Install 0.2.0
  Executing build process
  Building new layer /layers/paketo-buildpacks_composer-install/composer-packages
  Running 'composer install'
exit status 2
    Installing dependencies from lock file
    Verifying lock file contents can be installed on current platform.
    Your lock file does not contain a compatible set of packages. Please run composer update.
    
      Problem 1
        - kigkonsult/icalcreator is locked to version v2.41.71 and an update of this package was not requested.
        - kigkonsult/icalcreator v2.41.71 requires ext-zlib * -> it is missing from your system. Install or enable PHP's zlib extension.
    
    To enable extensions, verify that they are enabled in your .ini files:
        - /layers/paketo-buildpacks_composer-install/composer-php-ini/composer-php.ini
        - /layers/paketo-buildpacks_php-dist/php/etc/buildpack.ini
        - /layers/paketo-buildpacks_php-dist/php/etc/php.ini
        - /layers/paketo-buildpacks_php-dist/php/etc/buildpack.ini
        - /layers/paketo-buildpacks_php-dist/php/etc/php.ini
    You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode.
    Alternatively, you can run Composer with `--ignore-platform-req=ext-zlib` to temporarily ignore these required extensions.
ERROR: failed to build: exit status 1
Error failed to fetch an image or build from source: executing lifecycle: failed with status code: 51

Reviewing the documentation, it looks like, unfortunately, zlib isn’t supported within this buildpack and there is no option to install custom extensions. The package you depend on (kigkonsult/icalcreator) didn’t introduce the dependency on zlib until version 2.41.39 (earlier this year), so you could downgrade to 2.41.30. Alternatively, you could bypass the dependency check and cross your fingers that the code you’re relying on doesn’t actually need zlib: I couldn’t find any apparent reason it would be required… to do that you could add the build argument suggested in the error message, e.g:

[build]
  builder = "paketobuildpacks/builder:full"
  buildpacks = ["gcr.io/paketo-buildpacks/php"]
  [build.args]
    BP_COMPOSER_INSTALL_OPTIONS = "--ignore-platform-req=ext-zlib"

If downgrading to version 2.41.30 isn’t an option, and ignoring the dependency on zlib isn’t an option, then I think (without experience of buildpacks) the next best option would be to use your own Dockerfile instead of relying on a buildpack. If you don’t have Docker experience, that might be a painful switch but it would be straight forward if you know what you’re doing with Docker :slight_smile:

Wow. @shrink thank you so much for your help.

I downgraded the dependency and was able to get it built now.

However, I keep getting this error, which I suppose is openssl not properly setup.

PHP Warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in /workspace/index.php on line 129

I have a ´require` in my composer set to:

    "require": {
        "php": "~8",
        "ext-openssl": "*"
     }

As here: vih-viggo-standalone/composer.json at master · vih/vih-viggo-standalone · GitHub

So it should setup the extension requirement as per How to Build PHP Apps with Paketo Buildpacks - Paketo Buildpacks

But maybe there is something that i do not understand properly.

According to this comment on GitHub the openssl extension is not automatically available through the composer require behaviour due to an issue that is yet to be fixed. However, according to this issue there is a workaround whereby you create your own .ini file and enable the extension there:

diff --git a/.php.ini.d/extension.ini b/.php.ini.d/extension.ini
new file mode 100644
index 0000000..cb58b9c
--- /dev/null
+++ b/.php.ini.d/extension.ini
@@ -0,0 +1 @@
+extension=openssl.so

I ran a quick test and it seems to work – /calendar/proxy/vih loads, no error!

1 Like

@shrink. Thank you so much. You totally saved my weekend. Have a really good day.

1 Like