Guides
PHP application

Quickstart guide for a PHP application

In this guide, you'll learn the process of deploying a PHPapplication on Wasmer Edge. We will cover installation of the CLI, setting up a new PHP application, and deploying it.

Deploying a PHP application

Install Wasmer

Click here for instructions on how to install Wasmer if you haven't done it already!

Log in into Wasmer

Create a new account in Wasmer (opens in a new tab). Then, log in into the Wasmer CLI and follow the provided steps to provide the CLI access to your Wasmer account.

wasmer login

Deploy!

We begin creating a new empty directory.

mkdir my-new-php-app
cd my-new-php-app

Then, running a single command, we can setup a php application.

wasmer deploy --template=php-starter

This will prompt you for the following:

  • App owner: This is the owner of the app. It can be your username or an organization; if you're logged in, the command will prompt you tochoose from your namespaces: by default, it will be your username.
  • App name: This is the name of your app. By default, it will be the name of the current directory.

Expect to see output similar to this:

It seems you are trying to create a new app!
 Who should own this app? · wasmer-user
 What should be the name of the app? · php-starter
 Select the directory to save the app in · php-starter
 Unpacked template
 Do you want to deploy the app now? · yes
Loading local package (manifest path: php-starter/.)
 Correctly built package locally
 Package correctly uploaded
 Succesfully pushed release to namespace wasmer-user on the registry
Deploying app php-starter (wasmer-user) to Wasmer Edge...
 
App php-starter (wasmer-user) was successfully deployed 🚀
https://php-starter-wasmer-user.wasmer.app
 
 Unique URL: https://2e5h6i7cd9ql.id.wasmer.app
 Dashboard:  https://wasmer.io/apps/wasmer-user/php-starter
 
Waiting for new deployment to become available...
(You can safely stop waiting now with CTRL-C)
.
𖥔 Deployment complete

Your directory should now look like this:

    • index.php
    • info.php
  • README.md
  • app.yaml
  • wasmer.toml
  • Let's check it:

    curl https://php-starter-wasmer-user.wasmer.app

    The deployment URL follows the format https://<app-name>-<app-owner>.wasmer.app

    Expect output similar to this:

    1       =>      PHP code tester Sandbox Online
    emoji   =>      😀 😃 😄 😁 😆
    2       =>      5
    5       =>      89009
    Random number   =>      630
    PHP Version     =>      8.3.4

    Update the app

    To illustrate the lifecycle of an app, let's edit the index.php file in the app folder:

    app/index.php
    <?php 
      echo 'Hello World!';   
      var_dump($_SERVER); 
    ?>

    Now, simply run wasmer deploy again:

    wasmer deploy

    Let's check it again:

    curl https://php-starter-wasmer-user.wasmer.app

    Expect to see:

    Hello World!array(19) {
      ...
    }

    Testing your PHP application locally

    To test your PHP application locally simply run

    wasmer run .
    ℹ️

    You can see all the available options with wasmer run --help or click here to see the full documentation.

    Let's try to cURL the server:

    curl http://127.0.0.1:8080

    Expect to see:

    Hello World!array(19) {
      ...
    }

    Custom PHP settings

    PHP in Wasmer Edge is using the PHP development CLI, so we can assure maximum compatibility. Because of that, we want to showcase how you can make your app ready for production workloads.

    scaling.mode in app.yaml

    If you check the app.yaml file, you will see the setting scaling.mode (opens in a new tab):

    scaling:
      mode: single_concurrency

    scaling.mode: single_concurrency specifies to Wasmer Edge that each command only has one worker per thread, so Wasmer Edge can scale them accordingly. See scaling.mode docs.

    php.ini settings.

    You can customize the PHP settings with a custom php.ini. Create a config/php.ini inside of your folder, and use the following settings:

    config/php.ini
    opcache.validate_timestamps = 0
    opcache.file_update_protection = 0
    opcache.max_file_size = 0
     
    upload_max_filesize = "20M"
    post_max_size = "25M"

    Now, copy the config directory inside of the filesystem (fs) in wasmer.toml, and set the environment variable PHPRC

    wasmer.toml
    [fs]
    # ...
    "/config" = "config"
     
    [[command]]
    name = "run"
    module = "php/php:php"
    runner = "wasi"
     
    [command.annotations.wasi]
    main-args = ["-t", "/app", "-S", "localhost:8080"]
    env = ["PHPRC=/config/"] # this indicates PHP where to look for php.ini

    The main-args current values are equivalent to calling php -t app -S localhost:8080 in your local console.

    Instaboot

    If you want to accelerate the cold-start times of your PHP app (for example, if you are using Symfony or Laravel with tons of PHP imports), Instaboot can help to speed up cold-starts by 100-200x.

    app.yaml
    capabilities:
      instaboot:
        # We provide a list of HTTP requests that will be used to pre-warm the
        # application.
        requests:
          # Load the homepage
          - path: /

    Learn more about Instaboot in our docs.

    Conclusion

    Congratulations! You have successfully deployed a PHP application on Wasmer Edge 🚀.

    Tip: To make changes to your PHP application, simply modify the index.php file in the app directory and run wasmer deploy again to deploy the changes.

    Resources

    wasmer-examples/php-wasmer-starter