Web Hosting using PHP and MySQL on AWS

Image for post
Image for post

It’s was a Sunday and I was bored. Then I came across a custom URL shortener service, YOURLS that I had previously used in my college days. It was very simple to set it up with my GoDaddy shared web hosting. I no longer have a web hosting package on GoDaddy so I decided to try out AWS cloud instead.

AWS is cheap and easy to get started. Our goal is to get our own custom URL shortener service up and running on http://aloudapp.in.

Before we deep dive into the details of setting up everything, here are our broader goals.

  • Setup an EC2 instance for cloud hosting
  • Setup a hosting zone and configure DNS
  • Setup a load balancer for the EC2 instance.
  • Install a LAMP web server on the EC2 instance.
  • Configure YOURLS on the web server.

This article will cover everything up to setting up the LAMP web server.

Create an EC2 instance

An EC2 instance is a virtual server in Amazon’s Elastic Compute Cloud (EC2) for running applications on the Amazon Web Services (AWS) infrastructure.

Click on Launch Instance to create a new instance.

Image for post
Image for post
Create an EC2 instance

Choose Amazon Linux from the listed options for machine images.

Image for post
Image for post
Choose Amazon Linux image

We will go for a t2.micro free instance for the purpose of this example.

Image for post
Image for post
Choose t2.micro free instance

All done! Review your settings.

Image for post
Image for post
Review your settings

Create a public-private key pair if you desire. This is an optional step but we recommend that you create a key pair and download it. We will be using it later on in the article to SSH into our EC2 instance.

Good Going! Your instance is up and running.

Image for post
Image for post

Edit security group settings to allow SSH inbound traffic from your IP address.

Image for post
Image for post
Edit Inbound Traffic for your instance

Add a custom rule

Image for post
Image for post
Whitelist your IP

Take a look at this post if you want to SSH into your EC2 instance programmatically.

Domain Mapping

Next, you need to create a Hosting Zone where you would configure your DNS settings for the domain name. Here are the steps.

Click on Create Hosting Zone to get started.

Image for post
Image for post
Create a hosted zone

Fill in your Domain Name to create a new hosted zone.

Image for post
Image for post
Put in your domain name

Once you have created a Hosted zone, you will be able to see associated namespace records for it. Edit these NS records for your domain. I purchased the domain from GoDaddy, so I had to go and edit these records in DNS settings for the domain on GoDaddy’s admin panel.

Image for post
Image for post
Edit namespace on GoDaddy

Coming back to your Hosted Zone, create a new A record. Leave the Name field empty if you don’t want to configure just for a particular subdomain.

Image for post
Image for post

That’s it. Your Hosted Zone is now set up. You can also set up a subdomain in 4 easy steps by following this step by step guide.

Create a Load Balancer

Next, you need to create a Load Balancer for your EC2 instance. As described by Wikipedia,

In computing, load balancing improves the distribution of workloads across multiple computing resources, such as computers, a computer cluster, network links, central processing units, or disk drives.

Choose a Classic Load balancer which is used for HTTP, HTTPS and TCP traffic.

Image for post
Image for post
Click on Create Load Balancer
Image for post
Image for post
Choose a Classic Load Balancer

Follow the wizard to complete the Load Balancer settings.

Define its name and protocol as depicted in the section below.

Image for post
Image for post
Load balancer settings

Assign security groups to the load balancer. Create a new security group for your load balancer that allows traffic from any IP address.

Image for post
Image for post
Create a new Security group for load balancer

Set Load balancer’s security group to the newly created one.

Image for post
Image for post

Setup health checks. Again you and go ahead with the default options as of now. We can revisit these settings later.

Image for post
Image for post
Default health check settings

Choose the EC2 instance that you created earlier for the load balancer.

Image for post
Image for post
Map with your EC2 instance

Tags are optional and help to identify your resources.

Image for post
Image for post
Optional tags

All done! Your load balancer is now set up.

Edit Inbound Rules for EC2 instance

Go back to your EC2 instance and add a new inbound rule for it. This will allow all traffic from the load balancer.

Image for post
Image for post
Edit security rules for EC2

Now your EC2 instance, hosting zone, and load balancer are all setup. We will go ahead and install a LAMP web server on our EC2 instance.

Connect to your Instance

  • Open an SSH client
  • Locate your private key file and provide permissions
chmod 400 amazon_ec2_key.pem
  • Connect to your EC2 instance
ssh -i "amazon_ec2_key.pem" ec2-user@ec2-13-127-42-0.ap-south-1.compute.amazonaws.com

Install a LAMP webserver

  • Check if software packages are up to date.
sudo yum update -y
  • Install the packages for php and mysql.
sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd
  • Start the http server
sudo service httpd start
  • Create a health.html file in cd/var/www/html . Put any content in the file and save it.
nano health.html
  • Edit health check settings for your load balancer.
Image for post
Image for post
Change health check settings

Now you should be able to see the test page when you hit the domain.

http://aloudapp.in

Image for post
Image for post

Run the following commands to set file permissions.

sudo usermod -a -G apache ec2-user
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
Image for post
Image for post
Run these commands to give permissions to your user

Create a PHP file in Apache document root.

echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

Open http://aloudapp.in/phpinfo.php to test it.

  • Start MySQL server.
sudo service mysqld start
  • Run mysql_secure_installation and set root password, remove anonymous user accounts, disable remote root login, remove the test database, reload the privilege tables.
sudo mysql_secure_installation

Install phpMyAdmin

Run the following commands to setup phpMyAdmin.

sudo yum install php70-mbstring.x86_64 php70-zip.x86_64 -ysudo service httpd restartcd /var/www/htmlwget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gztar -xvzf phpMyAdmin-latest-all-languages.tar.gzmv phpMyAdmin-4.7.6-all-languages phpMyAdminsudo service mysqld start

Special thanks to Chetan Gulati who helped me set it up.

All done! Now your web server is up and running. We will cover the configuration of YOURLS on this web server in the next story.

Image for post
Image for post

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store