
## PREAMBLE: Upload this file to the EC2 instance; edit the file inline, make it executable, and then run it.         ##
scp -i ~/.ssh/MyFirstKeyPair.pem /data/Downloads/AWS/ec2DefaultConfig.apt.sh ubuntu@our.company.dynamic.dns.name.amazonaws.com:~/ec2DefaultConfig.apt.sh #Delete
ssh -i ~/.ssh/MyFirstKeyPair.pem ubuntu@our.company.dynamic.dns.name.amazonaws.com "sed -i 's/.*#Delete//g' ~/ec2DefaultConfig.apt.sh" #Delete
ssh -i ~/.ssh/MyFirstKeyPair.pem ubuntu@our.company.dynamic.dns.name.amazonaws.com "chmod a+rx ~/ec2DefaultConfig.apt.sh; ~/ec2DefaultConfig.apt.sh" #Delete
exit #Delete

## INSTALL SERVICES ##
sudo apt-get update -y
sudo apt-get install -y php
sudo apt-get install libapache2-mod-php
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password **********'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password **********'
sudo apt-get -y install mysql-server
sudo apt-get install -y apache2
sudo apt-get install -y php-mysqlnd
sudo service apache2 start
sudo service mysql start
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password **********'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password **********'
#sudo mysql_secure_installation

## INSTALL TOMCAT ##
sudo apt-get install tomcat7

## ADD MOD JK TO APACHE ##
sudo apt-get install libapache2-mod-jk
ls -al /etc/libapache2-mod-jk/workers.properties
sudo ln -s /etc/apache2/mods-available/jk.conf /etc/apache2/mods-enabled/
sudo ln -s /etc/apache2/mods-available/jk.load /etc/apache2/mods-enabled/

## ENABLE SSL FOR APACHE ##
sudo ln -s /etc/apache2/mods-available/ssl.conf /etc/apache2/mods-enabled/
sudo ln -s /etc/apache2/mods-available/ssl.load /etc/apache2/mods-enabled/
## CHANGE AN INCORRECT DEFAULT VALUE IN ssl.conf ##
sudo sed -i 's/SSLSessionCache /#SSLSessionCache /g' /etc/apache2/mods-enabled/ssl.conf

## CONFIGURE GROUPS ##
sudo groupadd www
sudo usermod -a -G www ubuntu
sudo chown -R root:www /var/www

## CONFIGURE FILES ##
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;

## RESTART SERVICES ##
sudo service mysql restart
sudo service tomcat7 restart
sudo service apache2 restart

#wget http://passmaster.okesystems.net/phpmyadmin

## TEST PHP ##
sudo echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
sudo chmod a+rx /var/www/html/phpinfo.php
wget http://www.companyName.com/phpDirectory/phpinfo.php
rm /var/www/companyName/phpDirectory/phpinfo.php

## INSTALL S3 MOUNT TOOL ##
sudo apt-get install -y build-essential git libfuse-dev libcurl4-openssl-dev libxml2-dev mime-support automake libtool
sudo apt-get install -y pkg-config libssl-dev
git clone https://github.com/s3fs-fuse/s3fs-fuse
cd s3fs-fuse/
./autogen.sh
./configure --prefix=/usr --with-openssl
make
sudo make install

## CONFIGURE S3 MOUNT POINT ##
echo 'key*********:keySecret***************' > ~/.passwd-s3fs
chmod 600 ~/.passwd-s3fs
mkdir /tmp/cache # cache for S3FS
chmod 777 /tmp/cache
sudo mkdir /mnt/s3 # define mount point
sudo s3fs -o allow_other,use_cache=/tmp/cache companyName.classifcation.bucket /mnt/s3 # 'companyName.classifcation.bucket' is your S3 bucket name
sudo -i # by default default user on EC2 cannot alter /etc/fstab
echo 's3fs#companyName.classifcation.bucket /mnt/s3 fuse allow_other,use_cache=/tmp/cache 0 0' >> /etc/fstab
exit # get out of root login and return to default user

## CHANGE OWNER, PERMISSIONS ON S3 MOUNT POINT ##
sudo chown -R root:www /mnt/s3/www
sudo chmod -R a+r /mnt/s3/www # Fix for unexplainable S3 errors - some files with wrong permissions
sudo find /mnt/s3/www -type d -exec chmod og+x {} \; # Fix for unexplainable S3 errors - some directories with wrong permissions


