How to Install a Web Server on Amazon EC2

This is a step by step tutorial on how to install a web server on an Amazon Linux AMI in EC2. I, Ian L. of Jafty.com, have personally used this method to set up well over a dozen web servers for myself and my clients. I have perfected this method and edited the directions according to what I have found works the best. Take note where I noted that you do not need to make database tables if you don’t want to and you plan to install PHPmyAdmin as instructed towards the end of the tutorial. Have fun!

Starting a Free Amazon Web Server:

Sign up for an Amazon Web Services account:

The first step to migrating your website to the cloud is to sign up for AWS using the link below.

Go to http://aws.amazon.com/ and sign up.

Next, you need to set up your account before you can launch a server into the cloud. Start by going to https://console.aws.amazon.com/console/home and following these few steps to set up your account for the cloud:

  1. Click the “EC2” tab.
  2. On the left column of the page, under the “Network & Security” heading, click on “Security Groups”.
  3. In the right column click on the “Create Security Group” button. Enter a name and description in the pop up window then hit the “yes create” button.
  4. Just under the “Create Security Group” button is a list of existing groups. Your new group should be here. Make sure it is the only one checked.
  5. Further down in the right column click on the “inbound”  tab and set up your server’s ports. Here is another tutorial I wrote on how to set this up that you should take a look at because it is a little complex and very important to get correct the first time: http://jafty.com/blog/amazon-cloud-server-security-rules/(link opens in new window or tab). This has changed a little and now you don’t have to do this here if you don’t want to…or if you do it wrong, you can have another chance when you are setting up your instance. If you do it when you set up your instance, refer to the same link just mentioned as well and you’ll be okay.

 

  1. Make sure you click on the button that reads “Apply Rule Changes” once you have the ports set up like we do in the above image.
  2. Now go back to the left navigation column of the page and click on the “key pairs” link.
  3. Click “Create Key” in the right column.
  4. Give the key pair a name that relates to your server like “iansserver” and save it to your local machine. Save it to the desktop or somewhere you can find it easily because you will need it soon.

Creating a server instance in the cloud

Now comes the fun part! We are going to create a virtual private server or VPS in the cloud on amazons network of servers for little or no cost at all! Currently as of March 2012, Amazon allows new users to receive a year of free services. Be careful though, there are limitations, so monitor your account to make sure you only use services on the free tier and that you don’t exceed any set limitations. Read online support for more information on charges that could apply. Follow the following steps to set up a free micro instance of a Linux web server complete with web server, PHP, database server and PHPMyAdmin. Those are the minimal things you should definitely have to create dynamic web sites.

  1. Picking up on the same page we left off on above, click on “instances” in the left navigation column of the amazon webpage.
  2. Click “Launch Instance” to make the instance wizard appear. Make sure “Classic Wizard” is selected in the pop up and click on “Continue”.
  3. On the “Choose an AMI” pop up, select the AMI that reads “Basic 64-bit Amazon Linux AMI ” it will have a notice by it indicating it is in the free usage tier. Check the box to select this AMI and click “Next…”.
  4. In the “instance details” and the step after it, just accept the defaults by clicking on “Next…” two times.
  5. You should now be at the point where there is a key and value column. Leave “name” in the key column and enter a name that describes your server in the value column like “64bit_linux”. Click Continue.
  6. Now you should be on the “create key pair” pop up. Select the key pair name you saved earlier in step 9 above and click continue.
  7. Now in the “configure firewall” pop up, select the preexisting security group you named earlier and click continue.
  8. Review your settings and click “Launch”. Click on “View your instances on the Instances page” and watch your new cloud server appear under “My Instances”.
  9. Check the instance name and make sure no others are checked.
  10. Look down below and find where it gives details for the checked instance. Start a notes page in notebook to record important info and copy and paste the public DNS address for safe keeping you will need it soon.
  11. Open PuttyGen and click on file/load private key and load in the file you saved to your desktop earlier that has a .pem extension. When it’s loaded, click save private key to save the .pem file as a .ppk file
  12. Open Putty and enter the public DNS where it says “Host name”.
  13. Under “Category” in the left column of the Putty window, click to expand “SSH” then click on “Auth” then click browse and select the .ppk file you saved earlier. Click open.
  14. Now a terminal window should appear asking for a username. First a pop up may appear saying the server’s host key…. Just click yes then Enter “ec2-user” for username in the terminal window and hit enter. If it says “…AMAZON LINUX AMI…” you did good! If not backtrack and find out where you messed up.

