Setting up an Amazon EC2 instance LAMP stack

Script to set up a LAMP stack to run a PHP website from an Amazon EC2 instance:

#!/bin/bash

set -eu
set -o pipefail

# Commands to set up an AWS EC2 instance as a web server, by installing a LAMP
# stack. These commands are for Amazon Linux instances, and will not work for
# Ubuntu instances

# These steps assume that you have already launched an instance with a public
# DNS name that is reachable from the Internet. You must also have configured
# your security group to allow SSH (port 22), HTTP (port 80), and HTTPS (port
# 443) connections.

# The commands must be run on the instance itself

# perform a quick software update on your instance
sudo yum update -y

# install the Apache web server, MySQL, and PHP software packages
sudo yum install -y httpd24 php56 mysql55-server php56-mysqlnd

# Start the Apache web server
sudo service httpd start

# configure the Apache web server to start at each system boot
sudo chkconfig httpd on

# Add the www group to your instance
sudo groupadd www

# Add your user (in this case, ec2-user) to the www group
sudo usermod -a -G www ec2-user

# Change the group ownership of /var/www and its contents to the www group
sudo chown -R root:www /var/www

# Change directory permissions of /var/www and subdirectories to add group
# write permissions and to set the group ID on future subdirectories
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;

# Recursively change the file permissions of /var/www and its subdirectories
# to add group write permissions
find /var/www -type f -exec sudo chmod 0664 {} \;