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!

Initialize the local directory

With the CLI installed, let's create a new PHP application! We begin creating a new empty directory - after that, we will need to run a single command.

$ mkdir my-php-application && cd my-php-application
$ wasmer deploy --template=php-starter

The wasmer deploy --template=php-starter command will prompt you for the following:

  • Download the PHP template from the registry
  • Deploy it to Wasmer Edge with the user-provided informations
    • 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 to choose 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.

Let's check it:

$ curl https://<your-app>.wasmer.app
1	=>	PHP code tester Sandbox Online
emoji	=>	😀 😃 😄 😁 😆
2	=>	5
5	=>	89009
Random number	=>	<random!>
PHP Version	=>	8.3.4

Your directory should now look like this:

    • index.php
    • info.php
  • README.md
  • app.yaml
  • wasmer.toml
  • 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, mount the config directoy 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.

    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
    [...]
    𖥔 Deployment complete

    Let's check it again:

    $ curl https://<your-app>.wasmer.app
    Hello World! <more text..>

    Testing your PHP application locally

    To test your PHP application locally simply run

    $ wasmer run .
    Starting server on http://127.0.0.1:8000
    ℹ️

    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:8000
    1	=>	PHP code tester Sandbox Online
    emoji	=>	😀 😃 😄 😁 😆
    2	=>	5
    5	=>	89009
    Random number	=>	286
    PHP Version	=>	8.3.4
    Hello	=>	World

    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