How to install LAMP on EC2 Ubuntu with phpmyadmin in 4 mins

After you start your AWS EC2 Ubuntu 9.10 Karmic Koala server in 6 mins, if you had to install a full LAMP (Apache PHP and Mysql) stack here is how you do it in under 4 minutes:

Start your command line and execute a command similar to below with your own keypair and Public EC2 DNS name:

Harry-MacBook-/User/Harry/ec2 $ ssh -i id_rsa-ubuntu ubuntu@ec2-185-77-129-63.compute-1.amazonaws.com

Once you are in then type in:

sudo aptitude update && sudo aptitude dist-upgrade

It will ask you for your password and it will ask you if you want to continue with the updates. Type Y and press enter.

It will install a bunch of updates and in a short while it will be done.

At that point you need to execute:

sudo reboot

This will reboot your EC2 instance and you will have to wait less than a minute before you execute your request.

Now we will install Apache, PHP and MySql using the following:

sudo aptitude install apache2 php5-mysql libapache2-mod-php5 mysql-server

During the install it will ask you for a MySql root password. Set it to something you remember just don’t leave it blank.

Test out your webserver by going to the browser and entering the public IP in the browser.

How do you know what is your public IP on EC2? It’s very easy. If you logged into the server using the public DNS ec2-185-77-129-63.compute-1.amazonaws.com, then your IP is 185.77.129.63. So go to your favorite web browser by typing in http://185.77.129.63{as an example in my case}. A page should come up which says “It works”. This is how you know Apache is working.

How do you know if php is working?
Well, you need to try a test php file on your server. For that execute the following on your command prompt:

cd /var/www

- This will take you into the web root.

Here execute the following:

vi test.php

press i at the prompt for inserting and enter the following:

press escape on your keyboard then :wq!. This will save a test.php with the above content.

Now go back to your browser and type in http://185.77.129.63/test.php

You should see a page similar to below:

php-info-screenshot

php-info-screenshot

But now how do we know if MySql is up and running for our website?

This is why we need phpMyAdmin. Here is how you install that:

sudo apt-get install phpmyadmin

Say yes to continue. And voila, its installed in a jiffy!

What is phpMyAdmin? It is a php based interface to your mysql server.

How do you test it? Well lets head back to the browser and type in the following:

http://185.77.129.63/phpmyadmin

And a page like this should show up:

phpmyadmin screenshot

phpMyadmin-screenshot

Wow that is great!!! Now how do you get in?

Use the user name: root
And remember the password you had set while installing MySql above? Use that for the password and you are in. You can now create databases and tables using a web user interface.

We need to do one more thing given that we have put our EC2 server up on the web as a web server which also has a database server. We need to security harden it. How to do this? Ubuntu makes it very easy for us.

sudo ufw enable

- This enables the Uncomplicated Firewall built-in to Ubuntu

sudo ufw allow 22

- This keeps the SSH port open

sudo ufw allow 80

- This keeps the Web Server Default port open

More on Uncomplicated Ubuntu Firewall here.

All this should take less than 4 mins if that. You have now been able to install the entire LAMP stack for yourself or your developer. With this you can get cranking on your next killer web application hosted on the reliable infrastructure of Amazon EC2.

Let me know if anyone has any other suggestions for this topic.

  • Weggrrht

    OK, I know this was a year old post… but you’re a lifesaver! Thanks a bunch!

  • Anonymous

    I am glad it helped!

  • Getpinch

    why ufw if we can configure security groups through aws console?

  • Anonymous

    For people who can understand security groups and their settings it will work well there. For people ok with machine level security ufw would work well. Remember security groups can span multiple machines on aws and some people may not want to do that.

  • Bwell

    I found that the following steps were also needed to get phpmyadmin working under Apache:

    In a terminal type:
    sudo vi /etc/apache2/apache2.conf

    Add the following line to the end:
    Include /etc/phpmyadmin/apache.conf

    and restart the apache:
    sudo /etc/init.d/apache2 restart

  • guest

    Hi, yes I worked through this. However when I request http://184-73-49-56 (in my case) it says that the server is not found….but if I type the full public DNS i get a page that says “It Works!” – so when I create my test.php file I cannot request to see this…..
    Any suggestions?

    Everything before this is spot on.
    Thanks,
    U.

  • Checkwit

    what about FTP ? How can I upload files to the server?

  • Anonymous

    You need to use sftp instead. Use a program such as FileZilla (Free client) to do so.

  • Anonymous

    That is very strange. Make sure you double check the ip and may be wait for a few minutes for the DNS propagation for it to work.

  • Anonymous

    Yes you are correct. It is not required but I wanted to make sure people knew about the native ubuntu firewall.