#!/bin/sh
# url: http://pank.org/scripts/
# description: Apache+MySQL+PHP (LAMP) install script for Amazon EC2, Tested in Basic 32-bit/64-bit Amazon Linux AMI 2011.02.1 Beta and AMI 2011.09
# comment: csh bash
# platform: linux
#
# You can change configure parameter to fit your needs.
#
# Version: 1.0
# http://pank.org/blog/2011/08/amp-install-for-amazon-ec2.html

clear
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
T=`date +%s` # record start time
LOG=/tmp/install-amp-for-amazon-ec2.log # v0.9 add
WORK_DIR=/tmp # can be change this if you want
APACHE_VERSION=auto # auto detect latest version
APACHE_VERSION2=2.2.22 # fix version number for auto failed
PHP_VERSION=auto # auto detect latest version
#PHP_VERSION=5.3.10 # use fix version number if auto failed
MYSQL_VERSION=auto # auto detect latest stable version
#MYSQL_VERSION=5.5.20 # use fix version number if auto failed
LONG_BIT=`getconf LONG_BIT` # 32bit or 64bit
if [ $LONG_BIT = 64 ] ; then
    ARCH=x86_64
else
    ARCH=i386
fi

mkdir -p $WORK_DIR
cd $WORK_DIR

# YUM
# update first
yum -y update 2>&1 | tee $LOG
# necessary packages for later build
yum -y install make ftp telnet w3m gcc gcc-c++ \
  curl-devel db4-devel expect glibc-devel \
  libtool \
  libjpeg-devel libmcrypt-devel libnet libpcap-devel libpng-devel \
  libtermcap-devel libxml2-devel libxslt-devel libtool-ltdl-devel \
  net-snmp-devel net-snmp-utils \
  openldap-devel openssl-devel \
  pam-devel perl perl-DBD-MySQL 2>&1 | tee -a $LOG

# APR
URL=`wget -qO- http://apr.apache.org/download.cgi | awk -F\" '/apr.*bz2/ {print $2}' | head -1`
wget --progress=dot:mega $URL | tee -a $LOG
tar jxf apr-* 2>&1 | tee -a $LOG
cd apr-*
./configure 2>&1 | tee -a $LOG
make 2>&1 | tee -a $LOG
make install 2>&1 | tee -a $LOG
cd $WORK_DIR

# APR Util
URL=`wget -qO- http://apr.apache.org/download.cgi | awk -F\" '/apr-util.*bz2/ {print $2}' | head -1`
wget --progress=dot:mega $URL | tee -a $LOG
tar jxf apr-util-* 2>&1 | tee -a $LOG
cd apr-util-*
./configure --with-apr=/usr/local/apr 2>&1 | tee -a $LOG
make 2>&1 | tee -a $LOG
make install 2>&1 | tee -a $LOG
cd $WORK_DIR

# PCRE
URL=ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.bz2
wget --progress=dot:mega $URL | tee -a $LOG
tar jxf pcre-* 2>&1 | tee -a $LOG
cd pcre-*
./configure 2>&1 | tee -a $LOG
make 2>&1 | tee -a $LOG
make install 2>&1 | tee -a $LOG
cd $WORK_DIR

# Apache
if [ $APACHE_VERSION = auto ] ; then
    URL=`wget -qO- http://httpd.apache.org/download.cgi | awk -F\" '/httpd.*bz2/ {print $2}' | head -1`
else
    URL=http://mirror.cc.columbia.edu/pub/software/apache/httpd/httpd-$APACHE_VERSION.tar.bz2
fi
wget --progress=dot:mega $URL 2>&1 | tee -a $LOG
if [ ! -f httpd*bz2 ] ; then # # if auto download failed
    wget http://ftp.tc.edu.tw/pub/Apache/httpd/httpd-$APACHE_VERSION2.tar.bz2 2>&1 | tee -a $LOG
fi
if [ ! -f httpd*bz2 ] ; then
    echo Apache source download failed, script stopped. | tee -a $LOG
    exit 1
fi
tar jxf `basename $URL` 2>&1 | tee -a $LOG
cd httpd-*
./configure --prefix=/usr/local/apache --sysconfdir=/usr/local/apache/conf \
  --enable-ssl --enable-rewrite --enable-proxy --enable-expires --enable-headers \
  --enable-reqtimeout --enable-deflate --with-apr=/usr/local/apr 2>&1 | tee -a $LOG
make 2>&1 | tee -a $LOG
make install 2>&1 | tee -a $LOG
# httpd config
sed -i 's/#LoadModule slotmem_shm_module modules/LoadModule slotmem_shm_module modules/' /usr/local/apache/conf/httpd.conf
cd $WORK_DIR

