A Comprehensive Step-by-Step Guide

Introduction

NodeBB is a modern, open-source forum software built on Node.js. It is designed to be fast, scalable, and easy to use. MongoDB is a NoSQL database that works seamlessly with NodeBB, while Nginx acts as a reverse proxy to handle incoming requests efficiently.

In this guide, we will walk you through the process of installing NodeBB with MongoDB and Nginx Proxy on AlmaLinux 9 using Docker and Ansible. By the end of this article, you will have a fully functional NodeBB forum running in a Dockerized environment, managed by Ansible.

Prerequisites

Before we begin, ensure that you have the following prerequisites:

Step 1: Set Up Docker and Docker Compose

First, ensure that Docker and Docker Compose are installed and running on your AlmaLinux 9 server.

Install Docker

sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker

Install Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Step 2: Create a Docker Compose File for NodeBB and MongoDB

Create a directory for your NodeBB setup and navigate into it:

mkdir nodebb-setup
cd nodebb-setup

Create a docker-compose.yml file:

version: '3.8'

services:
  mongo:
    image: mongo:5.0
    container_name: mongo
    restart: always
    volumes:
      - mongo_data:/data/db
    networks:
      - nodebb_network

  nodebb:
    image: nodebb/nodebb:latest
    container_name: nodebb
    restart: always
    ports:
      - "4567:4567"
    links:
      - mongo
    environment:
      - DATABASE=mongo
      - MONGO_HOST=mongo
      - MONGO_PORT=27017
      - MONGO_USER=nodebb
      - MONGO_PASSWORD=nodebbpassword
      - MONGO_DATABASE=nodebb
    volumes:
      - nodebb_data:/var/lib/nodebb
    networks:
      - nodebb_network

  nginx:
    image: nginx:latest
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    networks:
      - nodebb_network

volumes:
  mongo_data:
  nodebb_data:

networks:
  nodebb_network:

This Docker Compose file defines three services: MongoDB, NodeBB, and Nginx. It also creates Docker volumes for persistent data storage.

Step 3: Configure Nginx as a Reverse Proxy

Create an nginx.conf file in the same directory:

events {}

http {
    server {
        listen 80;
        server_name yourdomain.com;

        location / {
            proxy_pass http://nodebb:4567;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

Replace yourdomain.com with your actual domain name. This configuration forwards incoming HTTP requests to the NodeBB container.

Step 4: Deploy with Docker Compose

Run the following command to start the containers:

docker-compose up -d

This command will pull the required Docker images and start the MongoDB, NodeBB, and Nginx containers in detached mode.

Step 5: Automate with Ansible

To automate the deployment process, create an Ansible playbook named deploy_nodebb.yml:

---
- hosts: all
  become: yes
  tasks:
    - name: Ensure Docker is installed
      dnf:
        name: docker-ce
        state: present

    - name: Ensure Docker Compose is installed
      get_url:
        url: https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)
        dest: /usr/local/bin/docker-compose
        mode: '0755'

    - name: Start and enable Docker service
      service:
        name: docker
        state: started
        enabled: yes

    - name: Create NodeBB setup directory
      file:
        path: /opt/nodebb-setup
        state: directory

    - name: Copy Docker Compose file
      copy:
        src: docker-compose.yml
        dest: /opt/nodebb-setup/docker-compose.yml

    - name: Copy Nginx configuration
      copy:
        src: nginx.conf
        dest: /opt/nodebb-setup/nginx.conf

    - name: Deploy NodeBB with Docker Compose
      command: docker-compose up -d
      args:
        chdir: /opt/nodebb-setup

Run the playbook using the following command:

ansible-playbook -i your_inventory_file deploy_nodebb.yml

Replace your_inventory_file with the path to your Ansible inventory file.

Step 6: Access NodeBB

Once the deployment is complete, open your web browser and navigate to http://yourdomain.com. You should see the NodeBB setup wizard. Follow the on-screen instructions to complete the installation.

Conclusion

In this guide, we walked you through the process of installing NodeBB with MongoDB and Nginx Proxy on AlmaLinux 9 using Docker and Ansible. By following these steps, you can easily set up a scalable and efficient forum platform.

Docker and Ansible make it easy to manage and automate your deployments, ensuring a smooth and consistent setup process. Whether you’re running a small community forum or a large-scale discussion platform, this setup provides a robust foundation for your needs.