DEV Community

Cover image for How to Deploy an Application Using Nginx as a Web Server
Aaditya Kediyal
Aaditya Kediyal

Posted on

How to Deploy an Application Using Nginx as a Web Server

Deploying an application using Nginx as a web server is a common task for developers and system administrators. Nginx is known for its performance, stability, rich feature set, simple configuration, and low resource consumption. This blog post will guide you through the process of deploying an application using Nginx, complete with relevant code snippets to make the process as clear as possible.

Table of Contents

  1. Introduction to Nginx
  2. Installing Nginx
  3. Setting Up Your Application
  4. Configuring Nginx
  5. Starting and Enabling Nginx
  6. Securing Your Deployment with SSL
  7. Monitoring and Troubleshooting
  8. Conclusion

1. Introduction to Nginx

Nginx is an open-source web server that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. Due to its event-driven architecture, it handles multiple client requests efficiently, making it suitable for high-traffic websites and applications.

Key Features of Nginx:

  • High concurrency
  • Load balancing
  • Reverse proxy capabilities
  • Static file serving
  • SSL/TLS support
  • HTTP/2 support

2. Installing Nginx

To install Nginx, you need to have root or sudo privileges on your server. The following steps demonstrate how to install Nginx on a typical Linux-based system such as Ubuntu.

Installation on Ubuntu:

First, update your package list:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Then, install Nginx:

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

Verify Installation:

After installation, you can verify that Nginx is installed correctly by checking its version:

nginx -v
Enter fullscreen mode Exit fullscreen mode

You can also start the Nginx service and check its status:

sudo systemctl start nginx
sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

3. Setting Up Your Application

For the purpose of this guide, we will deploy a simple web application. You can replace this with your actual application.

Example Application:

Let’s assume we have a simple Node.js application. Here’s a basic example:

app.js:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello, world!');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

Running the Application:

Make sure you have Node.js and npm installed. Then, you can run your application with:

node app.js
Enter fullscreen mode Exit fullscreen mode

Your application should now be accessible at http://localhost:3000.

4. Configuring Nginx

Nginx configuration files are typically located in the /etc/nginx directory. The main configuration file is nginx.conf, and additional site-specific configurations are often stored in the /etc/nginx/sites-available directory with symlinks in the /etc/nginx/sites-enabled directory.

Create a Configuration File:

Create a new configuration file for your application:

sudo nano /etc/nginx/sites-available/myapp
Enter fullscreen mode Exit fullscreen mode

Add the following configuration:

server {
    listen 80;
    server_name your_domain_or_IP;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Replace your_domain_or_IP with your actual domain name or IP address.

Enable the Configuration:

Create a symlink in the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Test Nginx Configuration:

Before restarting Nginx, test the configuration for syntax errors:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

Restart Nginx:

If the configuration test is successful, restart Nginx to apply the changes:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Your application should now be accessible through Nginx at http://your_domain_or_IP.

5. Starting and Enabling Nginx

To ensure Nginx starts on boot, enable the service:

sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

You can start, stop, and restart Nginx using the following commands:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

6. Securing Your Deployment with SSL

For production applications, securing your site with SSL is crucial. We will use Let’s Encrypt to obtain a free SSL certificate.

Install Certbot:

Certbot is a client for Let’s Encrypt that automates the process of obtaining and renewing SSL certificates.

On Ubuntu, you can install Certbot and the Nginx plugin with:

sudo apt install certbot python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Obtain an SSL Certificate:

Run Certbot to obtain a certificate and configure Nginx:

sudo certbot --nginx -d your_domain
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to complete the setup. Certbot will automatically edit your Nginx configuration to use the obtained SSL certificate.

Automatic Renewal:

Let’s Encrypt certificates are valid for 90 days, but Certbot can handle renewals automatically. To set up automatic renewal, add a cron job:

sudo crontab -e
Enter fullscreen mode Exit fullscreen mode

Add the following line to run the renewal twice daily:

0 0,12 * * * /usr/bin/certbot renew --quiet
Enter fullscreen mode Exit fullscreen mode

7. Monitoring and Troubleshooting

Checking Nginx Logs:

Nginx logs are helpful for troubleshooting issues. By default, they are located in /var/log/nginx.

  • Access logs: /var/log/nginx/access.log
  • Error logs: /var/log/nginx/error.log

Common Commands:

  • Reload Nginx after changes: sudo systemctl reload nginx
  • Check Nginx status: sudo systemctl status nginx
  • View Nginx logs: sudo tail -f /var/log/nginx/error.log

Troubleshooting Tips:

  1. 502 Bad Gateway: This usually indicates that Nginx cannot connect to your application. Ensure your application is running and check the port configuration.
  2. 403 Forbidden: This error occurs due to permission issues. Check the permissions of your web root and the Nginx configuration.
  3. 404 Not Found: Ensure your application routes are correctly defined and the server block in the Nginx configuration points to the correct location.

8. Conclusion

Deploying an application using Nginx as a web server involves several steps, from installing Nginx and setting up your application to configuring Nginx and securing your deployment with SSL. With the above guide and relevant code snippets, you should be able to deploy your application efficiently and securely.

Nginx is a powerful tool that, when used correctly, can greatly enhance the performance and security of your web application. Whether you are deploying a simple static site or a complex web application, Nginx provides the flexibility and scalability needed to handle your web traffic effectively.

Feel free to explore more advanced configurations and features of Nginx, such as load balancing, caching, and more, to fully leverage its capabilities for your specific use case. Happy deploying!

Top comments (1)

Collapse
 
mrzaizai2k profile image
Mai Chi Bao

Is there a way we can set NGINX for multiple env, like in local I don't want to setup https. And I want to wrap them all in docker-compose