Amazon EC2, Apache, MySQL, PHP, and Tomcat 7 Tutorial for Laura

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay hey Laura its Jeff I'm just going to quickly show you how to get set up with a Amazon ec2 virtual machine how to install a web server on it how to install a database server on it how to configure it to use PHP in case you wanted to go the PHP route I'll show you how to build a very simple comments form in PHP just to prove that PHP is working and then I'll also show you how to set up a tomcat server in case you wanted to use Java server pages instead of PHP and all of this we'll do hopefully in under 20 minutes so let's get started so the first thing you need to do is go to aws.amazon.com and sign up for an account now what they offer here is called a free usage tier and what this gives you is basically a free virtual machine for twelve months ok so they give you a Linux micro instance well it's basically a very small virtual machine that has six hundred and thirteen Meg's of RAM and it can either be 32 or 64 bits the rams not very much however for the purpose of a CS 2212 project this is way more than enough so it'd be no problem whatsoever so they give you 750 hours per month for 12 months which means basically you can run this virtual machine continuously for an entire year and you won't pay anything now the catch is that an AWS account requires a valid credit card and this is going to be a problem for students because some of them might not have credit cards now they don't actually charge you anything because they have this free usage tier but if you were to ever go over this then they need to have a credit card on file so that you know they can charge you so the problem here is that again students might not have credit cards so the solution is that they have this AWS Amazon Web Services in education program that you fill out this simple form here and they will give you a teaching grant that will give you $100 in Amazon credits for each of your students and that'll be good for up to a gear so you'd have to apply once every summer for the upcoming year and yeah you should be able to get free virtual machines for all of your students so you just have to fill out this form it requires you to enter your email it can be a dot edu or equivalent so this is valid in Canada or anywhere else in the world it doesn't have to be an edu account and it does require you to have an Amazon Web Services account already so you will have to sign up and enter a credit card and then give them the email address that you use to sign up for Amazon Web Services if you don't feel comfortable doing that I'm happy to put it on my credit card that's not a problem whatsoever just say the word so it asks you for your course course URL and the number of students so you'd want to give them the number of students that you're expecting to have for the entire year so you know if you get like 50 per semester then put in 100 here alright so what it says in the FAQ here is that you apply with this form they review your application and if your grants accepted they'll notify you via email they'll send you a link to a site where you can redeem the credits and get all the accounts set up for your students so I realize this is kind of a pain in the butt but I think that the benefits here really outweigh the disadvantages I think this is going to be this could be I should say a great learning experience for the students so assuming that you have an Amazon Web Services account set up then what you want to do is go to my account click on the AWS management console and simply log in so I'm going to log in here and we'll go to Amazon ec2 that's where you manage your virtual machines and then going to click on launch instance I want to start up a new virtual machine I'll click continue and what they give you here is a list of predefined virtual machine images that you can use so I'm going to go to community am is I'm going to set up a virtual machine using Ubuntu and I just happen to know the ami ID it's the virtual machine ID for this particular image that I want to use so I just type that in and it just takes a minute to pull it up ok there we go so now I'm just going to click on select and I'm going to make sure that micro is selected and you can see here that it's free to your eligible so I'm not going to be charged anything for it so I'm going to click on micro instance leave everything else as is and then I'm going to click this box that says prevention against accidental accidental termination what this does okay the default on ec2 is that if you were to shut down your virtual machine then the default on ec2 is that they delete it it goes away it's gone by enabling this termination protection basically if you were to shut it down then it would just be stopped and you could restart it again later if you wanted to so I'll just check that and I'm just going to give it a name here so I'll say CS 20 to 12 server for Group one continue okay it's going to ask me to create a key pair so I'll create a new key pair here I'll call it say it's CS 22 12 this key pair is what I'm going to use to SSH into my server because they don't use password authentication so I'm going to click on create and download my key pair and you can see it just downloaded that key pair for me so we'll get to that later and now it's going to ask you to choose a security group this is basically the firewall I created a blank firewall here for this demo just because the existing firewall that I have already has a bunch of settings in it so I just wanted to start fresh but you would just select default so I'm just going to click this and click continue and everything's set to go so I'll click on launch so it says I'm it's now launching I'll click on close and go to my instances list and you can see here that my CS 2212 server is starting up it's in the pending state and it just takes a minute for it to start up now while that's going on I'm going to grab my key here that I just downloaded and I'm going to SSH I'm going to SCP it to Oh bollocks so I'll copy it up to obliques so that it's ready I'm going to do this entire demo from Obelix you could certainly do it from your own system on Windows you'd have to use cygwin on Linux or Mac you just open up a terminal but I'll do it from Obelix because I imagine a lot of students would be doing it for mobile X as well so you can now see that my 2212 server is running so I'm going to SSH into Obelix and I'm going to just click on this server here in my console and grab its hostname okay so there's the hostname and I'm going to try SSH into it now the user that I have des SH into as is Ubuntu so I'll say SSH Ubuntu at ec2 blah blah blah and you can see it's not working now the reason is that we have to go into the group here and we have to allow the SSH port so I'm just going to say inbound SSH add rule and then apply and now I've opened up the SSH port in my security group now you would have clicked on default I created this this CS 2212 security group but you would click on default and add the rule there okay so I'll try that again and you can see now it's listening and it gives me permission denied and that's because I have to use that CS 2212 key that I downloaded from them so I'm going to say SSH - I give it the file name of the key file and then Ubuntu at that hostname and you can see that I am now ssh din and if i just take a look here DF - H you can see that it's given me an 8 gig partition so there's plenty of space there for the students now I'm just going to exit out of here for a second I don't want to have to type every single time SSH - I bla bla bla bla bla so what I'm going to do is go into my dot SSH directory on Obelix I'm going to move that key into that directory and I'm going to create a file called config and I'm going to say host now right here I can put whatever alias I want to use for this server so I'm going to use the alias ec2 and the hostname is going to be that big long host name the user is going to be a bun - and the identity file is going to be tilled / - ssh cs20 to 12-pound that that PE m file that we downloaded from amazon so now if I save this I can simply type SSH ec2 from the console and I'm in perfect ok so now that we've got the server up and running we need to install some software on it we need to install a web server a database server get PHP configured so I'm going to switch to the root user by saying sudo su now I'm root and I'm going to say apt-get install apache2 we need the Apache web server we need PHP we need the module the PHP module' for apache' and then we'll install MySQL server as well for the database server so it's going to ask me if I want to do it I say yes hit enter and we'll just let it install for a second okay so it asks me for the root password that I want to use for MySQL so I'll just enter something enter it again okay so now that we have everything installed I'm just going to clear the screen here and then I'm going to restart Apache just to make sure that PHP is properly enabled by saying service Apache to restart that just restarts the web server and last thing I want to do you don't have to do this but it's good practice to run this MySQL underscore secure underscore installation script so it's going to ask you for your root password I enter that I don't want to change it so I say no and it asks me if I basically I'm just locking down my database server here so I'm going to remove my anonymous users I'm going to disable root log in remotely and I'm going to remove the test database and access to it and reload my privileges table so you don't have to do that but it just locks it down it's just hire security basically okay so now we want to see if this web server is up and running and working so once again I'm just going to grab that host name for that server and I'm going to go and try and access it but you can see here that it's just sort of doing nothing the reason for that is that we have to open up port 80 so I go into my security groups again you'd be clicking on default I'm clicking on CS 20 to 12 here and I'm going to create a new rule and I want to create one for HTTP I want to allow the HTTP port 80 click on apply rule changes and now let's see if we refresh this page you can see that it's working now it's actually responding perfect okay so we want to quickly create some sort of PHP app just to make sure that it's working so I'm going to MySQL log in as the root user and I'm going to specify a password since I set a root password so I'll enter that and let's create a database here create database CS 22:12 use CS 22 12 and then I'll just I'm going to create a simple comments form so I'll create a comments table and it's going to be it'll have an integer ID null that'll be its primary key so we'll have Auto increment here and we'll take the user's name which will make say a bar chart of 255 characters not null and then the body of the comment will be text not null and then finally the primary key will be the ID and that should do it so we've got the comments table created now we have to create a user because we don't want to use our root MySQL user in this comments app because that would be insecure so I'll say create user we'll call it 22 12 DB user at localhost identified by and then a password so the password I'll say CS 22 12 for life right and then we have to grant all privileges on our CS 2212 database all tables within the CS 2212 database to 20 to 12 DB user that user we just created 2012 DB user at localhost and then last thing I have to do is just flush the privileges to get them to take effect all right so the database is set up and now we're going to create the actual comments form so by default on Ubuntu Apache stores all of its files in slash var slash W W so I'm going to edit this index.html file and I'll just quickly put in a comments form so HTML the body tag create our form action equals we'll say it's going to post to a file called comment PHP the method is post and then we'll have name input type equals text name equals name and then we'll have a comment that'll be a text area with say 10 rows 80 columns and its name will be body and then we just need our submit button input type equals submit all right close out our form close out our body close out the HTML page so if I reload that just make sure it works perfect so now we just have to create the comment stop PHP file so go in here and create this start it off and we're going to have our DB host name as localhost our database username we said was 20 to 12 DB user our DB password whoops we set to bc s 20 to 12 for life and our database name we created the database as CS 22 12 okay so I need to connect to the database so I'm going to grab a handle to the database MySQL connect DB hostname pass the DB username pass the DB password and if that doesn't work then we'll die with an error message and we'll get MySQL to print out the error message that occurred okay assuming that worked then we'll select the database with MySQL select DB and I'm going to grab the parameters that have been posted to my form so post name and the body of the comment body and then finally I'll say try inserting it insert into comments name body values name oops body alright and obviously this is totally insecure I'm not doing any sort of input validation so somebody could totally do an SQL injection attack but it's just for testing so assuming that worked we're going to print out a simple message that says I don't know thank you and your comments was received and if that didn't work then I'm going to print out an error message that just says error we're sorry but your request could not be completed at this time okay and then we'll just close out our PHP block here and we should be good to go so we'll switch back to our web server here Jeff Shantz hello world submit and nothing happens now what happened was there was an error that happened behind the scenes and we can take a look at that by looking up VAR log Apache error dot log and you can see we have this PHP fatal error called undefined function MySQL connect now I did this intentionally and the reason I did this is I wanted to show you why one of the benefits of having a virtual machine if they were to be doing this say on Obelix they would have to be looking at a log that was maybe shared with a lot of different users and so all sorts of error messages would be intertwined by having their own server that can look at their logs and they can know that yeah this is an error that came from my script okay so I just think it's it's nice to have their own system that they can play with and get to understand how things work behind the scenes ok so we have this call to undefined function MySQL connect and that's because I didn't install the connector that tells PHP how to connect to MySQL so I'm going to say apt-get install PHP 5 dash minus 2 that'll get installed and now I simply have to restart Apache service Apache to restart my web servers restarted and I should be able to now go back submit my form and you can see I get my thank-you and if we just confirm that in the database use CS 2212 select star from comments you can see that my comment has been inserted so PHP is working properly everything's great okay so that's how you setup Apache and PHP assuming you wanted to go the route of Java server pages you would need to use Tomcat instead of Apache so I'm going to first just remove Apache I wouldn't have to remove it I could have stopped it I guess okay and instead I'm going to install Tomcat seven so this is an application server there are lots of different JSP application servers like WebSphere and I don't know JBoss and stuff like that but Tomcat is free it's pretty lightweight so it's probably the one to use so this will just take a minute to install it's installing Java it's installing Tomcat and all the dependencies for it okay by default Tomcat runs on port 8080 so if you wanted to access it you would have to go to port 8080 on this host and again you can see it's not responding and that's because we have to go to our security group here and allow 8080 now 8080 is not in this list so I'm just going to create a custom rule type in 8080 for the range click on add and then apply and now when I go back here and I click on reload there we go so now it's working with Tomcat now it's not very nice to have it running on port 8080 it would be nicer if it we're just running on port 80 so that we could just type in something like this you can change Tomcat to run on port 80 but if you want to do that it has to run as the root user and it's not very secure it's not usually recommended so instead what the hack is is that you just redirect port 80 to port 8080 so that Tomcat can be running on port 8080 and any requests that come in on port 80 will be redirected to it so this is just thing that the students could just copy and paste they don't really need to understand this I'm just creating a an IP tables rule to redirect from port 80 to port 8080 alright and then I'm just going to say IP tables save to make sure if we were to reboot this virtual machine that rule would still be in effect so now I should be able to go here and and look at that I don't have to specify the port anymore so again Tomcat is still running on port 8080 but I'm just using this firewall rule to redirect port 80 to port 8080 okay so what do we want to do next well we want to be able to access our console here so it comes with a manager web app that you can use to deploy dot war files to deploy JSP applications to the server but you have to create a user in order to be able to access that so I'm going to edit the file et Cie Tomcats and it's Tomcat - user stat XML so I'm going to go under this Tomcat users tag and I'm going to create a user we'll give it the username of Jeff and a password we'll just say is admin 1 2 3 roles the roles will be manager - GUI and admin - GUI again this is just something that the students could copy and paste into their file ok so we save that up and I just have to restart the tomcat server so I'll say e.t.c entity Tomcat 7 restart and that restarts it okay and I should be able to you know what I just realized that I forgot to install Tomcat seven - admin so I have to install the admin site so I just installed that I'm going to restart my tomcat server again sorry about that and once it started up I can now go back click on the manager web app it asked me for the username password I'll put in the one I just created Jeff admin one two three and now I'm into the application manager and so this is where you can see all the applications that are running on the server and you can deploy a new application so the students would compile their JSP application into a war file and they would simply upload the file and deploy it and then it would be listed as one of the applications in this list and they could play with it I'm not going to get into that right now but hopefully you get the picture alright so that pretty much covers it I'm not sure if I got under my 20 minutes or not but yeah hopefully you can see that this is really not that hard and I think the students could totally handle it and I think it'd be a great learning experience for them alright talk to you later
Info
Channel: Jeff Shantz
Views: 48,915
Rating: 4.9108062 out of 5
Keywords: amazon, ec2, amazon ec2, ubuntu, linux, apache, mysql, php, tomcat, western university, uwo, jeff shantz
Id: pZFSSVVdqmw
Channel Id: undefined
Length: 21min 54sec (1314 seconds)
Published: Thu Apr 19 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.