Free AWS EC2 Ubuntu-Apache-PHP-MySQL setup

Amazon Web Services is an advanced cloud platform which offers lots of features. As cloud platforms go it is mostly for larger projects that need to be able to scale. That said Amazon offers a free tier, which means that you can test out their features for one year for free. Yes, you get cloud services to play with for free! More info on Amazon’s Free Tier page.
AWS can have a slightly steep learning curve if you haven’t worked with cloud administration before so here is a straight forward guide to get your own LAMP box up and running on AWS.

  1. Create a new AWS account here, if you are an existing Amazon customer you can simply login with that account. Once you are logged in go into the AWS Management Console, from the menu in the upper right of the page.
  2. Now we need to set up your security group, the basics for allowing you to login to your server. Click on EC2 to enter the EC2 Dashboard. EC stands for Elastic Cloud which is Amazon’s name for their Cloud Servers. Click on Security Groups under Network & Security in the left menu and select the default group in the table. Select the Inbound tab and add rules for SSH, HTTP and HTTPS, then click Apply Rule Changes. If you need other protocols for your server you need to add them here.
  3. Let’s create an instance. Click on Instances under Instances in the left menu and click Launch Instance. Select the Quick Launch Wizard and type in a descriptive name for your instance. Choose to create a new key pair and download the key, which is a .pem file you need to access your server securely. Then select a launch configuration from the list, my suggestion is one of the Ubuntu Server configurations which are eligible for the free tier. You always need to keep an eye on what you do in the AWS Management Console since you don’t want to enable stuff Amazon will charge you for. Click Continue and then Edit Details. Make sure that under Security Settings the default security group is selected. It is important that you configure the security groups before launching the respective instance, since you won’t be able to change it after launch. Click Launch and in a couple of seconds your instance will be up and running. Congratulations you have a new LAMP box to play with.
  4. In order to connect to the instance we need to assign an IP address. In the left menu in the EC2 Management Console select Elastic IPs under Network & Security. Allocate a new address and assign it to your instance. Elastic IPs are free as long as they are assigned to an instance, so remeber not to leave any unallocated IP addresses as you will get charged by the hour. Remember the assigned IP.
  5. On your local machine use the pem key to SSH into your instance. On OS X change the permission of the key to 600 and use following command in terminal:
    ssh -i yourkeyname.pem ubuntu@yourelasticip

    and boom you are in.

  6. Update the Ubuntu package manager and the installed packages:
    sudo apt-get update
    sudo apt-get dist-upgrade
  7. Install Apache:
    sudo apt-get install apache2
  8. Enable .htaccess files by editing the /etc/apache2/sites-available/default file, fx with vi. Look for the /var/www directory and make sure it contains AllowOverride All.
  9. Enable the rewrite module in Apache:
    sudo a2enmod rewrite
  10. Install PHP5:
    sudo apt-get install libapache2-mod-php5
  11. Restart Apache:
    sudo /etc/init.d/apache2 restart
  12. Ensure that Apache is running by typing the instance IP in your browser.
  13. Allow the Ubuntu user to work with the /var/www folder:
    sudo adduser ubuntu www-data
    sudo chown -R www-data:www-data /var/www
    sudo chmod -R g+rw /var/www

    Logout and login again to pick up the new user group. Test by creating a directory and deleting it again in www without having to sudo. This will make your life easier since permissions will get messed up less once you start adding frameworks and tools to your web service.

  14. Test PHP by cd into /var/www folder and typing sudo vi phptest.php. Hit i key to enter insert mode and just write a simple php script with a phpinfo() call.
    Hit ESC to exit insert mode and type :wq to save and quit the vi editor. Go to instanceIP/phptest.php in your browser and you should see the phpinfo displayed.
  15. Install MySQL:
    sudo apt-get install mysql-server
    sudo apt-get install php5-mysql

    Insert root password for the database and remember it.

  16. To test MySQL let’s install PHPMyAdmin:
    sudo apt-get install phpmyadmin

    Select Apache2 as server and Create Databases, just follow the instructions. Go to instanceIP/phpmyadmin to test. If there are any issues check the ubuntu community for help.

You can use FTP with your instance by setting up SFTP. I recommend using Git though, as described in my web development setup post as well as configuring MySQL Workbench instead of PHPMyAdmin. Also once you have your instance configured create an AMI image of it in your EC2 Management Console so that you can just launch a new instance from your image next time or if you need to add another to your setup.

