Please note: This is going to be another of those techie posts – the previous ones have been well received so since we have spent some time with this particular combo of hardware and software lately we thought we would share the info here.
Lately we have been trialling Amazon Web Services, and particularly their cloud server product called Amazon EC2. Now is a pretty good time to get your feet wet since they are offering a completely free tier of usage, allowing you to play around with cloud computing without any cost.
This guide will detail how to set up a web server with a LAMP (Linux, Apache, MySQL, PHP) stack on a free Amazon EC2 account, and as a bonus will also let you know how to connect to it with Coda 2 (our current favourite Mac coding environment) to start getting your web site online.
So without further ado here is the guide.
Starting up a server
- Go and sign up for Amazon Web Services. You will need to enter a credit card, but it won’t be charged as we will only be using a server that is on the free usage tier.
- Sign in to the AWS Management Console (It’s a link under the dropdown in the top right) with your user name and password.
- Click on EC2: Virtual Servers in the Cloud – the first option under Compute & Networking. Amazon offers a baffling amount of services, but for our purposes (and the purposes of most people) EC2 will be the only one you need.
- Click the button marked “Launch Instance”. Instances are basically servers, so what you are doing when you are launching an instance, is effectively starting up a server.
- Click “Classic Wizard” and continue, as its a bit more explanatory than the quick launch one, though that might be a better option when you get to grips with it.
- Select a 32 bit Amazon Linux AMI type server – it has the star next to it, so that means it’s eligible for the free tier if you select a Micro (lower powered) instance.
- Ensure number of instances is set to 1 and type is set to Micro (so it’s free) . The availability zone doesn’t really matter here so select no preference on that dropdown and hit continue.
- On the next page, all of the defaults are fine, so we just hit continue again.
- Let’s give this server a name for when we are administering it later – let’s go for Webserver. Enter Webserver in the value box next to name and hit continue.
- Now on to some security. In order to securely access the server from your local machine, you will need to create a key pair, so under Create a New Key Pair enter a name for it. It doesn’t matter what it’s called really, but for this tutorial let’s call it “myawskey” and click “create and download your key pair”. This will download a file called myawskey.pem to your computer. Put it somewhere you will be able to find it later on.
- You now need to add some security to your new server. These come in the form of security groups, so let’s select an existing group (AWS offers you some defaults) to make it easy. Select “quick-start-1″ and click Continue.
- Next you will get a screen showing everything you’ve just selected – as long as nothing looks particularly odd you can click “Launch” and your new server will be up and running within a minute or so!
Adding all the software you need to your new server to make it into a Webserver
So we now have a brand new server, but it needs some software installing so that it can serve web pages for us, so that’s the next step. Here we will be connecting to the server with something called SSH and installing the LAMP stack Apache, MySQL and PHP (we already have the L of the LAMP stack, in that the servers operating system is Linux). These instructions are Mac-centric, although the same commands could be used with a different terminal application on a PC.
- First of all, let’s get familiar with a few things in the AWS console. Your server (an instance) is listed under the instances menu item on the left hand side. Once you click this you will see it on the right. Click the tick next to it and you will see some information about it appear at the bottom. Scroll down to “public DNS” and copy it. This is the address you will be using throughout to connect to the server.
- Let’s take the key pair you created earlier and put it in the default directory for key pairs so that our Terminal app will apply it automatically when we try to connect to our server. Copy the myawskey.pem file to “/
/.ssh/”. For example, mine would be /anthony/.ssh/
- Start up the Terminal application on your Mac and type “ssh ec2-user@
“. For example it may look something like this: ssh email@example.com
- Now we are in we’ll need to run some linux commands to install the software, but first let’s give ourselves the appropriate permissions to do all the installs by typing “sudo su”.
- First off lets install any updates to the packages that are already on the server by typing “yum update”. Say yes to all the updates until it has finished running.
- Now let’s install apache with “yum install httpd”.
- Now let’s get PHP installed: “yum install php php-mysql”.
- And finally let’s install mySQL: “yum install mysql-server”.
- Now let’s start those services, beginning with the webserver: “service httpd start”.
- And ending with MySQL: “service mysqld start”.
- Now we need to do some work to add a user that can manage the database from your favourite MySQL program on your local machine, so let’s run the following to start the program: “mysql”
- Decide on a username and password for your admin account and enter this command (in the example we are using username “admin” and password “password” – please pick something a bit more inventive!):
GRANT ALL PRIVILEGES ON *.* TO admin@’%’ IDENTIFIED BY ‘password’ WITH GRANT OPTION;
- Hit enter to go onto a new line and type “\g” to execute the command, then \q to quit.
- While we are on the server, let’s do a couple more setup tweaks that you may need when you start to add your webpages to the server, enabling URL rewriting and PHP short tags.
- To enable URL rewriting type “vi /etc/httpd/conf/httpd.conf” to open the apache config file in the vi editor.
- Scroll down to where it says “AllowOveride None” inside the DocumentRoot Directory Directive, normally
- Press i to enter insert mode and change “AllowOveride None” to “AllowOveride All”. Press escape, then type :wq! to save and quit the editor.
- To enable PHP short codes, type vi /etc/php.ini to start the vi editor with the appropriate file.
- Scroll down to where it says short_open_tag = Off and change it to short_open_tag = On by pressing i to go into insert mode.
- Press escape, then type :wq! to quit the editor.
- Lastly, we will need to restart the webserver so those last two changes that effect. Enter “service httpd restart”.
- That’s it! We are all done on setting up the LAMP stack.
Using Amazon AWS Management Console to allow web and MySQL access to the server.
So we have everything set up, but our security settings mean we can’t actually access the server through our browser (to view the webpages) or through our SQL program to add any databases, so let’s rectify that.
- Go back to your browser window that has the AWS Management Console open and click “Security groups” under “Network and Security” on the right hand menu.
- Click the tick next to “quick-start-1″ to select the security group we applied to our webserver.
- Select the “Inbound” tab in the details pane at the bottom left. You will see that SSH access on port 22 is set up, which is what we’ve just used to access the server and install all of the software.
- Now lets add HTTP access so you can view web pages in a browser. Select “HTTP” under “Create a new rule”, Leave the source as it and click “Add Rule”.
- Save the rule by clicking “Apply Rule Changes”.
- Now lets add remote MySQL access. Select “MYSQL” under “Create a new rule”, Leave the source as it and click “Add Rule”.
- Save that rule by clicking “Apply Rule Changes”.
- All done! You should now be able to visit http://
and see an apache test page from your server. You should also be able to use your favourite SQL program to access with the admin username and password we set up on the server.
Last part: Uploading files to your webserver
When it comes to uploading files to your webserver, most would use FTP, however for added security and convenience (since it doesn’t exactly play well with FTP) Amazon AWS uses SFTP instead. You can check that SFTP is working with the command line, but we had some issues setting it up in Coda 2, so we’ll just cover that briefly here for anyone with the same problem.
- First let’s check SFTP is actually working. Go to your terminal and type “sftp ec2-user@
“. For example this may look like: sftp firstname.lastname@example.org.
- If it connects, then SFTP is working fine, you would set this up in Coda by going to site settings and doing the following steps.
- On “Site”, set protocol to “SFTP”
- Set Server to “
- Set User Name to “ec2-user”.
- No need to enter a password or select a key file in the next box, as we put the key file in the default directory earlier on and Coda is smart enough to look there.
- Coda should now be set up to upload files via SFTP.
As of the time of this writing Coda 2 had a bug which stopped it from connecting to this particular type of SFTP, which wasn’t a problem in earlier versions. Thankfully the Coda developers Panic, are aware of it, and it should be fixed in an official update soon, however in the meantime one of the developers offered an unofficial release on their bug tracking platform that should solve the problem. You can download the unofficial fix here.
That’s it! Enjoy your new free webserver from Amazon, and when you’ve tinkered around with it some more you will be able to make more of what cloud hosting has to offer!