Is it possible to use AWS CloudFront for PHP files? - php

I'm trying to load PHP files such as index.php using AWS CloudFront.
The documentation states -
Create a web distribution if you want to:
Speed up distribution of static and dynamic content, for example,
.html, .css, .php, and graphics files. Distribute media files using
HTTP or HTTPS. Add, update, or delete objects, and submit data from
web forms. Use live streaming to stream an event in real time.
However, when I upload PHP files to the relative CloudFront bucket it ends up downloading the file and opening it. How will allow me to host PHP files?

However, when I upload PHP files to the relative CloudFront bucket
There is no such thing as a CloudFront bucket, so you are likely referring to an S3 bucket, configured behind CloudFront as an origin.
CloudFront works with dynamic content, such as might be generated with PHP, but the PHP site needs to be hosted on a server that supports it -- not S3.
You can host a static website on Amazon Simple Storage Service (Amazon S3). On a static website, individual webpages include static content. They might also contain client-side scripts. By contrast, a dynamic website relies on server-side processing, including server-side scripts such as PHP, JSP, or ASP.NET. Amazon S3 does not support server-side scripting. (emphasis added)
https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html
See AWS Web Site Solutions for options for hosting sites involving static or dynamic content, bearing in mind that PHP requires a solution supporting server-side scripting and dynamic content, so not all solutions presented there (including S3) will fit your needs... but these are all compatible with CloudFront -- which is only tasked with delivering the rendered content, not the original rendering.

CloudFront is designed to serve content to end users and not execute your code. Your PHP files would be on an EC2 instance running PHP and a webserver (Apache, Nginx) which you could then put behind CloudFront to get the benefits. This would then generate the HTML for CloudFront to serve. CloudFront itself does not handle the processing and just deals with the static HTML. When using CloudFront with S3 it will serve up the content directly to the end user.
I am not quite sure where you found that snippet but the introduction does not seem to list .php for me.
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html
Amazon CloudFront is a web service that speeds up distribution of your
static and dynamic web content, such as .html, .css, .js, and image
files, to your users.

Related

If I host my web app in AWS Do I have to use S3

I have a web application that store images and Videos.im using PHP.is it make sense if I use S3 instead of store in file server since both are in AWS ( web server and file storage)
Yes. I would strongly recommend. S3 is an object storage service provided by AWS. It is a good place to store static files like images/videos/css/javascript which you can serve directly without putting more load on your servers. There are many benefits if you use S3,
99.999999999% durability and 99.99% availability of your files
Scalability on demand
Provide file caching support with the integration of CloudFront - Load Files much faster
Granular access management of the files
Private content distribution with CloudFront Signed URls
Stream media with CloudFront to viewers across the globe
Ability to protect data with encryption easily (eg: SSE-S3, SSE-KMS)
Lifecycle management of your files eg: archiving at low cost
Easy set up of disaster recovery with Cross Region Replication of the files
You may use AWS SDK for PHP to interact with S3 from your server.
First of all its not a must to host files in S3 if you use AWS but strongly recommended for Static Files that doesn't change frequently like Videos, Images, JS, CSS & etc.
If you use S3 for file storage, you can leverage all the benefits of reliability and performance efficiency which comes default by S3.
Also its recommended to use Event Driven Patterns for file uploads to S3 and storing metadata of files, which will increase the scalability of your application. Following example illustrates a typical flow for file uploads and storing file metadata in DynamoDB with S3 event triggers.
For frequent modification of files or partial updates you can use different storage options like Elastic File System or EBS.
For both approaches you can gain higher read speeds by caching the content through AWS CloudFront CDN.

AWS Cloudfront - and filenames upon deploying

