Build an Amazon EC2 LAMP Server
Posted in
First dog watch, 1 bell (4:34 pm)

Why build your own when there are a lot of community options?

I don't know, go try them and come back if you need to build your own. This article will help you build a LAMP (Linux, Apache, MySQL, PHP) server from a default Amazon AMI cloud image.

This article assumes you can create an EC2 image and connect to it, and focuses on what to do next.

First off, update the server!. From the command line, run:
sudo yum update
and follow the prompts until the system is up to date.

The default AMI machine doesn't have Apache on it. The easiest way to install the latest available (to the Amazon image, anyway) version is to run the following commands:

sudo yum install httpd
sudo yum install mod_ssl
sudo yum install mysql
sudo yum install mysql-server
sudo yum install php
sudo yum install php-mysql

If you want to install specific versions, you may need to build from source. If you want to do this, see my how to compile Apache2, PHP5, and MySQL page.

Now you've installed them, but nothing is really ready to go yet. Let's start with MySQL:

cd /etc/rc.d/init.d
sudo ./mysqld start
sudo /usr/bin/mysql_secure_installation

and follow the prompts from the script to create a password for the mysql user, remove the public test database, etc.

Next, let's fix the startup scripts, because right now only MySQL is running, Apache is stopped. If you reboot the machine, neither will start by default:

cd /etc/rc.d/rc3.d
sudo rm K15httpd
sudo rm K36mysqld

By default, AMI images start at runlevel 3 (cf. /etc/inittab). Now that we've removed the roadblocks to getting the services started, let's link them up. From the same directory (/etc/rc.d/rc3.d), run:

sudo ln -s ../init.d/mysqld S30mysql
sudo ln -s ../init.d/httpd S85httpd

You can adjust the start priorities if you want, this is just a suggestion.

You will probably need to make a few changes to the Apache config file before your website is ready to start up, so:

sudo vi /etc/httpd/conf/httpd.conf

and make any changes you need. Always test your config file before trying to run Apache, it will save you downtime and heartache: just run httpd -t and it will check your syntax.

I always move my web directories and logfiles, etc to a /www directory. If you do this, make sure you update logrotate appropriately. A busy server can run out of space if you don't watch your logfile growth.

sudo vi /etc/logrotate.d/httpd

Now, start up Apache and you're ready to go:

sudo /etc/rc.d/init.d/httpd start


6 Responses to “Build an Amazon EC2 LAMP Server”

  1. Scurvy Jake says:

    If you get tired of typing 'sudo' in front of every command, you can change to the root user by executing 'sudo -s'

  2. Jose Galego says:

    Thanks for the article.

    I found, that the below line:

    sudo ln -s ../init.d/mysqld S30mysql

    Must have a 'd' at the very end:

    sudo ln -s ../init.d/mysqld S30mysqld

    It saved me a weekend of google searches.

  3. Scurvy Jake says:

    I run several servers like this, are you sure it wasn't just the d at the end of '../init.d/mysqld' that you were missing?

  4. [...] Build an Amazon EC2 LAMP Server [...]

Leave a Reply