Part 1: NGINX, OpenSSL, and the static site all bundled into Docker

 The requirement listed here is for an NGINX Docker container to be deployed onto a linux EC2 host with a self signed SSL cert.

For this, we can show off just a little bit and embellish the static hosted site across NGINX.
Let's create a directory for our nginx, static-site, and all the required files.
mkdir nginx
cd nginx
touch Dockerfile
mkdir static-site
cd static site
touch index.html index.css nginx.conf
Let's start by adding the html and css quick. Of course, feel free to do whatever you want to this.
 
index.html
<!DOCTYPE html>

<html>
<head>
    <title>Nginx EC2</title>
    <link href="index.css" rel="stylesheet">
</head>
<body>
    <main class="main">
        <h1>Hi, I'm Scotty</h1>
    </main>
</body>
</html>
index.css
body {
    margin:0;
    font-family: Arial, Helvetica, sans-serif;
    background: linear-gradient(to right, #5c258d, #4389a2);
    color:white;
}

.main {
    width:100%;
    height:100vh;
    display:flex;
    justify-content: center;
    align-items: center;
}
Next, we can add the standard nginx config, only with some modifications (I have trimmed out the options and added the SSL server with the locations of where our certs will be in the next step):
 
Note: you can find the basic config here
 
nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
            root   html;
        }
    }

    server {

        listen 443 http2 ssl;
        listen [::]:443 http2 ssl;

        server_name localhost;

        ssl_certificate /etc/nginx/certs/nginx-selfsigned.crt;
        ssl_certificate_key /etc/nginx/private/nginx-selfsigned.key;

        location / {
            root   html;
            index  index.html index.htm;
        }

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

}
Now, save those and open up the Dockerfile one level up:
Here we are taking the base and lastest Nginx image from docker and then making the dirs for our certs and copying our static files over into /etc/.
Then we are running the OpenSSL command to generate a self signed cert with our information. In this case, you will get a security warning when accessing the ec2 from the browser. Go ahead and replace my information with your information in that RUN command.
 
Dockerfile
FROM nginx

RUN mkdir /etc/nginx/private /etc/nginx/certs
COPY static-site/index.html /etc/nginx/html/
COPY static-site/index.css /etc/nginx/html/
COPY static-site/nginx.conf /etc/nginx/nginx.conf

RUN openssl req -x509 -nodes -days 365 \
-subj "/C=US/ST=FL/O=Parlor Design, LLC/CN=scottyfullstack.com" \
-newkey rsa:2048 -keyout /etc/nginx/private/nginx-selfsigned.key \
-out /etc/nginx/certs/nginx-selfsigned.crt;
finally in this step, we can build and push the docker image to your own docker repo:
docker build . -t scottyfullstack/nginx-static --no-cache
docker push scottyfullstack/nginx-static

 


 

Part 2 : Terraform

Comments

Popular posts from this blog

Different Types of Reports in Scrum - Agile

Terraform

Scrum Master Interview help - Bootcamp