AWS EC2 PHP7 upgrade

Nick Bennett

25/08/2017

  • Archetecture
  • PHP
  • Technology

Goal: Upgrade AWS EC2 to PHP7
Secondary Goals: Learn Docker

After the dust had settled from the birth of our 2nd child, I decided it was time to revisit my to-do list. Top of that list was to upgrade my AWS EC2 instance to PHP7. Those braver than myself may have simply just upgraded and hoped for the best. My doomsday mindset wouldn’t allow me to do this. Although, as it turned out the more gun-ho approach would have probably been fine.

My existing production environment is a LAMP stack with all dependencies installed on the same AWS AMI EC2 instance. I use this WordPress blog as a service so this is also hosted on the same box. I also have several side projects each with its own VHOST entries. My dev environment is using Zend server community edition which is still using PHP5. This dev environment is what I’m hoping I can replace with Docker.

Preparation

Ok, first up I created a new directory that would contain all files I would need on the box.

/container
/container/configs/apache — an vhost conf file for each domain
/container/mysql/ – SQL dump files to import tables
/container/sites/nickbennett/ – all files for main site
/container/sites/blog/ – all files for the blog

A point to note regarding the SQL files is because the DB is blank you will need to set up your initial user e.g.

/container/mysql/user.sql

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mypassword');

/container/mysql/databases.sql

CREATE DATABASE IF NOT EXISTS nickbennett;

The Dockerfile

FROM amazonlinux:2017.03
RUN yum update -y
RUN yum install -y php70 php70-mysqlnd httpd24 mysql56-server nano.x86_64
ADD sites/nickbennett/ /var/www/site/nickbennett
ADD sites/blog/ /var/www/site/blog
ADD configs/apache/ /etc/httpd/conf.d/
ADD mysql/ tmp/
EXPOSE 80
CMD service httpd start
CMD chkconfig mysqld on
CMD service mysqld start
CMD mysql < /tmp/users.sql
CMD mysql --password=mypassword < /tmp/databases.sql
CMD mysql --password=mypassword < /tmp/nickbennett.sql
CMD mysql --password=mypassword < /tmp/blog_nickbennett.sql

Then to build

docker build -t nbsite .
docker run nbsite

wait something happened…there were no errors…but I can’t access anything on port 80!

I run docker ps and there is no container listed. I tried without much luck to find the answer. My assumption was that the httpd service running would be enough to keep the container running. I even raised a stack overflow question…

Stack Overflow

Luckily, an old colleague came to the rescue with this command…

docker run -it -p 8080:80 --rm nbsite bash

This runs bash within the container so as long as I remain logged into bash the container would remain open. The only downside is all of the ‘CMD’ calls made in the Dockerfile would no longer be run. These would have to be run manually. To save running these each time I created a new directory /container/Bash and inside it, I made an executable shell file with the same commands as the docker file. I simply copied this onto the container and ran it from the command line. Hey presto my local AWS AMI PHP7 box is up and running! I can access the site via port 8080 i.e. nickbennett.dev:8080 (remember to update your local /etc/hosts).

To use the container as a dev environment I need the ability to edit the local files on the container. Docker has a simple -v command which allows you to map a local directory to the one on your container. I can edit the files locally and see the change immediately in the browser.

docker run -it -p 8080:80 -v /LOCAL_PATH_TO_CONTAINER_DIR/container/sites/nickbennett:/var/www/site --rm nbsite bash