50 thoughts on “Free AWS EC2 Ubuntu-Apache-PHP-MySQL setup

  1. Pingback: Configuring an Amazon EC2 instance with ubuntu, apache2, mysql (LAMP) - Shashank Agarwal | Shashank Agarwal

  2. Raveesh

    Thanks a ton, Chris, this was very, very helpful. Just one part I’d like to add: in case a user isn’t able to access PhpMyAdmin after setting it up, they need to simply run this line:

    sudo ln -s /usr/share/phpmyadmin /var/www

    1. Chris Hjorth Post author

      Glad I could help and thank you for the addition Raveesh.

      I didn’t experience the issue myself and don’t really use phpmyadmin anymore, I find MySQL Workbench much more useful. If I can reproduce the issue I will add it to the post.

  3. Eric-Pierre Menard

    I have to say, you are the light at the end of a long tunnel.

    The post was clear, concise and descriptive unlike most posts who provide some commands with no detail as to what is being done or why.

    I found it very informative and removes much anger I had against AWS and steep learning curve.


      1. Matt

        Hey, I know this comment’s kinda late to the party, but I got to step “12: Ensure that Apache is running by typing the instance IP in your browser.”, and discovered that my browser will not open the basic welcome message from Apache2.

        I was hoping you might hav some insight into this as I’m at a complete loss right now?


        1. Matt

          Sadly I’m unable to edit my comment as a guest but there is also no “/etc/apache2/sites-available/default” file. It is, on my server at least, 000-default.ssl? This seems not to have the ‘AllowOverride None’ anywhere in it 🙁

  4. Davo

    Awesome post man! Thanks a lot!

    One extra doubt maybe someone can help: can we create more than 1 elastic IP (attached to an instance) for free? It’s kinda tricky in the aws docs. Thanks in advance 🙂

    Let’s code my smart friends!

    1. Chris Hjorth Post author

      Np. As far as I know elastic IPs are free as long as they are attached to an instance. It seems possible to assign two elastic IPs to the same instance in the AWS console. My guess is that the reason they are charging for unassigned IPs is to avoid people just registering IPs and then forgetting about them. So I don’t think they will charge for assigning an extra IP to the same instance.
      I would just try it and keep an eye on the AWS billing just to be sure. Please let me know if it isn’t so.

      1. Davo

        Hello again Chris, I was going to follow your advice but before that I found this info in AWS documentation:

        Can an instance have more than one Elastic IP or Public IP?
        Today, an instance can only have one Internet routable IP address. If an Elastic IP is mapped to an instance its existing Public IP address mapping is removed.

        Another fact to keep in mind is that you can actually create unto 5 elastic IPs per account (you’ll need to contact AWS if you need more).

        I was thinking about getting 2 different IPs for one instance because I wanted to add those IPs to my domain registrant, however there’s another AWS services called “Route 53” 😀 pretty useful and cheap.

        Thanks again for your time and help Chris, keep the good job 😉

        Best regards.

        1. Chris Hjorth Post author

          This is really good information, thank you for taking time to post it here.
          I didn’t know about Route53, I’ll check it out.
          Best regards to you too Davo.

  5. klaus


    I am new to Linux/Apache.

    In step 8, you are enabling .htaccess but in the Apache documentation it says to avoid .htaccess. Can you please clarify?

    Also, should stay away from 64-bit ubuntu since php is no more supported on 64-bit OS?


    1. Chris Hjorth Post author

      Hi Klaus,
      it is all explained in the Apache .htaccess documentation here. Basically there are the following two reasons:
      – Performance: the server will look in each directory of your site for .htaccess files which can be avoided by keeping configuration in the main configuration file.
      – Security: you allow users (read developers allowed to upload to your server) to modify the configuration, which might not be desired.
      In general I would use .htaccess files during development and then move everything to the main configuration file for production. Since you are new to Apache this should not concern you much so just stick with .htaccess files.

      Where have you read that PHP is not supported on ubuntu 64-bit? It is supported as far as I am aware, but can’t remember if anything has changed. I have switched to using Lighttpd mostly (I recommend you stick with Apache though until you get a hang of it).

  6. Dave

    Hi I am struggling with point 12. I am getting a 403 error with permission denied. I changed my default security group to allow all traffic and all ports but even then I get the same thing. I went back and removed the edits I had made in the 000-default.conf file and it worked.

    As the 000-default.conf file is new since your post could someone give me an idea of the code to add here as I dont see a section. Adding one myself broke everything

  7. Ankit Singh Thakur

    Hey Chris! thanks for such a nice list of instructions 🙂
    I have configured many of AWS environment each time with your given instructions, can you please create a similar one for configure phpmyadmin for same environment with details ?


  8. Pingback: Moving wordpress from shared host to aws ec2 | stlplace

  9. Nguyễn Lương Huynh

    Hi everyone.

    This tutorial very good. Step by step setting server. I have problem with deloy source (Laravel).
    When I up source to EC2 with Cyberduck and i have error:

    Error in exception handler: The stream or file “/var/www/html/app/storage/logs/laravel.log” could not be opened: failed to open stream: Permission denied in /var/www/html/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84

    please help me.

    1. Chris Hjorth Post author

      Sorry for the extremely late reply, but I have had zero time for my blog the past two years. Hope you solved the issue, although I haven’t had the chance to work with Laravel yet, it sounds like permission issues, so I would double check on the folder and file permissions on your server.

  10. Pingback: AWS micro Ubuntu, Apache2, Python, installation and configuration

  11. Hanumanthu

    I have come across couple of other procedures before I ran into yours which is more lucid compared to others. All was as easy as 1-2-3. Thank you, very much. By the way, you were suggesting to use Mysql Workbench than phpmyadmin. In fact, I have used both of them in the past and found them comfortable. Is there a specific reason performance wise or anything that you would suggest mysql workbench to phpmyadmin.

    thanks a lot once again.

    1. Chris Hjorth Post author

      No reason in particular regarding the user experience for simple tasks. But with Workbench you avoid having to install and maintain extra software on your server. Sorry for the late response, I have been really busy and had zero time for my blog.

  12. Pingback: AWS EC2 + LAMP + Ubuntu + Drupal install - 虾子酱油

Leave a Reply

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