How To Host A Dynamic PHP Website On Amazon EC2 And Static Website On S3

The main advantage of hosting your website on amazon EC2 is that you have full control over the server. You can do anything with the server unlike other hosting providers where the things you can do with the server are limited. You can have a high availability architecture for your application on aws. With the right combination of EC2 ,ELB and route53 you can have a highly scalable and available application on AWS.

You might like :Amazon Web Services – Web Hosting & Cloud Computing With AWS

You can host a php application on AWS using two methods,

1. Hosting it directly on an EC2 instance: In this scenario, you have to launch, install and configure the application environment.

2. Using elastic beanstalk: Elastic beanstalk is a an app deployment service, using which you can deploy a php application with few clicks. EC2 instances, load balancer, autoscaling etc,, everything will be setup automatically in the backend using elastic beanstalk. All you have to do is just upload your application using the management console. The instances created by elastic beanstalk is accessible by the user. Amazon wont charge you for elastic beanstalk service, but will charge only for the resources created in the backend.

Also read: How to host application on AWS elastic beanstalk

In this tutorial i will explain the first scenario where you have to setup the instance and webserver manually.

Tutorial for hosting a dynamic site using EC2

1. Launch an instance: View this tutorial to launch an instance How to launch an instance on Amazon EC2

2. Connect to your EC2 instance using putty: Follow this tutorial to How to connect an EC2 instance using putty  

Update the server using the following command

 sudo apt-get update

3. Install Lampserver : install web server using the following command

sudo apt-get install lamp-server^

The above command will install apache web server , php and Mysql. For a detailed tutorial for installing the lamp stack you can refer this tutorial How To Install Lamp Stack On Amazon EC2

 4. Now copy and paste the public DNS of your instance in the address bar of the browser and click enter. If it shows a page showing the message “It Works” , It means you have successfully installed the web server in your instance. 

5. Uploading Web Pages to WWW Folder: After the web server has been installed you have to upload your web pages to the www folder of apache web server. You can locate the folder using the following command

 cd /var/www 

You can upload the files to your instance using filzilla client. If you never used filezilla client you can refer the following tutorial How To Upload Files To AWS Server Instance Using FileZilla 

By default www folder contains index.html. Remove that default index.html and start uploading your HTML, CSS and PHP files to the server inside www folder.

Index page of your website should have the name index.html. After uploading the files, if you go to your public DNS you can see the index page of your site. You can map your custom domain name to the EC2 instance using the elastic IP.

Hosting A Static Website Using S3: In S3 we can host only static websites. If you want your dynamic website run on amazon then your should host it on Amazon EC2 instance.

Hosting a static website in S3 is a simple process, just design your pages locally in a computer and just upload it to S3 buckets. 

1. Login to Amazon S3 dashboard and create a bucket and give it your desired website name. eg: comtechies.com and also select the region you want to host your website. 

s3bucket name

2. Now select the bucket you created and click upload button at the left top corner. A window will pop up asking you to add files. Click add files and select the webpages of the website you designed and then  click start upload. 

S3 file upload

3. Once the files are uploaded ,right click on index page and click “make it public” option. Now go to properties, there you can see the S3 provided domain name of your website.

eg: https://s3.amazonaws.com/comtechies.com/index.html. [amazon bucket address]

s3 public index

 4. The amazon bucket address bit lengthy and if you would prefer to use a custom domain for your S3 website , Follow this tutorial How To Set Up A Custom Domain For Your S3 Website.

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like