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
- A ServerStadium VM or dedicated server. Review the options at ServerStadium VM Pricing and Dedicated Servers.
- Basic knowledge of Docker, Nginx, and Node.js.
Step 1: Prepare Your ServerStadium Environment
- 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.
- Server Setup: Access your server via SSH. Update the system:
sudo apt update
sudo apt upgrade
Step 2: Install Docker
- Install Docker: Install Docker on your ServerStadium environment:
sudo apt install docker.io
- Start and Enable Docker:
sudo systemctl start docker
sudo systemctl enable docker - Verify Installation:
docker –version
Step 3: Create Your Node.js Application
- Application Setup: On your local machine, create a basic Node.js application. For example, an Express.js application.
- 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" ]
- Build Your Docker Image:
docker build -t mynodeapp .
- Run the Docker Container:
docker run -d -p 8080:8080 mynodeapp
Step 4: Install and Configure Nginx as a Reverse Proxy
- Install Nginx:
sudo apt install nginx
- 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 - 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
- Transfer Your Docker Image: Use
docker save
anddocker load
or a Docker registry to transfer your Docker image to your ServerStadium server. - Run Your Docker Container: On the server, run your Docker container as before.
Step 6: Secure Your Application
- 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.
- 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.