Tag: aws

  • AWS EC2 PHP7 upgrade

    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

  • Deploying to AWS with Phing

    Deploying to AWS with Phing

    After a recent switch to Amazon Web Services, I thought updating my Phing build XML would be a straightforward task. It wasn’t. There weren’t a lot of resources out there for this particular scenario so I decided to write this blog piece.

    AWS comes with good security out of the box. This is obviously a good thing but it does require a bit more thought when setting up your Phing build file.

    Filesync
    During the process of setting up your micro instance, you will be prompted to create an SSH private key which you can download. Put this file somewhere safe and ensure you update the permissions.

    chmod 644 mykey.pem
    All deployment will be done using the ec2-user user. So to save frustration ensure that this user has the write permissions on your target directory (on your target box).

    Where my file sync previously prompted for a password the identityfile parameter automatically connects to instance.

    <filesync
    sourcedir="${source.path}"
    destinationdir="${target.user}@${target.host}:${target.path}"
    verbose="true"
    checksum="true"
    excludeFile="${exclude.file}"
    identityfile="${source.identityFile}" />
    SCP/SSH
    You would think this would be the same as filesync. Unfortunately not. This requires you to create a public key using your private key. From the private key location run the following.

    ssh-keygen -f mykey.pem -y > mykey.pub
    Now in your build XML add these parameters

    <scp
    username="${target.user}"
    privkeyfile="${source.identityFile}"
    pubkeyfile="${source.pubIdentityFile}"
    host="${target.host}"
    todir="${target.path}/mytargetpath"
    autocreate="true"
    file="${source.path}/local.txt" />
    You should be good to go. Hope this is useful.