How can we help?
Categories
< All Topics
Print

Setting Up a Node.js Application Using Docker and Nginx on ServerStadium

This tutorial guides you through creating a secure and scalable environment for a Node.js application using Docker and Nginx on ServerStadium’s infrastructure.

Prerequisites

Step 1: Prepare Your ServerStadium Environment

  1. Select and Set Up Your Server: Choose a VM or dedicated server from ServerStadium. A medium-sized VM should suffice for most Node.js applications.
  2. Server Setup: Access your server via SSH. Update the system:

    sudo apt update
    sudo apt upgrade

Step 2: Install Docker

  1. Install Docker: Install Docker on your ServerStadium environment:

    sudo apt install docker.io

  2. Start and Enable Docker:

    sudo systemctl start docker
    sudo systemctl enable docker

  3. Verify Installation:

    docker –version

Step 3: Create Your Node.js Application

  1. Application Setup: On your local machine, create a basic Node.js application. For example, an Express.js application.
  2. Dockerize the Application: Create a Dockerfile in your application directory:

    FROM node:14

    WORKDIR /usr/src/app

    COPY package*.json ./

    RUN npm install

    COPY . .

    EXPOSE 8080

    CMD [ "node", "app.js" ]

  3. Build Your Docker Image:

    docker build -t mynodeapp .

  4. Run the Docker Container:

    docker run -d -p 8080:8080 mynodeapp

Step 4: Install and Configure Nginx as a Reverse Proxy

  1. Install Nginx:

    sudo apt install nginx

  2. Configure Nginx:

    Create a new site configuration file:

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


    Add the following configuration:

    server {
    listen 80;

    server_name your_domain.com;

    location / {
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
    }


    Enable the new site:

    sudo ln -s /etc/nginx/sites-available/mynodeapp /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx

  3. Firewall Configuration: If you have a firewall enabled, allow Nginx traffic:

    sudo ufw allow <span class="hljs-string">'Nginx Full'</span>

Step 5: Deploy Your Application

  1. Transfer Your Docker Image: Use docker save and docker load or a Docker registry to transfer your Docker image to your ServerStadium server.
  2. Run Your Docker Container: On the server, run your Docker container as before.

Step 6: Secure Your Application

  1. SSL Certificate: Obtain an SSL certificate for your domain (you can use Let’s Encrypt for a free certificate) and configure Nginx to use it.
  2. Environment Variables: Use environment variables for sensitive information like database passwords.

Conclusion

You now have a scalable and secure Node.js application running in a Docker container, with Nginx as a reverse proxy on a ServerStadium server. This setup ensures efficient load handling and enhanced security for your application.

For additional assistance, visit our knowledge base or contact our support team.

Table of Contents