Skip to content

Instantly share code, notes, and snippets.

@diyism
Last active November 18, 2022 01:38
Show Gist options
  • Save diyism/85d22357292559bb50b80df0f452f593 to your computer and use it in GitHub Desktop.
Save diyism/85d22357292559bb50b80df0f452f593 to your computer and use it in GitHub Desktop.
vercel php serverless

build your vercel php serverless website:

  1. create github private repo

  2. create vercel.json in the private repo:

    {
      "builds":[{"src": "/api/*.php", "use": "[email protected]"},
                {"src": "/api/*.go", "use": "@vercel/go"},
                {"src": "**/*.ico", "use": "@vercel/static"},
                {"src": "**/*.gif", "use": "@vercel/static"},
                {"src": "**/*.js", "use": "@vercel/static"},
                {"src": "**/*.png", "use": "@vercel/static"},
                {"src": "**/*.jpg", "use": "@vercel/static"}
               ],
      "routes":[{"src":"/(.*)", "dest": "/api/$1"}
               ]
    }
    
  3. create dir "api" in the private repo

  4. copy your whole php website codes into "api"

  5. login vercel.com, create project and import the github private repo, click "Configure Github App" and add this private repo in "Repository access (Only select repositories)"

  6. done, you can visit your website "https://[your repo name]-[your vercel user name].vercel.app" or bind your own domain name.

  7. by clicking the last commit hash(https://github.com/[your github user name]/[your repo name]/commit/[hash]) you can see the vercel build bot error(it's weired that it didn't list it in the deployments log page)

  8. you can see all deployments and click "redeploy" on https://vercel.com/[your vercel user name]/[your repo name]/deployments, the github deployments log is delayed on https://github.com/[your github user name]/[your repo name]/deployments

integrate with the free google firestore database:

  1. it seems that the free vercel php serverless webserver has no difference with the normal nginx php webserver

  2. except that there's no database with the vercel php serverless webserver

  3. we can use the "1GB per project"(20000 writes per day) firestore database of the $0 plan(spark plan) of google firebase, we can create 5 to 10 projects

  4. login https://console.firebase.google.com, create a project(eg: "[my username]-firestore-1"), create a database(test mode) and collection(data table)

  5. find the " Web API Key" in https://console.firebase.google.com/project/[your project name]/settings/general

  6. in vercel php, read line1 from firestore table1:

    $row1=@json_decode(@file_get_contents('https://firestore.googleapis.com/v1/projects/[your project name]/databases/(default)/documents/table1/1?key=[your web api key]'), 1);
    
  7. in vercel php, update line1 of firestore table1:

$context=stream_context_create(array('http'=>array('method'=>'PATCH',
                                                   'timeout'=>5,
                                                   'header'=>"Content-Type: application/json\r\n",
                                                   'content'=>json_encode(['fields'=>['visits'=>['integerValue'=>$visits+1]]])
                                                  ),
                                    )
                              );
@file_get_contents('https://firestore.googleapis.com/v1/projects/[your project name]/databases/(default)/documents/table1/1?updateMask.fieldPaths=visits&key=[your web api key]', false, $context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment