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:
- Click the “EC2” tab.
- On the left column of the page, under the “Network & Security” heading, click on “Security Groups”.
- 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.
- 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.
- 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.
- 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.
- Now go back to the left navigation column of the page and click on the "key pairs" link.
- Click “Create Key” in the right column.
- 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.
- Picking up on the same page we left off on above, click on “instances” in the left navigation column of the amazon webpage.
- Click “Launch Instance” to make the instance wizard appear. Make sure “Classic Wizard” is selected in the pop up and click on “Continue”.
- 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...".
- In the “instance details” and the step after it, just accept the defaults by clicking on “Next...” two times.
- 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.
- 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.
- Now in the “configure firewall” pop up, select the preexisting security group you named earlier and click continue.
- 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”.
- Check the instance name and make sure no others are checked.
- 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.
- 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
- Open Putty and enter the public DNS where it says “Host name”.
- 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.
- 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
- 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
- Next install MySQL software. Type the following command and hit “y” when prompted:
sudo yum install mysql-server
- 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.
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:
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.
Now let’s install PHPMyAdmin to complete our web server:
Here is how:
1. Navigate to the html folder:
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:
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:
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:
Starting server from 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 184.108.40.206.
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
The best tutorial I found on how to get a linux micro instance up and running with a web server was at:
Tip: find public data at:
To find out how to use Filzilla with your instance:
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