Setting up a LAMP server on AWS EC2

Amazon Web Services have grown quite rapidly over the last few years. This is why they’re generally the most popular scalable hosting that large companies use to ensure their applications are up and running with no pitfalls.

Amazon provide something called ‘free tier’ so you can get started straight away and get stuck in. Go ahead, set up an account and have a play about. It’s free for the first year, either cancel your account after before that point or, if you’ve gotten far enough, it’s pretty cheap anyway.

Once you’re logged in via SSH, use the following commands to set up your LAMP server. Please read the code line by line to ensure you have copied and pasted every command in correctly, and that you understand what each one does (using the helpful comments).

This code will install a simple LAMP server on AWS installing PHP 5.5, MySQL, Intl, Git, APC and ElasticSearch.

# Install the web server (with php55, httpd24, mysql apc, intl and git)
sudo yum update -y
sudo yum install -y httpd24 php55 php55-mysql php55-pdo php55-gd php55-mysqlnd mysql-server php55-pecl-apc-3.1.15 php55-intl git

# Start the http service
sudo service httpd start
sudo chkconfig httpd on
sudo groupadd www
sudo usermod -a -G www ec2-user
exit

# Log back into the server (now that we are added to a new group)
sudo chown -R root:www /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} +
find /var/www -type f -exec sudo chmod 0664 {} +

# At this point we can add a phpinfo page to check installation has succeeded
#echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
#rm /var/www/html/phpinfo.php

# Start mysqld and run a secure mysql installation
sudo service mysqld start
sudo mysql_secure_installation
sudo chkconfig mysqld on

# Create ssh key for server
ssh-keygen -t rsa -C "[email protected]"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
#cat ~/.ssh/id_rsa.pub

# Create the directory and install the source
mkdir -p /var/www/handle/production
cd /var/www/handle/production
git clone [email protected]/handle.git --recursive .

# Install composer dependances and generate autoloader
curl -sS https://getcomposer.org/installer | php
php composer.phar install --no-dev

# Change PHP configuration (allow short tags and add timezone = Europe/London)
sudo vi /etc/php.ini

# Set up Virtual Hosts
sudo vi /etc/httpd/conf.d/vhosts.conf

# @todo: Add EOF syntax

# <VirtualHost *:80>
# ServerAdmin [email protected]
# DocumentRoot /var/www/handle/production/public
# #SetEnv APPLICATION_ENV "production"
# AliasMatch ^/core/(.*)$ /var/www/handle/production/core/Company/public/$1
# ServerName handle.production.company.co.uk
# ErrorLog logs/handle.production.company.co.uk-error_log
# CustomLog logs/handle.production.company.co.uk-access_log common
# <Directory />
# Options FollowSymLinks
# AllowOverride None
# </Directory>
# <Directory /var/www/handle/production/public>
# Options Indexes FollowSymLinks MultiViews
# AllowOverride All
# Order allow,deny
# allow from all
# Require all granted
# </Directory>
# </VirtualHost>

# Create handlef.production.company.co.uk subdomain
# Eventually attach Route 53 API here

# Restart httpd
sudo service httpd restart

# Import Elasticsearch
sudo rpm --import https://packages.elasticsearch.org/GPG-KEY-elasticsearch

# Create custom repository
sudo vi /etc/yum.repos.d/elasticsearch.repo

# Paste in the following
[elasticsearch-1.5]
name=Elasticsearch repository for 1.5.x packages
baseurl=http://packages.elasticsearch.org/elasticsearch/1.5/centos
gpgcheck=1
gpgkey=http://packages.elasticsearch.org/GPG-KEY-elasticsearch
enabled=1

# Install Elasticsearch
sudo yum install -y elasticsearch

# Add Elasticsearch to startup routine
sudo chkconfig --add elasticsearch

# Create index
curl -XPUT 'http://localhost:9200/handle/'

# Edit the apcu configuration to allow caching on the command line
sudo vi /etc/php-5.5.d/apcu.ini

# Uncomment the following line and change to 1
apc.enable_cli=1

If you’re looking for specific packages to install, take a look at Amazon’s package list:¬†http://aws.amazon.com/amazon-linux-ami/2013.09-packages/