#!/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
# comment: csh bash
# platform: linux
#
# You can change configure parameter for your needs.
#
# Version: 0.8
# http://pank.org/blog/2011/08/amp-install-for-amazon-ec2.html

clear
T=`date +%s` # record start time
WORK_DIR=/tmp
APACHE_VERSION=auto # auto detect latest version
#APACHE_VERSION=2.2.21 # use fix version number if auto failed
PHP_VERSION=auto # auto detect latest version
#PHP_VERSION=5.3.9 # use fix version number if auto failed
MYSQL_VERSION=auto # auto detect latest stable version
#MYSQL_VERSION=5.5.19 # 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
yum -y update # update first
yum -y install make ftp telnet w3m gcc gcc-c++ \
  curl-devel db4-devel expect glibc-devel \
  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

# 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://ftp.tcc.edu.tw/pub/Apache/httpd/httpd-$APACHE_VERSION.tar.bz2
    URL=http://mirror.cc.columbia.edu/pub/software/apache/httpd/httpd-$APACHE_VERSION.tar.bz2
fi
wget $URL
tar jxvf `basename $URL`
cd httpd-*
./configure --prefix=/usr/local/apache --sysconfdir=/usr/local/apache/conf \
  --enable-ssl --enable-rewrite --enable-proxy --enable-expires --enable-headers --enable-deflate
make     
make install
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.mirror.mirrors.pair.com/Downloads # default
if [ -d /etc/yum.repos.d ] ; then
    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 $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-client-$MYSQL_VERSION-1.rhel5.$ARCH.rpm
wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-devel-$MYSQL_VERSION-1.rhel5.$ARCH.rpm
#wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-embedded-$MYSQL_VERSION-1.rhel5.$ARCH.rpm
wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-server-$MYSQL_VERSION-1.rhel5.$ARCH.rpm
wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-shared-$MYSQL_VERSION-1.rhel5.$ARCH.rpm
#wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-shared-compat-$MYSQL_VERSION-1.rhel5.$ARCH.rpm
wget $MYSQL_MIRROR/MySQL-${MYSQL_VERSION:0:3}/MySQL-test-$MYSQL_VERSION-1.rhel5.$ARCH.rpm

rpm -e --nodeps mysql-libs
rpm -ivh MySQL*.rpm
cd $WORK_DIR

# freetds for PHP MSSQL support
URL=http://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
wget $URL
tar zxvf `basename $URL`
cd freetds-0*
if [ $LONG_BIT = 64 ] ; then
    ./configure --libdir=/usr/local/lib64
else
    ./configure 
fi
make
make install
cd $WORK_DIR

# php
URL=http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2
wget $URL
tar jxvf `basename $URL`
cd libmcrypt-2.5.8
if [ $LONG_BIT = 64 ] ; then
    ./configure --libdir=/usr/local/lib64
else
    ./configure
fi
make
make install
cd ..

if [ $PHP_VERSION = auto ] ; then
    PHP_VERSION=`w3m -dump http://www.php.net | awk '/Stable:/ {print $NF}'`
fi
URL=http://www.php.net/distributions/php-$PHP_VERSION.tar.bz2
wget $URL
tar jxvf `basename $URL`
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-ttf --with-gettext --enable-soap --with-xsl \
  --with-ldap --with-snmp
make
make install
# 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
/etc/init.d/mysql start

echo "<?php phpinfo()?>" > /usr/local/apache/htdocs/phpinfo.php
 
echo "
Apache + MySQL + PHP installation finished.
Elapsed $[(`date +%s`-T)/60] minutes.

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

Apache config /usr/local/apache/conf/httpd.conf
Apache DocumentRoot /usr/local/apache/htdocs

Try http://`curl ifconfig.me`/phpinfo.php
"