Okay congrats, you have successfully launched your bare bones server! There is currently no software on it though, so the next several steps are going to involve installing PHP, a database and required libraries to support them and the web server.

Installing server software on your Amazon EC2 AMI instance

  1. First install PHP and the web server software. Type the following at your command prompt then hit “y” whenever prompted:

sudo yum install php-mysql php php-xml php-mcrypt php-mbstring php-cli mysql httpd

  1. Next install MySQL software. Type the following command and hit “y” when prompted:
sudo yum install mysql-server

 

  1. Start the mysql server by typing:
     sudo /etc/init.d/mysqld start
4.  Set the root password and record it in your notebook page you started earlier for safe keeping. Type:
mysqladmin -u root password UrPassWord
where UrPassWOrd is a unique secure character combo.

NOTE: THE NEXT FEW STEPS ARE OPTIONAL DEPENDING WHAT YOU ARE ATTEMPTING TO DO. YOU DON'T HAVE TO CREATE THE DATABASE TABLES HERE THOUGH, YOU CAN WAIT AND DO SO LATER, AS NEEDED FROM PHPMYADMIN. I included the next three steps to demonstrate how to work with mysql from the command line though as it is a good learning exercise, so you should do it anyway just for the sake of learning.
5.Log into mysql by typing:
mysql -u root –p
then enter the password from step 4.
6.  Type the following commands, hitting enter after each semicolon:
CREATE DATABASE [DataBase NAME];
CREATE USER '[DataBase NAME]_admin'@'localhost' IDENTIFIED BY '[ADMIN PASSWORD]';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER ON [DataBase NAME].* TO '[DataBase NAME]_admin'@'localhost';
CREATE USER '[DataBase NAME]_user'@'localhost' IDENTIFIED BY '[USER PASSWORD]';
GRANT SELECT, INSERT, UPDATE, DELETE ON [DataBase NAME].* TO '[DataBase NAME]_user'@'localhost';
7.  Be sure to make a note of both users you created in your notes with the usernames and passwords you used above. Type “exit” then type “sudo chown ec2-user /var/www/html” hit enter to change the owner of the html dir to the ec2-user user.
8.  Type “sudo vim /var/www/html/index.php” and hit enter to start a test .php file as the home page of your new live site on your new cloud VPS.
9.  Hit “I” to put the text editor in insert mode and type some basic HTML to test php and the server. Something like this will do:
<h1>Hello World!</h1>
10.   Hit “ESC” and type “:wq” to save and quit the vim editor. Then start your server with: "sudo service httpd start".
11.   Goto your notepad and get the public DNS address you recorded in your notes earlier. Copy and paste that address to your browswer’s address bar. If it works you should see the bold text: hello world! If it didn’t work and you receive an error with 13 in it, type “sudo chown ec2-user /var/www/html/index.php” and that should fix it.

Okay you now have a working web server! You just need to do some final steps to get the database PHPMYADMIN up and working.

 

Installing PHPMyAdmin

