×
Community Blog How to Host Multiple Websites on a Single ECS Instance

How to Host Multiple Websites on a Single ECS Instance

In this article, we will learn how to host more than one website on an ECS instance with Docker Compose and a Reverse Proxy.

Alibaba Cloud ECS instance can potentially serve more than one website with Docker Compose and a Reverse Proxy, there are multiple solutions to handle different content in one single instance ECS:

  1. Subfolders ("example.com/web1", "example.com/web2", "example.com/web3")
  2. Port-based Virtual Hosting ("example.com:80", "example.com:8080", "example.com:8181")
  3. Subdomains ("web1.example.com", "web2.example.com", "web3.example.com")
  4. Name-based Virtual Hosting ("web1.com", "web2.com", "web3.com")

Now you can gain deep insights on ECS and integrate the best practices on the cloud to empower your business on our Academy Day Online Conference in March 5th, 2020 with free ACA Cloud Computing certification exams. Sign up free now!

The best (and more elegant) solution here is to use Name-based Virtual Hosting, as it helps you to use better naming for your sites. And nowadays, software such as Docker performs operating-system-level virtualization also known as containerization, where, from the running programs point of view, they look like real computers. The benefit of using this is the isolation that occurs between services running in different containers, as a website running in one container won’t have any connection (by default) with a website running in another container.

But containerization itself still doesn’t solve the problem of handling the HTTP Requests to give the proper website to the visitors of each site. That's why we need a Reverse Proxy, a service that can also run as a container and intercepts the requests to send them to the correct place, making it the main part for handling Virtual Hosting.

So we need a Name-based Virtual Host solution which will run on Docker, having a Reverse Proxy and at least 2 websites running on it.

Each container in a docker-compose.yml config file is called "service", and goes into the services array in a YAML format. The services in our file will be:

  1. proxy
  2. web1 (web1.com)
  3. web2 (web2.com)

For simplicity’s sake, I’m using the standard and unmodified official image of PHP 7.2 with Apache (php:7.2-apache) in both web services. "web1" and "web2" will show, therefore, the generic message from Apache telling you that "It works!". Something that you can just change by entering by command line into the container with docker exec -it web1 bash.

Then after you installed Docker on your ECS instance following the official documentation, you can create a file called "docker-compose.yml" with the contents being shown in the following section and run it like a normal Docker Compose project.

docker-compose.yml

version: '3.6'

services:
  proxy:
    image: neilpang/nginx-proxy
    network_mode: bridge
    container_name: proxy
    restart: on-failure
    ports:
      - "80:80"
      - "443:443"
    environment:
      - ENABLE_IPV6=true
    volumes:
      - ./certs:/etc/nginx/certs
      - /var/run/docker.sock:/tmp/docker.sock:ro
  web1:
    image: php:7.2-apache
    network_mode: bridge
    container_name: web1
    restart: on-failure
    environment:
      - VIRTUAL_HOST=web1.com
  web2:
    image: php:7.2-apache
    network_mode: bridge
    container_name: web2
    restart: on-failure
    environment:
      - VIRTUAL_HOST=web2.com

But if you don't have an ECS instance yet, then I recommend you to do it with Terraform, then please check the details here.

Related Blog Posts

Run Docker on Alibaba Cloud's Elastic Compute Service (ECS)

Alibaba Cloud's Elastic Compute Service (ECS) is a range of elastic cloud-based server products that are simpler and more efficient to manage than physical servers. You can create instances, add memory to scale up when required, build out connected VPCs in different regions, and use them as essential components of Alibaba Cloud's vast range of cloud products and services.

Docker is a management tool that allows you to develop, deploy, and run applications inside containers. This process is known as containerization.

You can run many containers at the same time on the same host machine. These containers are managed by Docker instead of the OS hypervisor. Containerization means that applications run in total isolation from each other. However, you can also configure ports for communication between running containers. In contrast, Virtual Machines take up significant computational power from the host machine and most often have far too many resources than required.

In this tutorial, we'll show you how to set up, install, and run Docker on an Alibaba Cloud Elastic Compute Service (ECS) instance.

How to Install Mastodon Using Docker on Alibaba Cloud ECS

Mastodon is an open source decentralized social network that allows anyone to host their own server node in the network. In this tutorial, I will install and set up Mastodon on Alibaba Cloud Elastic Compute Service (ECS) with Docker on Ubuntu 16.04.

Related Documentation

Add an existing ECS instance - Container Service

You can add an existing ECS instance in the following ways:

  1. Add ECS instances automatically: The image and system disk of the ECS instance are reset by using this method. You can add one or more ECS instances to the cluster at a time.
  2. Add the ECS instance manually: Manually add the ECS instance by running scripts on the ECS instance. You can only add one ECS instance to the cluster at a time.

Health check of Docker containers

In a distributed system, the service availability is frequently checked by using the health check to avoid exceptions when being called by other services. Docker introduced native health check implementation after version 1.12. This document introduces the health check of Docker containers.

Related Products

Elastic Compute Service

Alibaba Cloud Elastic Compute Service (ECS) provides fast memory and the latest Intel CPUs to help you to power your cloud applications and achieve faster results with low latency. All ECS instances come with Anti-DDoS protection to safeguard your data and applications from DDoS and Trojan attacks.

Deploy ECS instances with just a few clicks from the easy-to-use console and scale capacity up or down based on real-time demands. This means you only pay for the resources you use and avoid the need to procure expensive IT infrastructure and hire large network teams.

Container Service

Container Service is a high-performance and scalable container application management service that enables you to use Docker and Kubernetes to manage the lifecycle of containerized applications.

Container Service offers a variety of application publishing methods and continuous delivery capabilities and supports microservice architectures.

Container Service simplifies establishment of container management clusters and integrates Alibaba Cloud virtualization, storage, network, and security capabilities to create the optimal container-running environment on the cloud.

Related Course

Introduction to Docker Container

This course introduces the basic concepts and features of docker container technology, then showes the steps of basic installation and some frequently used commands. At the same time introduces the the basic concepts and architecture of container service on alibaba cloud, and then shows the steps of container based application creation procedure from scratch.

Related Special Offer

Elastic Compute Service Starter Packages

Alibaba Cloud offers easy-to-use high-performance virtual machines with data transfer plan starting from $2.50 a month now.

0 0 0
Share on

Alibaba Clouder

2,599 posts | 758 followers

You may also like

Comments