Miss any of our Open RFC calls?Watch the recordings here! »

vercel-php

0.3.1 • Public • Published

PHP Runtime for Vercel

Enjoyable & powerful 🐘 PHP Runtime (php.vercel.app) for Vercel platform.

🏋️‍♀️ It works with these frameworks and tools. Discover more at examples.

Made with ❤️ by @f3l1x (f3l1x.io) • 🐦 @xf3l1x


😎 Getting Started

Let's picture you want to deploy your awesome microproject written in PHP and you don't know where. You have found Vercel it's awesome, but for static sites. Not anymore! I would like to introduce you your new best friend vercel-php, PHP runtime for Vercel platform.

Most simple example project is this one, using following project structure.

project
├── api
│   └── index.php
└── now.json

First file api/index.php is entrypoint of our application. It should be placed in api folder, it's very standard location for Vercel.

<?php
phpinfo();

Second file now.json is pure gold here. Setup your project with configuration like this and voila. That's all.

{
  "functions": {
    "api/*.php": {
      "runtime": "vercel-php@0.3.1"
    }
  }
}

Last thing you have to do is call now. If you are more interested take a look at features and usage.

# Install it globally
npm i -g now

# Log in
now login

# Let's fly
now

Are you ready to deploy your first PHP project to Vercel? Click & Go!

🤗 Features

  • Architecture: PHP development server (🚀 fast enough)
  • PHP version: 7.4.7
  • Extensions: apcu, bcmath, brotli, bz2, calendar, Core, ctype, curl, date, dom, ds, exif, fileinfo, filter, ftp, gettext, hash, iconv, igbinary, imap, intl, json, libxml, lua, mbstring, msgpack, mysqli, mysqlnd, openssl, pcntl, pcre, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, phalcon, Phar, protobuf, psr, readline, redis, Reflection, runkit7, session, SimpleXML, soap, sockets, sodium, SPL, sqlite3, standard, swoole, timecop, tokenizer, uuid, xml, xmlreader, xmlrpc, xmlwriter, xsl, Zend OPcache, zlib
  • Speed: cold ~250ms / warm ~5ms
  • Memory: ~90mb
  • Frameworks: Nette, Symfony, Lumen, Slim, Phalcon

List of all installable extensions is on this page https://blog.remirepo.net/pages/PECL-extensions-RPM-status.

⚙️ Usage

Before you can start using this runtime, you should learn about Vercel and how runtimes works. Take a look at blogpost about Serverless Functions.

You should define functions property in now.json and list PHP files directly or using wildcard (*). If you need to route everything to index, use routes property.

{
  "functions": {
    "api/*.php": {
      "runtime": "vercel-php@0.3.1"
    }
  },
  "routes": [
    { "src": "/(.*)",  "dest": "/api/index.php" }
  ]
}

Do you have more questions (❓)? Let's move to FAQ.

👨‍💻 now dev

For running now dev properly, you need to have PHP installed on your computer, learn more. But it's PHP and as you know PHP has built-in development server. It works out of box.

php -S localhost:8000 api/index.php

👀 Demo

PHP

🎯Examples

Browse more examples. 👀

📜 Resources

🚧 Roadmap

Next

  • PHP 8.0

Done

  • next-gen PHP runtime
  • Composer
    • config.composer: true
    • composer.json detection
  • zero config
  • customize php.ini
  • now dev
  • typescript codebase
  • github workflows (CI)
  • PHP 7.4
  • composer.json scripts

Help wanted

  • create examples using vercel-php

⁉️ FAQ

1. How to use more then one endpoint (index.php)?
project
├── api
│   ├── index.php
│   ├── users.php
│   └── books.php
└── now.json
{
  "functions": {
    "api/*.php": {
      "runtime": "vercel-php@0.3.1"
    },

    // Can be list also directly

    "api/index.php": {
      "runtime": "vercel-php@0.3.1"
    },
    "api/users.php": {
      "runtime": "vercel-php@0.3.1"
    },
    "api/books.php": {
      "runtime": "vercel-php@0.3.1"
    }
  }
}
2. How to route everything to index?
{
  "functions": {
    "api/index.php": {
      "runtime": "vercel-php@0.3.1"
    }
  },
  "routes": [
    { "src": "/(.*)",  "dest": "/api/index.php" }
  ]
}
3. How to update memory limit?

Additional function properties are memory, maxDuration. Learn more about functions.

{
  "functions": {
    "api/*.php": {
      "runtime": "vercel-php@0.3.1",
      "memory": 3008,
      "maxDuration": 60
    }
  }
}
4. How to use it with Composer?

Yes, Composer is fully supported.

project
├── api
│   └── index.php
├── composer.json
└── now.json
{
  "functions": {
    "api/*.php": {
      "runtime": "vercel-php@0.3.1"
    }
  }
}
{
  "require": {
    "php": "^7.4",
    "tracy/tracy": "^2.0"
  }
}

It's also good thing to create .nowignore file and put /vendor folder to this file. It will not upload /vendor folder to Vercel platform.

5. How to override php.ini / php configuration ?

Yes, you can override php configuration. Take a look at default configuration at first. Create a new file api/php.ini and place there your configuration. Don't worry, this particulary file will be removed during building phase on Vercel.

project
├── api
│   ├── index.php
│   └── php.ini
└── now.json
{
  "functions": {
    "api/*.php": {
      "runtime": "vercel-php@0.3.1"
    }
  }
}
# Disable some functions
disable_functions = "exec, system"
 
# Update memory limit
memory_limit=1024M
6. How to exclude some files or folders ?

Runtimes support excluding some files or folders, take a look at doc.

{
  "functions": {
  "api/**/*.php": {
    "runtime": "vercel-php@0.3.1",
    "excludeFiles": ["node_modules", "somedir", "foo/bar"],
  }
}
7. How to call composer script(s) ?

Calling composer scripts during build phase on Vercel is supported via script named vercel. You can easilly call php, npm or node.

{
  "require": { ... },
  "require-dev": { ... },
  "scripts": {
    "vercel": [
      "@php -v",
      "npm -v"
    ]
  }
}

👨🏻‍💻CHANGELOG

Show me CHANGELOG

🧙Contribution

  1. Clone this repository.
    • git clone git@github.com:juicyfx/vercel-php.git
  2. Install NPM dependencies
    • make install
  3. Make your changes
  4. Run TypeScript compiler
    • make build
  5. Run tests
    • make test
  6. Create a PR

📝 License

Copyright © 2019 f3l1x. This project is MIT licensed.

Install

npm i vercel-php

DownloadsWeekly Downloads

592

Version

0.3.1

License

MIT

Unpacked Size

44.3 kB

Total Files

17

Last publish

Collaborators

  • avatar