I was wondering if it's possible to manipulate or change the names of files upon deploying the distribution.
Reason for during this, is that we don't have the actual files on our own servers but is provided by a partner. Is it somehow possible to run a php function upon deploy to change the name of the file on the cdn ?
So eg.
partner.example.com/image/123120913.jpg
to
1234.cloudfront.com/image/SHOE-NAME.jpg
One way is to import all images first to local storage and upon that download change filename - but seems very extensive.
As we can provide the image name easy if it's possible to run a php function upon deploying.
Amazon CloudFront is a caching service that retrieves content from a specified origin (eg web server, Amazon S3), stores it in a cache and then serves it to users.
Amazon CloudFront does not create aliases to filenames. It simply passes the request to the origin. If the origin is a web server, you could write a web app that returns any type of information given the request URL, but CloudFront cannot rename or map filenames.

Amazon S3 and running PHP using an external server

I know you can't run PHP on Amazon S3, but is there a way to host a static site on S3 and then have a contact form that runs using PHP from another server? In essence, I'm asking if the PHP file needs to be on S3 for it to run properly?
No, the PHP file does not have to be on S3. Just put it on the other server and refer to the absolute (full) URL.
Pages can submit forms to other domains (e.g. to the PHP server). This is actually what makes CSRF possible even when a server requires POST requests.
Pages can load images, scripts, and stylesheets from other domains. In fact, it is very common for these assets to be loaded from separate servers or even third-party content delivery networks. For example, Stack Overflow loads its copy of jQuery from //ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js.

Amazon S3 for file Uploads and caching

I am currently writing an application using the Yii-framework in PHP that stores a large number of files uploaded by the users of the application. I decided that since the number of files are going to be ever increasing, it would be beneficial to use Amazon S3 to store these files and when requested, the server could retrieve the files and send it to the user. (The server is an EC2 instance in the same zone)
Since the files are all confidential, the server has to verify the identity of the user and their credentials before allowing the user to receive the file. Is there a way to send the file to the user in this case directly from S3 or do I have to pull the data to the server first and then serve it to the user.
If So, Is there any way to cache the recently uploaded files on the local server so that it does not have to go to s3 to look for the file. In most cases, the most recently uploaded files will be requested repeatedly by multiple clients.
Any help would be greatly appreciated!
Authenticated clients can download files directly from S3 by signing the appropriate URLs on the server prior to displaying the page/urls to the client.
For more information, see: http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
Note that for confidential files you may also want to consider server-side/client side encryption. Finally, for static files ( such as images ) you may want to set the appropriate cache headers as well.
Use AWS Cloud Front to server these static files. Rather than sending the files to the user, send them links to the files. The Links need to be cloud front links & not direct links to the S3 bucket.
This has the benefit of keeping load low on your server as well as caching files close to your users for better performance.
More details here Serving Private Content through CloudFront

How to reliably upload large files through a web interface

Background:
I have a site that deals with large video files (sports videos). The site allows users to upload any source file, but most of the videos come from DVDs. I am currently using a modified version of jumploader to encode and upload files to the webserver with an HTTP request. The jumploader is a java client, and looking at my webstats, only about 75% of my users have java installed.
What I want:
I have looked for a flash based uploader that transfers a byte stream to a server. Byte streams would be ideal so I can capture partial videos for failed uploads. I have also looked for flash-based ftp clients, but I haven't found anything promising. There are some flash-based uploaders that post via HTTP, and I'm considering this. I do not want to use a simple file post. Some upload methods also put the full file into memory first, and this obviously is not an option for me.
Does anyone have experience uploading large (up to 2-3GB) files to a web (or ftp) server from a web frontend (PHP) with any reliability?
as a flash based solution you can use swfupload, which is widely used because you can display a progress bar. It sends answers back to you which you can read with JavaScript.
We decided upon using Transloadit, a service which is specialised in uploading and encoding video files. You can even track whether a download was aborted by closing the browser window, it has realtime encoding and supports multiple formats. Moreover your files are stored at Amazon S3 so you can easily stream them with AWS CloudFront and JWPlayer or Flowplayer.
See also zencoder.
Cheers

Categories