Lowering Costs With EC2, WordPress and Docker

Introduction

In today’s digital landscape, businesses of all sizes are leveraging the power of the cloud to enhance their online presence. Amazon Elastic Compute Cloud (EC2) and Docker have emerged as popular tools for deploying scalable and cost-effective solutions. In this blog post, we will explore the technical implementation and financial benefits of utilizing EC2 and Docker to run WordPress and MySQL, two widely used components of modern web applications. We will also briefly touch on securing the site and using nginx to add a Node.js server running alongside WordPress on the same server.

Cost Optimization with EC2

Amazon EC2 offers a pay-as-you-go model, enabling businesses to optimize their costs based on usage. Here’s how EC2 contributes to the financial benefits of running WordPress and MySQL:

  • Flexibility in Instance Types: EC2 allows you to choose the most suitable instance type based on your application’s requirements, balancing between performance and cost. You can opt for smaller and cheaper instances during periods of low traffic, and scale up when demand increases.
  • Reserved Instances: EC2’s Reserved Instances provide significant cost savings compared to On-Demand instances. By committing to a longer-term usage, businesses can reduce their expenses further.
  • Spot Instances: EC2’s Spot Instances allow you to bid on unused capacity, leading to potential cost savings of up to 90% compared to On-Demand instances. While these instances may be interrupted if the spot price exceeds your bid, they are ideal for non-mission-critical workloads like development or testing environments.

Containerization with Docker

Docker provides a lightweight and efficient containerization solution, bringing additional financial benefits when running WordPress and MySQL:

  • Resource Optimization: Docker enables you to run multiple isolated containers on a single EC2 instance, reducing resource wastage. By optimizing resource allocations, you can efficiently utilize server resources, resulting in lower hosting costs.
  • Easy Scalability: Docker’s container-based architecture simplifies scaling operations. When facing increased traffic or workload demands, you can easily spin up additional containers to distribute the load across multiple instances. This flexibility allows you to handle growth without incurring unnecessary expenses.
  • Development and Deployment Efficiency: Docker’s containerization eliminates the need for complex configuration and installation processes, making it easier to develop, test, and deploy software. This streamlined workflow reduces the time and effort required, ultimately resulting in cost savings.

Putting It Into Action!

It’s time to dive into the technical implementation. Here’s how we used EC2, Docker, WordPress, MySQL and nginx to build the Coder Lanes website:

  1. Since we are still a small school and expect relatively light traffic, we started with a t2.micro instance, which also happens to offer a free tier for further cost reduction.
  2. While it is possible to use different operating systems for your implementation, we elected to go with Amazon Linux.
  3. Assign this server an elastic IP address, and connect this IP address to your domain through your domain host.
  4. Open ports 22, 80 and 443 into your instance. Port 22 is needed for SSH access, ports 80 and 443 are needed for web access.
  5. Create a directory where your site’s data will be stored, for example /usr/local/mysite/site. Change the owner of this directory to ec2-user.
  6. In this directory, create 3 subdirectories:
    • /usr/local/mysite/site/database – this is where MySQL will store its database
    • /usr/local/mysite/site/wordpress – this is where WordPress will store its data
    • /usr/local/mysite/site/html – this is where Node.js will store its data
  7. Install OS updates and some additional packages that can be useful later:
    • sudo yum update
    • sudo yum upgrade
    • sudo yum install mysql git
  8. Install Docker:
    • sudo amazon-linux-extras install docker
    • sudo service docker start; sudo usermod -a -G docker ec2-user; sudo chkconfig docker on
    • sudo reboot
    • sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    • sudo chmod +x /usr/local/bin/docker-compose
  9. Create the docker-compose yaml:
    • Create a new /usr/local/mysite/site/docker-compose.yaml file and copy the below into it:
/usr/local/mysite/site/docker-compose.yaml
version: '3.9'
services:
  mariadb:
    image: mariadb/server:latest
    restart: always
    ports:
      - 3306:3306
    environment:
      MARIADB_ROOT_PASSWORD: somestrongpassword
      MARIADB_DATABASE: wordpress
      MARIADB_USER: "'wordpress'@'%'"
      MARIADB_PASSWORD: mypassword
    volumes:
      - /usr/local/mysite/site/database:/var/lib/mysql
  wordpress:
    depends_on:
      - mariadb
    image: wordpress:latest
    restart: always
    ports:
      - 8080:80
    user: 1000:1000
    environment:
      WORDPRESS_DB_HOST: mariadb
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: mypassword
      WORDPRESS_TABLE_PREFIX: wp_
    volumes:
      - /usr/local/mysite/site/wordpress:/var/www/html
  node:
    build: ./html
    restart: always
    ports:
      - 3000:3000
    environment:
      PORT: 3000
    volumes:
      - /usr/local/mysite/site/html:/app
      - /app/node_modules
    command: "npm start"
  1. Start everything:
    • docker-compose up -d
  2. Install nginx:
    • sudo amazon-linux-extras install nginx
    • sudo systemctl enable nginx
    • sudo systemctl start nginx.service
  3. Configure nginx:
    • Create a new /etc/nginx/conf.d/mysite.conf file and copy the below into it:
/etc/nginx/conf.d/mysite.conf
server {
    server_name mysite.com;
    listen 80;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_pass "http://127.0.0.1:8080";
    }
    location /html/ {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_pass "http://127.0.0.1:3000/";
    }
}
  1. Set a TLS cert for the site:
    • sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    • sudo yum install certbot python-certbot-nginx
    • sudo certbot –nginx
    • Follow the prompts to install the certificate into nginx
    • Add to cron: sudo crontab -e, and add 0 3 * * * certbot renew -q
  2. Assuming your site’s DNS is mysite.com, login to WordPress at https://mysite.com and configure WordPress as relevant (plugins, users, themes etc).
  3. Copy the Node.js files to /usr/local/mysite/site/html/. The Node.js server will be accessible at https://mysite.com/html.

With the above steps, we’ve created a containerized architecture with WordPress, MySQL and Node.js running in containers and with their data stored in external volumes under /usr/local/mysite/site/ for easy backup.

This implementation also runs the Node.js server alongside WordPress, and has these routes open: https://mysite.com will connect to the WordPress instance, and https://mysite.com/html will connect to the Node.js server. This arrangement allows you to build your own Node.js app that delivers hands-on exercises or any other auxiliary service on the same all-in-one server.

Maintenance and Management

After the site is launched, another financial advantage we can leverage is the reduction in maintenance and management costs:

  • Automated Infrastructure: EC2 provides several management features like auto-scaling, load balancing, and automated backups, reducing the need for manual intervention and minimizing administrative overheads.
  • Simplified Updates and Rollbacks: Docker’s image-based approach simplifies application updates and rollbacks. By maintaining a library of Docker images, you can quickly deploy new versions or revert to previous ones, saving both time and resources.
  • Enhanced Stability: Containerization isolates individual applications, preventing conflicts and dependency issues. This isolation enhances stability, reducing the risk of downtime and associated financial losses.

Summary

By leveraging the power of Amazon EC2 and Docker for running WordPress and MySQL, businesses can unlock significant financial benefits. From cost optimization through instance flexibility and spot instances to resource optimization and simplified management with Docker, these technologies offer scalable and efficient solutions. Embracing EC2 and Docker empowers businesses to make the most of their cloud infrastructure while minimizing costs, ultimately helping them achieve their financial objectives.

Similar Posts