Setting up an AWS webserver
Recently I decided to change webhosts from a shared webhosting provided to a VPS operated by Amazon Web Hosting.
*Getting the domain and DNS ready
In preparation for this move I decided to move my domain name from Godaddy to Google domains. I have been unhappy with Godaddy’s raising the prices every year and with Google Domains offering free domain privacy I decided to make the switch. After transferring the domain, which only took a few hours, I made sure that the cloudflare name servers that I was assigned were set up with Google correctly.
The next step is setting up our DNS. While most domain registers offer DNS service, I use Cloudflare instead.
With this move I desired to change up how I configure my DNS settings, taking advantage of Cloudflare’s support for CNAME Flattening.
I set up a subdomain on my domain for the server. This A record points to the server IP address. All other DNS records are CNAMEs pointing to the server’s subdomain. This way if I ever need to update IP addresses, or even add servers it should be easy.
As I was moving number of sites over, I left their old entries as they were until the sites were moved to the new server and then the old records were removed and the new CNAMEs were set up.
*Setting up an AWS free tier account
Setting up the free Tier account was one of the easest parts of the move. I went to the AWS Website and set up an account. Took only a few minutes.
I then set up a EC2 VPS server following these directions:
A few Things I kept in mind were that while there are hard drive images available with the webserver software setup, I wanted to do it myself, so I chose to use a generic Linux image.
Once I had the VPS server up and running I set up an elastic IP address and associated it with the VPS. I then used this IP to set up the A record in DNS.
I then made sure that my SSH and FTP software ere setup to connect and were able to connect successfully. For the rest of this we will mainly be using SSH to work from the Linux command line.
**Setting up Apache and MySQL.
First we need to install the software:
sudo yum update -y
sudo yum install -y httpd24 php56 mysql55-server php56-mysqlnd
Lets start up apache so we can make sre it’s runing:
sudo service httpd start
Lets make sure that Apache will start on boot:
sudo chkconfig httpd on
At this point if we goto our website we should see a page that informs us the server is up and running.
Next we set up the MySQL database server:
sudo service mysqld start
Enter the current
rootpassword. By default, the
rootaccount does not have a password set, so press Enter.
Type Y to set a password, and enter a secure password twice. For more information about creating a secure password, see this. Make sure to store this password in a safe place.
Type Y to remove the anonymous user accounts.
Type Y to disable remote
Type Y to remove the test database.
Type Y to reload the privilege tables and save your changes.
Now we want to make sre that MySQL will also start on boot:
sudo chkconfig mysqld on
Now lets install phpMyAdmin
sudo yum-config-manager --enable
sudo yum install -y phpMyAdmin
phpMyAdmininstallation to allow access from your local machine. By default,
phpMyAdminonly allows access from the server that it is running on, which is not very useful because Amazon Linux does not include a web browser.
Find your local IP address by visiting a service such as whatismyip.com.
/etc/httpd/conf.d/phpMyAdmin.conffile and replace the server IP address (127.0.0.1) with your local IP address with the following command, replacing
your_ip_addresswith the local IP address that you identified in the previous step.
sudo sed -i -e 's/127.0.0.1/
Restart Apache to update the configuration
sudo service httpd restart
At this pont you should be able to goto
my.public.dns.amazonaws.com with either your amazon elastic IP or your domain name). You can login with the username root and the password you set earler.
phpMyAdmin is pretty straight forward, and well documented online. I am not going to go into how to use it. At this point I set up 2 databases, one for my main websites and one to support some archived sites I keep around.
*upload your website
At this point you can login with FTP and upload your website files to /var/www/html/ and call it a day, however as I was transferring over a number of websites (or if you plan to host multiple sites / subdomains) I needed to take a few more steps.
First inside of /var/www/html/ I created a number of subfolders, one for each site I was brining over.
I then created a file at /etx/httpd/conf.d/vhosts.conf.
The term Virtual Host refers to the practice of running more than one web site (such as
company2.example.com) on a single machine. Virtual hosts can be “IP-based”, meaning that you have a different IP address for every web site, or “name-based”, meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user. for most user setting upa name based vhosts is going to be the easiest.
I opened the file I just made and added the following:
I needed to add several of these blocks, one for each domain / subdomain. After editing the vhosts file, restart Apache
sudo service httpd restart
At this point I started uploading the files for my website and importing the dumps fro my old database.