Set up Web Hosting with Docker on Ubuntu 24.04

By Raman Kumar

Updated on Jul 17, 2024

In this tutorial, we'll discuss about set up web hosting with Docker on Ubuntu 24.04 server with Nginx for proxy server and Certbot for SSL certificate

In today's digital age, scalability is a crucial aspect of web hosting. Whether you're hosting a personal blog or managing a business website, Docker offers a versatile and efficient way to handle web hosting. This tutorial will guide you through installation process of web hosting environment using Docker.

Prerequisites

Before we dive in, ensure you have the following:

  • A dedicated server or virtual machine running a Ubuntu 24.04 (preferably Ubuntu 20.04 or later).
  • Basic knowledge of Docker and Docker Compose.
  • Root or sudo access to the server.

Step 1: Install Docker and Docker Compose

First, we need to install Docker and Docker Compose on our server.

Installing Docker

Update the package database:

sudo apt update

Install necessary packages:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the Docker repository to APT sources:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update the package database with Docker packages:

sudo apt update

Execute following command to install Docker:

sudo apt install docker-ce

Verify the installation:

sudo systemctl status docker

Installing Docker Compose

Download the latest version of Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the binary:

sudo chmod +x /usr/local/bin/docker-compose

Verify the installation:

docker-compose --version

Step 2: Configure Firewall

We need to add HTTP and HTTPS ports in firewall.

ufw allow 80/tcp
ufw allow 443/tcp
ufw reload

Step 3: Generate SSL Certificates

First, we need to install Certbot and Nginx plugin. Execute following command:

sudo apt install certbot python3-certbot-nginx

Use Certbot to generate the SSL certificates. Replace example.com with your domain name.

sudo certbot certonly --standalone -d example.com 

Follow the prompts to complete the certificate generation process. The certificates will be saved in /etc/letsencrypt/live/example.com/.

Step 4: Setting Up a Basic Web Server

We'll use Nginx as our web server. Create a directory for your Docker setup and navigate into it.

mkdir web-hosting && cd web-hosting

Dockerfile

Create a Dockerfile for Nginx:

nano Dockerfile

Add following content:

# Use the official Nginx image from the Docker Hub

FROM nginx:latest

# Copy custom configuration file from the host to the container
COPY nginx.conf /etc/nginx/nginx.conf

# Copy website content to the container
COPY html /usr/share/nginx/html

Nginx Configuration

Create an nginx.conf file for custom Nginx configuration:

Note: Replace example.com with your domain name.

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
    types_hash_max_size 2048;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  example.com;
        return 301 https://$host$request_uri;
    }

    server {
        listen 443 ssl;
        server_name example.com;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page  404 /404.html;
        location = /40x.html {
        }

        error_page  500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

HTML Content

Create an html directory and add a simple index.html file:

mkdir html
nano html/index.html

Add following HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Nginx!</title>
</head>
<body>
    <h1>Success! The Nginx server is working!</h1>
</body>
</html>

Docker Compose Configuration

Create a docker-compose.yml file to manage your Docker containers:

nano docker-compose.yml

Add following content:

version: '3.9'
services:
  web:
    build: .
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt

Step 5: Building and Running the Docker Containers

With the configuration files in place, it's time to build and run your Docker containers.

Build the Docker image:

sudo docker-compose build

Run the Docker containers:

sudo docker-compose up -d

Verify the setup:

Open a web browser and navigate to your server's IP address. You should see the Nginx welcome page.

Conclusion

You've successfully setup web hosting environment with Docker. Docker's flexibility and efficiency make it an excellent choice for modern web hosting solutions. Feel free to expand this setup with more services, databases, or caching solutions as needed.