Install Apache, PHP, APC, & MySQL on Amazon EC2 with Amazon Linux AMI

The following guide is a step by step guide on installing and configuring Apache, PHP (with APC) & MySQL (LAMP Stack) on an Amazon EC2 instance running Amazon Linux AMI. The procedure has been tested on Amazon Linux AMI, but should also apply in general to Fedora/Red Hat/CentOS distributions. Also, should you be interested in manually installing PHP APC straight from the PHP PECL library, please check this guide.

 

1. Install Linux updates, set time zones, followed by GCC and Make

sudo yum -y update
sudo ln -sf /usr/share/zoneinfo/America/Indianapolis \
      /etc/localtime
sudo yum install -y gcc make

 

2. Install PHP and PHP extensions

sudo yum install -y php
sudo yum install -y php-devel php-mysql php-pdo
sudo yum install -y php-pear php-mbstring
sudo pear install Log
sudo yum install -y pcre-devel

 

3. Install Apache httpd

sudo yum -y install httpd
sudo service httpd start

 

4. Install MySQL

sudo yum -y install mysql-server mysql
sudo service mysqld start

 

5. Install PHP APC module

sudo yum -y install httpd-devel
sudo pecl install apc-beta
echo "extension=apc.so" | sudo tee /etc/php.d/apc.ini
Or Simply:
sudo yum install -y php-pecl-apc

 

6. MySQL Configuration

sudo vi /etc/my.cnf
[mysqld]
...
skip-external-locking
long_query_time=1
slow_query_log
slow_query_log_file=/var/log/log-slow-queries.log
log-bin=mysql-bin
server-id= 1
...
[mysqld_safe]
...
myisam_recover_options
...
[mysqld]
...
key_buffer_size = 128M
max_allowed_packet = 3M
table_open_cache = 64
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 16M
thread_cache_size = 8
query_cache_size= 32M
thread_concurrency = 8
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
innodb_additional_mem_pool_size = 8M
innodb_log_buffer_size = 4M
...
Log Slow Queries:
sudo touch /var/log/log-slow-queries.log
sudo chown mysql.mysql /var/log/log-slow-queries.log
sudo /sbin/chkconfig --levels 235 mysqld on
sudo service mysqld start

 

7. PHP Configuration Changes in php.ini

sudo nano /etc/php.ini
expose_php = Off
memory_limit = 128M
date.timezone = America/Indianapolis
Note: Location of error file
/var/log/httpd/error.log

 

8. Auto Start Apache in EC2 Amazon Linux

sudo /sbin/chkconfig --levels 235 httpd on
sudo service httpd start

If this tutorial was helpful to you, then please do not forget to leave a comment, or two. :)

  • Pingback: Install PHP APC on Amazon EC2 Linux AMI or CentOS

  • Pingback: Install PHP-FPM, Nginx, and MySQL on Amazon Linux AMI - Coding Steps

  • http://twitter.com/Jason_Blackwell Jason Blackwell

    this was awesome thanks.

    • saxenap

      Well, nothing better then knowing that this was useful to someone. :) Thanks for leaving a note Jason!

  • http://alexsexton.com/ Alex Sexton

    The php apc stuff is great since the ec2 amis fail on a yum install for those.

    • saxenap

      Well I’m glad to find that you liked this installation guide Alex! Good luck!

  • jobish

    ‘If this tutorial was helpful to you, then please do not forget to leave a comment, or two. ‘
    ———— super
    by the by
    awesome thanks dear ‘saxenap :)

  • Zubair Khan

    i love you man! This saved my life.

    • saxenap

      I love you for leaving a note as well Zubair! ha ha.
      Thanks!

      • http://twitter.com/El_Duderino_van El Duderino

        I’m feeling the love too, just *awesome*! namaste

  • Stacey

    Very helpful. Thank you!

  • Arun Prabu

    Thank you… Your tutorial helped me submit my app for AWS startup challenge 2012. Thanks a lot.

  • Ravi

    Thanks a lot. you really rock. You saved all my time.

  • Arjuna

    Thanks so much. The amazon docs are terrible. Your tutorial helped me so much

  • Gabriel

    You’re the mayun.

  • Gabriel Nahmias

    Once again, you’re the man.

  • blackbelt

    thanks for valuable tutorial

  • Bob

    sudo pear install Log
    WARNING: channel “pear.php.net” has updated its protocols, use “pear channel-update pear.php.net” to update
    WARNING: “pear/DB” is deprecated in favor of “pear/MDB2″
    Did not download optional dependencies: pear/DB, pear/MDB2, pear/Mail, use –alldeps to download automatically
    pear/Log can optionally use package “pear/DB” (version >= 1.3)
    pear/Log can optionally use package “pear/MDB2″ (version >= 2.0.0RC1)
    pear/Log can optionally use package “pear/Mail”
    pear/Log can optionally use PHP extension “sqlite”
    downloading Log-1.12.7.tgz …
    Starting to download Log-1.12.7.tgz (46,898 bytes)
    ………….done: 46,898 bytes
    install ok: channel://pear.php.net/Log-1.12.7

  • Nikhil J

    Helped me a lot.Thanks :D

  • Pingback: Steps to set up LAMP on Amazon ec2 micro-instance from scratch | WL's Blog

  • Gary Z

    This is life saver! Thank you!

  • Pingback: - Coding Steps

  • Neochange

    Great, this is a good start for a development using amazon servers.
    Thanks!

  • Pingback: Install Apache 2.4, PHP 5.4, APC, & MySQL on Amazon EC2 with Amazon Linux AMI - Coding Steps

  • joel

    very, very helpful!!!