LambdaPHP v0.02
Host your website on Aws Lambda with full PHP 7 support (i.e. pay by requests instead of paying a fixed monthly hosting fee)., (*1)
Now it's possible for you to host dynamic PHP files, static HTML files, css files on AWS Lambda (serverless) just like an Apache server running mod_php. Any files you put inside the public
directory will be accessible as if they were hosted on an Apache server with
mod_php
. There are no handlers to write or config files to maintain., (*2)
For example, put two files, index.php and deep/other.php inside your public
folder. The type lambdaphp deploy -v
.
Once deployed you should be able to access them online at https://yourdomain.com/index.php, https://yourdomain.com/deep/other.php, etc (details below)., (*3)
But why should I care?
The Difference is you don't have to pay any monthly hosting fees because they're running on AWS Lambda which
means you are billed only by the number of requests. This includes 1 million free requests per month and 400,000 GB-seconds
of compute time per month (details here)., (*4)
Other advantages of going serverless include
-
Super fast deployments:
With just a simple command your updates are live instantly., (*5)
-
Unlimited scalability:
Aws Lambda can scale from 0 to virtually infinite instances without any extra code or setup. So whether your site gets one hit or a million hits a day it can handle all scenarios easily., (*6)
-
Ultra Low cost:
Are you hosting your site on a $500 server yet not fully utilizing all server resources? AWS lambda is billed by number of requests not by monthly usage, so for a site that gets less than a million requests per month it is virtually free forever!, (*7)
-
Focus on your ideas not servers:
Are you spending a lot of time configuring load balancers, docker containers, etc to handle loads? Now you don't have to since it's all done for you., (*8)
-
Improved latency and geolocation:
Why make a user in India connect to a server thousands of miles away in America? Even if your site is super fast, the download will still be slow because of network latency. Aws Lambda Edge makes it possible for users to connect to the nearest server making the downloads super fast., (*9)
-
No server vulnerabilities:
Since the lambda instances (or web servers) are created on demand and destroyed after each request, it makes it virtually impossible to hack your web server or install any malware on it., (*10)
-
No server admin:
Same as above. No need to patch or upgrade packages as they are managed by the vendor. Plus the server is read-only an created and destroyed after each request, making it virtually impossible to exploit or install malware on it., (*11)
Installation
Installation is simple. All you need is PHP 7+ with composer
, (*12)
To install, just type this on your command line (terminal), (*13)
composer create-project lambdaphp/lambdaphp <project-name>
This should create a project-name directory inside which there is a public
directory. Any files,
including any PHP files you put in the public
directory can be accessed directly from your web browser., (*14)
Once you're done putting files in the public
folder, just type this on your command line to
deploy your site on AWS Lambda:, (*15)
lambdaphp deploy -v
You may need to enter your AWS credentials as described here (same as aws-cli)., (*16)
If everything goes as expected, you should see this message:, (*17)
Website deployed!
To access your site visit:
https://XXXX.execute-api.us-east-1.amazonaws.com/web/<any-file-in-public-folder><.php|css|js|etc>
(If you get a "command not found" error, make sure you have ./vendor/bin
in your PATH), (*18)
That's it! LambdaPHP will give you the URL using which you can access your site just like any other
site hosted on Apache. It is possible to use your own custom domains with https too [details here](http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html)., (*19)
Features
Using LambdaPHP you can now use AWS Lambda to:, (*20)
- Instantly host your static (.css, .js, .png, etc) and dynamic files (.php)
- Most PHP functionality, GET, POST, SESSIONS, etc work seamlessly.
- File operations incl
file_get_contents
, file_put_contents
, etc works seamlessly with AWS S3 (using S3 stream wrapping)
-
Sessions
and works right out of the box! (DynamoDB session wrapper under the hood)
- User authentication support using AWS Cognito (demo included)
- Cron support
- Supports Lambda Layers (for PHP executable and
vendor
dir both!)
Examples
You can also use your own domain to host these demos, (*21)
-
Static site, (*22)
Demo | Source code, (*23)
-
Handling POST data, (*24)
Demo | Source code, (*25)
-
Creating sessions using AWS DynamoDB, (*26)
Demo | Source code, (*27)
-
File IO on AWS lambda (using S3 wrapper), (*28)
Demo | Source code, (*29)
-
Simple File uploader to AWS S3, (*30)
Demo | Source code, (*31)
-
User signup and login using AWS Cognito, (*32)
Demo | Source code, (*33)
-
Using Custom domains with https support, (*34)
How to guide, (*35)
New layer support
In previous version of lambdaphp you had to upload the PHP executable and a bunch of files (min 32MB) every time you made even a small change to any php file.
Starting with this version lambdaphp now uses AWS Lamba Layers. It even creates a seperate layer for the vendor
dir (inside public directory) to ensure that minimum
amount of update is needed to deploy., (*36)
By default it uses the PHP layer which I've compiled with PHP 7.3 (with most mods including ImageMagick) but if you want to use a custom LayerARN, just modify your lambdaphp.ini
,
create a [php]
section and set the layerArn
value to your own Layer's ARN. Read the FAQ for more info., (*37)
So anyway, what this means to you is that previously it meant even a small update took 40MB and 5 minutes to deploy, the same can now be done in 1kb under 1 sec!
How's that for speed improvements? :), (*38)
Limitations
FAQ
-
I'm getting an error during deployment?, (*39)
This is still in beta so bear with me.
But here are some remedies:, (*40)
- Try to deploy again. AWS has a very strange bug where it takes a few seconds for some commands to work (sometimes). Basically what I mean to say is have you tried turning it off and on again?
- Make sure the API keys you are using have access to following:
AdministratorAccess
in your IAM (if you're in a hurry). Though it's better to enable the following access instead (requires full access): S3
(for files), DynamoDb
(for sessions), Lambda
(wonder why?), Api Gateway
(for invoking lambda via http).
- Try to clear composer's cache and re-installing it (composer sometimes picks old versions)
-
Which PHP version will my site be running?, (*41)
Currently it supports PHP 7.3 with most mods including PDO, GD and ImageMagick., (*42)
-
Is it really free to host my site this way?, (*43)
Yes, you get 1 million free requests per month and 400,000 GB-seconds of compute time per month.
Currently lambdaphp runs very fast with 128MB RAM and has an average response time is 20 to 50ms. So using the pricing calculator here,
you can process about 1,000,000 (1M) requests per month free of cost. YMMV. Also remember every asset on your page creates a new request, so factor that in and bundle them together using Webpack., (*44)
-
How do I update my site?, (*45)
Just run lambdaphp deploy
again!, (*46)
-
My site is loading slowly?, (*47)
The average load time is 25ms (via pingdom).
Use Webpack to reduce the number of requests per page (bundle CSS, JS, Font files, etc into a single js file). This will also make sure to reduce the number of requests per load helping you stretch your 1M requests even more., (*48)
-
How much traffic can this handle?, (*49)
Theoretically speaking AWS Lambda scales very well so it should be able to handle a lot of traffic easily. It performs better with more traffic since that reduces the load time. But for anything above 1M requests / day, you may consider a dedicated instance instead., (*50)
-
How do I add X
PHP extension?, (*51)
I've currently compiled php using the most popular PHP extensions. If you need to a custom extension, just using the
Dockerfile in Robert's project below., (*52)
-
What's the point of it anyway?, (*53)
It's a quick and dirty way to get simple PHP website online without paying any monthly hosting fees. Also a great resource if you wish to
learn Amazon's amazing services like API Gateway, AWS Lambda, S3, DynamoDB, Cognito (which work together under the hood of lambdaphp) for your own sites., (*54)
-
How to add cron jobs?, (*55)
Open (or create) file lambdaphp.ini
(it should be inside your project's root folder) and create a section called crontab
. To add cron jobs just use the following format, (*56)
job_name = 'cron(rate_expression)',php_file.php,enabled|disabled
So let's say you want to ping your site every 5 minutes. Here is how your lambdaphp.ini
should look, (*57)
[crontab]
ping = 'cron(*/5 * * * *)',ping.php,enabled
After you run lambdaphp deploy
, this will create a cron job to run ping.php
(relative to public
directory) every 5 minutes!, (*58)
To remove this cron job, just remove it from your lambdaphp.ini
and run lambdaphp deploy
again (alternatively you can mark last column as disabled
to temporarily suspend a cron job without removing it), (*59)
The timing of your cron job is controlled by the rate expressions as described in the link., (*60)
-
Can it run Wordpress / Laravel / etc?, (*61)
It could theoretically at least with some tweaks but I haven't had the time to test it. Maybe if you do, do let me know too!, (*62)
Need more features?
This was just a weekend project for my own amusement but I will definitely add more features, (*63)
- Please star this project to show your interest.
- Leave me some feedback on this Hackernews thread(please use Issues for bugs only).
- Contribute and send me PRs (I will add your name to credits/thanks).
Credits
Sanchit Bhatnagar - Co-creator, (*64)
Navneet Rai - Co-creator, (*65)
Thanks
License
The software is licensed using a GPL License. It means you can do whatever you want with it (including using it for commercial purposes freely), as long as you include the original copyright and license notice in any copy of the software/source., (*66)