# MySQL
if [ $MYSQL_VERSION = auto ] ; then
    MYSQL_VERSION=`w3m -dump http://dev.mysql.com | awk '/5.5.*GA/ {print $2}' | head -1`
fi
MYSQL_MIRROR=http://mysql.mirrors.pair.com/Downloads # default
if [ -d /etc/yum.repos.d ] ; then # get near server for Asia area
    grep -q northeast /etc/yum.repos.d/* && MYSQL_MIRROR=http://ftp.iij.ad.jp/pub/db/mysql/Downloads # Japan
    grep -q southeast /etc/yum.repos.d/* && MYSQL_MIRROR=http://mysql.oss.eznetsols.org/Downloads # Singapore
fi                                                                                                                
wget --progress=dot:mega $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-client-$MYSQL_VERSION-1.rhel5.$ARCH.rpm 2>&1 | tee -a $LOG
wget --progress=dot:mega $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-devel-$MYSQL_VERSION-1.rhel5.$ARCH.rpm 2>&1 | tee -a $LOG
#wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-embedded-$MYSQL_VERSION-1.rhel5.$ARCH.rpm 2>&1 | tee -a $LOG
wget --progress=dot:mega $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-server-$MYSQL_VERSION-1.rhel5.$ARCH.rpm 2>&1 | tee -a $LOG
wget --progress=dot:mega $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-shared-$MYSQL_VERSION-1.rhel5.$ARCH.rpm 2>&1 | tee -a $LOG
#wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-shared-compat-$MYSQL_VERSION-1.rhel5.$ARCH.rpm 2>&1 | tee -a $LOG
#wget --progress=dot:mega $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-test-$MYSQL_VERSION-1.rhel5.$ARCH.rpm 2>&1 | tee -a $LOG
if [ ! -f MySQL-server* ] ; then
    printf "\nMySQL packages download failed, script stopped.\n\n" | tee -a $LOG
    exit 1
fi
rpm -e --nodeps mysql-libs 2>&1 | tee -a $LOG
rpm -ivh MySQL*.rpm 2>&1 | tee -a $LOG
cd $WORK_DIR

# freetds for PHP MSSQL support, optional, if download failed, skip
URL=http://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
wget --progress=dot:mega $URL 2>&1 | tee -a $LOG
tar zxf `basename $URL` 2>&1 | tee -a $LOG
if [ -f freetds-* ] ; then
    cd freetds-*
    if [ $LONG_BIT = 64 ] ; then
        ./configure --libdir=/usr/local/lib64 2>&1 | tee -a $LOG
    else
        ./configure 2>&1 | tee -a $LOG
    fi
    make 2>&1 | tee -a $LOG
    make install 2>&1 | tee -a $LOG
    WITH_MSSQL=--with-mssql
fi
cd $WORK_DIR

# PHP
URL=http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2
wget --progress=dot:mega $URL 2>&1 | tee -a $LOG
tar jxf `basename $URL` 2>&1 | tee -a $LOG
cd libmcrypt-2.5.8
if [ $LONG_BIT = 64 ] ; then
    ./configure --libdir=/usr/local/lib64 2>&1 | tee -a $LOG
else
    ./configure 2>&1 | tee -a $LOG
fi
make 2>&1 | tee -a $LOG
make install 2>&1 | tee -a $LOG
cd ..

if [ $PHP_VERSION = auto ] ; then
    PHP_VERSION=`w3m -dump http://www.php.net | awk '/Stable:/ {print $NF}' | head -1`
fi
URL=http://www.php.net/distributions/php-$PHP_VERSION.tar.bz2
wget --progress=dot:mega $URL 2>&1 | tee -a $LOG
if [ ! -f php-* ] ; then
    printf "\nPHP source download failed, script stopped.\n\n" | tee -a $LOG
    exit 1
fi
tar jxf `basename $URL` 2>&1 | tee -a $LOG
cd php-$PHP_VERSION
if [ $LONG_BIT = 64 ] ; then
    LIBDIR=--with-libdir=lib64
fi
./configure $LIBDIR \
  --with-mysql=/usr --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli $WITH_MSSQL \
  --with-apxs2=/usr/local/apache/bin/apxs \
  --with-config-file-path=/usr/local/etc --libexecdir=/usr/local/apache/libexec \
  --enable-sockets --enable-pcntl --enable-mbstring --with-iconv --with-mcrypt \
  --with-gd --with-curl --with-zlib \
  --with-jpeg-dir --with-png-dir \
  --with-gettext --enable-soap --with-xsl \
  --with-ldap --with-snmp 2>&1 | tee -a $LOG
make 2>&1 | tee -a $LOG
make install 2>&1 | tee -a $LOG
# create php.ini
if [ -f php.ini-production ] ; then # 5.3
    sed -re 's:;session.save_path.*:session.save_path="/var/tmp":' php.ini-production > /usr/local/etc/php.ini
else
    sed -re 's:;session.save_path.*:session.save_path="/var/tmp":' php.ini-recommended > /usr/local/etc/php.ini
fi
# create apache user if not exist
if ! grep -q apache /etc/passwd ; then
    groupadd -g48 apache
    useradd -u48 -g48 apache
fi
cd $WORK_DIR

# httpd.conf, making necessary change
HTTD_CONF=/usr/local/apache/conf/httpd.conf
sed -i 's/index.html/index.html index.php/' $HTTD_CONF
sed -i '/application\/x-gzip .gz .tgz/a \
\
    AddType application/x-httpd-php .php\
    AddType application/x-httpd-php-source .phps' $HTTD_CONF
sed -i 's/User daemon/User apache/' $HTTD_CONF
sed -i 's/Group daemon/Group apache/' $HTTD_CONF

# use apache as service name distinguish from default httpd
cat<<EOF>/etc/init.d/apache
#!/bin/sh
#
# Startup script for the Apache Web Server (for /usr/local/apache)
#
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#	       HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache/logs/httpd.pid
# config: /usr/local/apache/conf/httpd.conf

# Source function library.
. /etc/rc.d/init.d/functions

# See how we were called.
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/cnr/usrbin

case "\$1" in
  start)
	action "Starting httpd: " /usr/local/apache/bin/httpd -k start
	;;
  stop)
	action "Shutting down http: " /usr/local/apache/bin/httpd -k stop
        killall httpd > /dev/null 2>&1
	;;
  status)
	/usr/local/apache/bin/apachectl status
	;;
  restart)
	\$0 stop
	while pidof httpd > /dev/null
	do
	    sleep 1
	done
	\$0 start
	;;
  *)
	echo "Usage: \$0 {start|stop|restart|status}"
	exit 1
esac

exit 0
EOF
chmod 755 /etc/init.d/apache
chkconfig apache on

# Start service
/etc/init.d/apache start 2>&1 | tee -a $LOG
/etc/init.d/mysql start 2>&1 | tee -a $LOG

ls -l $WORK_DIR >> $LOG
cat /etc/resolv.conf >> $LOG
uname -a >> $LOG

echo "<?php phpinfo()?>" > /usr/local/apache/htdocs/phpinfo.php
 
if [ -d /usr/local/apache ] ; then                    A1=Existed; else A1=Missing; MISSING=1; fi
if [ -f /usr/local/apache/conf/httpd.conf ] ; then    A2=Existed; else A2=Missing; MISSING=1; fi
if [ -d /usr/local/apache/htdocs ] ; then             A3=Existed; else A3=Missing; MISSING=1; fi
if [ -f /usr/bin/mysql ] ; then                       M1=Existed; else M1=Missing; MISSING=1; fi
if [ -f /usr/sbin/mysqld ] ; then                     M2=Existed; else M2=Missing; MISSING=1; fi
if [ -f /usr/local/etc/php.ini ] ; then               P1=Existed; else P1=Missing; MISSING=1; fi
if [ -f /usr/local/apache/modules/libphp5.so ] ; then P2=Existed; else P2=Missing; MISSING=1; fi
if [ -f /usr/local/bin/php ] ; then                   P3=Existed; else P3=Missing; MISSING=1; fi
echo "

Apache service name is apache (default rpm is httpd)
MySQL service name is mysql

Aapache directory     /usr/local/apache ($A1)
Apache config         /usr/local/apache/conf/httpd.conf ($A2)
Apache DocumentRoot   /usr/local/apache/htdocs ($A3)

MySQL client          /usr/bin/mysql ($M1)
MySQL server          /usr/sbin/mysqld ($M2)

PHP config            /usr/local/etc/php.ini ($P1)
PHP module for Apache /usr/local/apache/modules/libphp5.so ($P2)
PHP CLI               /usr/local/bin/php ($P3)

Apache + MySQL + PHP installation finished.
Elapsed time: $[(`date +%s`-T)/60] minutes.
" | tee -a $LOG
if [ -z $MISSING ] ; then
    echo Try http://`wget -qO- ifconfig.me/ip`/phpinfo.php | tee -a $LOG
else
    echo Some of your files missing, check log file install-amp-for-amazon-ec2.log to debug. | tee -a $LOG
fi
echo