Now let’s install PHPMyAdmin to complete our web server:
Here is how:
1.  Navigate to the html folder:
cd /var/www/html
2.  Type: wget http://www.sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/3.4.1/phpMyAdmin-3.4.1-all-languages.tar.bz2
3.  Unzip by typing: tar -jxf phpMyAdmin-3.4.1-all-languages.tar.bz2 -C /var/www/html
4.  Rename the directory by typing: mv phpMyAdmin-3.4.1-all-languages phpmyadmin
5.  Make a config directory in the phpmyadmin main folder:
cd /var/www/html/phpmyadmin
mkdir config
cp /var/www/html/phpmyadmin/config.sample.inc.php /var/www/html/phpmyadmin/config/config.inc.php
6.  Open config.inc.php in VIM text editor by typing:
sudo vim /var/www/html/phpmyadmin/config/config.inc.php
7.  Find the line that reads “$cfg['blowfish_secret'] = '';” and change it so it reads:
$cfg['blowfish_secret'] = 'yoursecretcode';
You can enter anything you want for your secret code above as long as it’s not over approximately 60 characters.
8.  Find the line that reads “$cfg['Servers'][$i]['AllowNoPassword'] = false;” and change false to true only if you don’t have a root password set or if you are not sure. You can always come back and change this to false again later when you tighten up your server’s security as you will want to do before too long.
9.  Hit “Esc” to get out of insert mode in your text editor. Then type:
:wq 
Type it just like that, including the semicolon, to save and exit the file.
10.   Go to http://your_public_DNS/phpmyadmin/ and enter you mysql admin username and password. You should have recorded these in your notes when setting up the database above in step 7 of the previous section(not this one).

You should now be logged into your new phpMyAdmin section of your VPS server. If so, you have successfully created a cloud VPS server with all of the most essential components to develop a live dynamic PHP website! Once you are done or if you do not intend to use this instance, terminate it so you don’t get billed for it later on after your free year expires. If you intend to use it, do not terminate it or you will lose all of your work!

To learn more about how to connect to an instance go to http://docs.amazonwebservices.com/AWSEC2/latest/GettingStartedGuide/ConnectToInstanceLinux.html and learnn to connect to your instance.

To become root user in putty type:

sudo su

Starting server from putty

Open putty

Enter the public DNS of the server instance which can be found in your AWS dashboard and looks like ec2-50-16-87-242.compute-1.amazonaws.com into the host name field in putty. UPDATE: now Amazon IPs are simply like 45.23.34.22.

In the left column under “category” expand “ssh” and click on Auth

Click browse and select your security key file you created while creating your server instance, it will be a file like: adamskey.ppk you should of saved it on your desktop for convenience.

Click open and your terminal window should come up.

At the login prompt, enter ec2-user and hit enter.

You should see several lines of text with “Amazon Linux AMI” in the middle then a command prompt at the end.

You are now in your server. If you need root access, type sudo su

Reference:

The best tutorial I found on how to get a linux micro instance up and running with a web server  was at:

http://www.alexkorn.com/blog/2011/03/getting-php-mysql-running-amazon-ec2/

Tip: find public data at:

http://aws.amazon.com/publicdatasets/

 

To find out how to use Filzilla with your instance:

http://www.latentexistence.me.uk/how-to-set-up-a-free-linux-server-on-amazon-ec2/

 

 

 

 

 

 

 

Here is how I didn command line server installation second time because above didn’t work:

 

Yum install httpd

Yum install php

Yum install mysql-server mysql

Service httpd start

Service mysqld start

5 Replies to “How to Install a Web Server on Amazon EC2”

  1. My coder is trying to persuade me to move to .net from
    PHP. I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on a
    variety of websites for about a year and am concerned
    about switching to another platform. I have heard good things about blogengine.net.

    Is there a way I can import all my wordpress posts
    into it? Any help would be really appreciated!

    1. First of all, Why would you want to change? Chances are that if your developer wants you to do it, it’s because he is just better at .net than he is at PHP. PHP is by far the most popular coding language which in my eyes, makes it the best because you have more resources and information at hand always. WordPress is also by far the most popular Blog engine, making it the best in my opinion. I would advise you to find a new developer if he can’t work with it. Is there something you cannot get your WordPress site to do? if so, let me know, I am a WordPress and PHP expert and I will help you. I can make anything happen on a WordPress site! Just give me the word. .net is a bad idea in short! blogengine.net? never heard of it and when I went to see what it was, I got an error on the website and no page showing, so I really don’t think it is what you think it is. Junk it seems to me. Seriously, let me know if you want help with WordPress and/or PHP. Thanks for your comment, -Ian L. of Jafty.com

  2. I am really impressed along with your writing skills and also with the format in your weblog. Is that this a paid theme or did you modify it your self? Anyway stay up the nice high quality writing, it is uncommon to look a nice weblog like this one these days..

Leave a Reply

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