I am experimenting with using AWS. To be specific, I am following the tutorial at this link:
I have tried to add empty PHP tags to the start of the file and renamed the file to index.php instead of the index.HTML of the tutorial. I did a full sequence of refreshing the web app resources and deploying the app on the Amplify console. It did not work. I tried only using the HTML code on index.php and it still did not work. I put back the PHP code, added an echo statement echo "<h1>PHP Code Ran</h1>";
but renamed the file to index.html and it did render. Granted, there was an error in the text output. It also wrote the ending semi-colon and ending quotation, but it worked.
Is there any way for me to use a file named index.php as the home page of a web app using AWS amplify?
Advertisement
Answer
A PHP file isn’t just an HTML file with a different name: you need to have a server somewhere that’s running PHP which will look at the PHP code and run it.
If you’re just uploading files to S3, that’s not going to happen, the file is just going to be sent straight to the browser, regardless of what you call it and what you put in it.
Putting <?php echo "<h1>PHP Code Ran</h1>"; ?>
into a file “worked” only in the sense that when you opened the page in the browser, you saw your browser’s best attempt to interpret that as HTML. If you go to “View Source”, you’ll see that the file is exactly what you uploaded to S3, no PHP has run at all.
If you want to write a PHP application, you need to understand how to run PHP – most likely on an EC2 server, but it could also be in a Fargate container, or something even fancier like bref which lets you run PHP in a Lambda function.