Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple NGINX Test with Docker-Compose

DISCLAIMER: This is for testing and development purposes only. NOT FOR PRODUCTION USE.

This is a very simple NGINX web server using Docker with a self-signed certificate. The idea behind this repo was a very quick and portable way to test out load balancing on Ubuntu Server instances.

For native instructions without using Docker, please see the Native System Install section at the bottom of this README.

Prerequisites

If you prefer not to use Docker, see the Native System Install section.

Installing Docker and Docker Compose

Official Docker documentation: Instructions

Before running this project, you'll need to install Docker and Docker Compose on your system.

  1. Run the following command to uninstall all conflicting packages.
sudo apt-get remove docker docker-engine docker.io containerd runc
  1. Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
  1. Add the repository to Apt sources
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
  1. Install Docker Engine, containerd, and Docker Compose
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  1. Add your user to the docker group to run Docker without sudo
sudo usermod -aG docker $USER
  1. Log out and log back in for this to take effect
exit
  1. Verify Docker Compose installation:
docker compose version

Verify Docker Installation

To verify that Docker is installed correctly, run:

docker --version
docker compose --version

Cloning the Repo

git clone https://github.com/notlimahrelyt/simple-nginx-test.git
cd simple-nginx-test

Generating Self Signed SSL Certificates

Make sure you're in the project directory before generating certificates:

1. Create a Self-Signed Certificate

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout ./key.pem \
  -out ./cert.pem

When prompted, enter appropriate information for your self-signed certificate. For the "Common Name" field, it's best to use your server's IP address or domain name that you'll be using to access the site, rather than "localhost".

Using the Containers

1. Start the Web Server

docker compose up -d

Note: Both docker-compose and docker compose commands may work depending on your Docker version. The newer docker compose syntax (with a space) is recommended.

This command will:

  • Pull the NGINX Docker image if it's not already available locally
  • Create and start the container with the configuration specified in the docker-compose.yml file
  • Run the container in detached mode (-d flag) so it runs in the background

2. Access the Website

  • HTTP (redirects to HTTPS): http://SERVER_IP
  • HTTPS: https://SERVER_IP

Replace SERVER_IP with the actual IP address of your Ubuntu Server.

For example:

Note: Since you're using a self-signed certificate, your browser will show a security warning. This is expected and can be bypassed for local testing. In most browsers, you can proceed by clicking "Advanced" and then "Proceed to site (unsafe)".

3. Stop the Web Server

docker compose down

Project Structure

  • docker-compose.yml: Defines the NGINX service configuration
  • nginx.conf: NGINX server configuration with SSL settings
  • index.html: Simple web page served by NGINX
  • key.pem & cert.pem: SSL certificate files (generated by you)

Troubleshooting

  • If ports 80 or 443 are already in use, modify the port mappings in the docker-compose.yml file.
  • Ensure that Docker is running before executing Docker commands.
  • If you encounter permission issues creating the certificates, try running the commands without sudo.
  • If you see "Permission denied" errors when running Docker commands, make sure you've properly added your user to the docker group and have logged out and back in.

Native System Install

If you prefer not to use Docker, you can install NGINX directly on your Ubuntu Server. This approach may be simpler for users who are not familiar with containers:

  1. Install NGINX:

    sudo apt update
    sudo apt install -y nginx
  2. Generate a Self-Signed Certificate:

    sudo mkdir -p /etc/nginx/ssl
    
    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
      -keyout /etc/nginx/ssl/key.pem \
      -out /etc/nginx/ssl/cert.pem

    When prompted for the "Common Name", enter your server's IP address or domain name, not "localhost".

  3. Configure NGINX: Create a new site configuration:

    sudo nano /etc/nginx/sites-available/example

    Paste the following configuration:

   server { 
       listen                  443 ssl;
       listen                  [::]:443 ssl;
       server_name             YOUR_SERVER_IP;   # Replace with your server's IP or domain
       ssl_certificate         /etc/nginx/ssl/cert.pem;
       ssl_certificate_key     /etc/nginx/ssl/key.pem;

       root /var/www/html;
       index index.html;

       location / {
         try_files $uri $uri/ =404;
       }
   }
   server {
       listen 80;
       listen [::]:80;

       server_name             YOUR_SERVER_IP;   # Replace with your server's IP or domain

       return 302 https://$server_name$request_uri;
   }

Note: Be sure to replace YOUR_SERVER_IP with your actual server IP address or domain name in both server blocks.

  1. Create a Symbolic Link to Enable the Site:

    sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/
  2. Copy the Website Content:

    # Copy index.html to the web root
    sudo cp /path/to/simple-nginx-test/index.html /var/www/html/

    Note: Replace /path/to/simple-nginx-test with the actual path to where you cloned the repository.

  3. Test NGINX Configuration:

    sudo nginx -t
  4. Restart NGINX:

    sudo systemctl restart nginx
  5. Access the Website:

    • HTTP (redirects to HTTPS): http://SERVER_IP
    • HTTPS: https://SERVER_IP

    Replace SERVER_IP with the actual IP address of your Ubuntu Server (e.g., 192.168.1.100 or ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com).

  6. Control NGINX Service:

    # Stop NGINX
    sudo systemctl stop nginx
    
    # Start NGINX
    sudo systemctl start nginx
    
    # Check status
    sudo systemctl status nginx

Troubleshooting Native Installation (Ubuntu Server)

  • Port conflicts: If ports 80 or 443 are already in use, you may need to change them in the NGINX configuration.
  • Permission issues: Make sure you have proper permissions to write to the configuration directories.
  • Certificate errors: Ensure the paths to your SSL certificates are correct in the NGINX configuration.
  • Service doesn't start: Check the NGINX error logs for more information:
    sudo journalctl -u nginx.service
    # or
    sudo cat /var/log/nginx/error.log
  • Default site conflict: If the default NGINX site is interfering, disable it:
    sudo rm /etc/nginx/sites-enabled/default
    sudo systemctl reload nginx

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages