Nginx (Ubuntu 22.04)

Nginx (Ubuntu 22.04)

  • OS: ubuntu 22

Description

Nginx is a web server that is used for processing web requests and delivering web content to users. It is extremely fast and highly scalable, capable of serving large amounts of requests and being scaled across multiple servers. Nginx is open source and free software that can be installed on various operating systems and used for managing websites and applications.

Software included

Package Version
Nginx 1.23
Docker 3.20.10
Docker compose 2.12.2
containerd.io 1.6.10

Initial start of the service

This application uses Docker to provide faster startup of the underlying software (Nginx) and direct use of it. This eliminates the need to familiarize yourself with its specifics in terms of configuration and installation. To start working directly with the application, all you need to do is upload the content that you want to be served by Nginx to the /opt/nginx/data/ directory. Once you've done this, you can visit the IP address assigned to your virtual machine in your browser to view the content you've uploaded.

Note: By default, Nginx can be accessed directly by IP address. To start using Nginx with your real domain, you first need to point the domain in question to your IP address and change the settings for where Nginx opens from through its admin panel.

Quick settings and frequently asked questions

  • Logging into the cloud service is done using an SSH key or password that you have specified.
  • Docker is installed as recommended by the official docker documentation.
  • The standard docker and docker compose commands are now available in this application.
  • When the virtual machine is first started, it takes a few minutes before a Docker container containing Nginx is started.
  • Nginx data is located in /opt/nginx/data/.
  • By default, Nginx runs on all available interfaces on the machine. Nginx access ports are 80 and 443.

Adding a static site

This application image is internally organized in such a way that it loads the content uploaded to the /app directory. The uploaded content is loaded from the Nginx default vhost (catch-all) server block. That is, to upload a static site to be served by Nginx you just need to upload your content to the directory /opt/nginx/data/ which is predefined by us. It is linked to the internal docker directory /app. Of course, if you wish you can always change the directory you upload your content to. To do this, you need to perform the following steps:

Step 1 - create the directory to serve your site

mkdir /path/to/your/app

Step 2 - modify the docker-compose.yml file and restart docker

cd /opt/docker-nginx/
vi docker-compose.yml
  1. Edit docker-compose.yml which should look like this, changing /path/to/your/app to the path you want:
---

version: '2'

services:
  nginx:
    image: docker.io/bitnami/nginx:1.23
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - /path/to/your/app:/app

Adding custom Nginx vhost configurations

By default, the main nginx.conf file loads all configuration files placed in the internal /opt/bitnami/nginx/conf/server_blocks/ docker directory. If you create a new configuration file, for example my_server_block.conf, you can load it into this directory. To do this, follow the following steps, in which an example is given with the site www.example.com:

Step 1 - save your my_server_block.conf file with the following sample content

server {
  listen 0.0.0.0:8080;
  server_name www.example.com;
  root /app;
  index index.htm index.html;
}

Step 2 - enabling the new configuration file

  1. Open /opt/docker-nginx/docker-compose.yml and add your new configuration file as an additional docker volume, for example:
---
version: '2'

services:
  nginx:
    image: docker.io/bitnami/nginx:1.23
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - /path/to/your/app:/app
      - /opt/nginx-vhosts/my_server_block.conf:/opt/bitnami/nginx/conf/server_blocks/my_server_block.conf:ro

Please note that this configuration file contains standard Nginx configuration options. For more information, we advise you to familiarize yourself with the official documentation.

  1. Restart your docker containers:
cd /opt/docker-nginx/
docker compose restart

Use of own SSL certificates

Note: The steps below assume that you already have your own domain set up that points to the IP address assigned to your server. This means that you must have a virtual host(s) already configured, specifying a standard SSL configuration and the path to the certificates according to the официалната документация на Nginx.

By default, this application image has a dummy certificate and private keys uploaded to the docker internal /certs directory. If you wish to upload your own certificate (.crt) and its private key (.key), please follow the steps below:

Step 1 - uploading the certificates

Choose a directory to house the certificates and upload them there, for example:

mkdir -p /path/to/nginx-persistence/certs
cp /path/to/certfile.crt /path/to/nginx-persistence/certs/server.crt
cp /path/to/keyfile.key  /path/to/nginx-persistence/certs/server.key

Step 2 - add the necessary configuration in my_server_block.conf

server {
    listen       8443 ssl;

    ssl_certificate      bitnami/certs/server.crt;
    ssl_certificate_key  bitnami/certs/server.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

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

Step 3 - enabling the certificates in the configuration

  1. Open /opt/docker-nginx/docker-compose.yml and add your new configuration file as an additional docker volume, for example:
---
version: '2'

services:
  nginx:
    image: 'bitnami/nginx:1.23'
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - /path/to/nginx-persistence/certs:/certs
      - /path/to/your/app:/app
      - /opt/nginx-vhosts/my_server_block.conf:/opt/bitnami/nginx/conf/server_blocks/my_server_block.conf:ro
  1. Restart your docker containers:
cd /opt/docker-nginx/
docker compose restart

Changing the full Nginx configuration (nginx.conf)

In case you need to make a change to the main nginx.conf file, you always have the option of overwriting it with your own. To do this, follow the steps below:

Step 1 - upload your configuration file

You can upload it, for example, to /opt/nginx-conf/nginx.conf

Step 2 - enabling nginx.conf in the configuration

  1. Open /opt/docker-nginx/docker-compose.yml and add your new configuration file as an additional docker volume, for example:
---
version: '2'

services:
  nginx:
    image: 'bitnami/nginx:1.23'
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - /path/to/your/app:/app
      - /path/to/your_nginx.conf:/opt/bitnami/nginx/conf/nginx.conf:ro
  1. Restart your docker containers:
cd /opt/docker-nginx/
docker compose restart

Working with Docker

The organization of this application is entirely done using Docker. In this way, we can provide you with ready-made applications faster. In addition, their configuration is more flexible and quite controllable. To be able to control this application, however, you need to know some basic Docker functionalities and features.

Restarting all services

Restarting the database and the Docker container that contains Nginx itself, along with the rest of the software, can be done in two ways:

  1. By restarting the entire virtual machine
  2. Restarting the Docker containers, which is the faster option. To do this, access your machine via SSH, and run the following commands:
sudo su -
cd /opt/docker-nginx/
docker compose restart

Recreating Docker containers

Recreation of a docker container may be necessary if you've changed the configuration inside the docker-compose.yml file. If there are changes, please follow these steps:

cd /opt/docker-nginx/
docker compose stop
docker compose up -d

Checking the status of the Docker application

To check, you need to run the following commands:

sudo su -
docker ps -a

Please pay attention to the STATUS column. It should show how long the service has been running, for example Up X minutes. If the status is Restarting then you need to check what is wrong with the application. In such a case, you can check by following the application logs as follows:

  1. Get the application name that you can see in the NAMES column from the output of the docker ps -a command. View the logs with the docker logs <NAME> command, where is the name of the container.