Essential NGINX Commands on Ubuntu (Cheatsheet)

Updated: January 19, 2024 By: Guest Contributor Post a comment

Introduction

NGINX is a powerful, high-performance web server and reverse proxy that is popularly used to deploy and manage web applications. Though renowned for its efficiency and speed, navigating its commands can sometimes be daunting, particularly for beginners. This cheatsheet aims to provide you with a guide covering the essential NGINX commands you would use on an Ubuntu system, presented in a way that takes you from basics to more advanced operations.

Installation and Basics

Before diving into the commands, ensure that NGINX is installed on your system. The installation is straightforward:

sudo apt update
sudo apt install nginx

Once installed, you can perform primary operations:

# To start NGINX
sudo systemctl start nginx

# To stop NGINX
sudo systemctl stop nginx

# To restart NGINX (useful when you have changed configurations)
sudo systemctl restart nginx

# To reload NGINX (reloads configurations without dropping connections)
sudo systemctl reload nginx

# To enable NGINX to start on boot
sudo systemctl enable nginx

# To disable NGINX from starting on boot
sudo systemctl disable nginx

# To check the status of the NGINX service
sudo systemctl status nginx

Testing Configuration

After modifying NGINX configuration files, it’s imperative to test your configurations to ensure there are no syntax errors:

sudo nginx -t

If everything is fine, you’ll see a message indicating ‘syntax is okay’ and ‘test is successful.’ If there are issues, the output will provide details of what and where the error is.

Configuration Files

Understanding the structure of the NGINX configuration files:

# Main NGINX configuration file
sudo nano /etc/nginx/nginx.conf

# Default server block configuration
sudo nano /etc/nginx/sites-available/default

# To create a new server block
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/your_domain
sudo nano /etc/nginx/sites-available/your_domain

# To enable the new server block
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

# To view enabled sites
ls -l /etc/nginx/sites-enabled/

# To remove a server block
sudo rm /etc/nginx/sites-enabled/your_domain

Logging

Logs are incredibly helpful in monitoring NGINX:

# Access Log
sudo tail -f /var/log/nginx/access.log

# Error Log
sudo tail -f /var/log/nginx/error.log

You can use the ‘tail’ command with the ‘-f’ flag to follow the log in real-time.

Security Commands

It’s important to manage the security aspects of your NGINX server as well:

# Reload NGINX after a Let's Encrypt SSL certificate has been renewed
sudo systemctl reload nginx

You might need to secure your websites with SSL/TLS certificates, and after renewal, reloading the server will apply the new certificates.

Performance Tuning

For performance tuning, here are a few commands:

# Optimize number of worker processes
sudo nano /etc/nginx/nginx.conf
# and set the 'worker_processes' to the number of CPU cores

# Worker connections
# in the same file, you can set 'worker_connections' to the appropriate number depending
# on your expected traffic load

These settings help optimize resource usage and handle more connections effectively.

Advanced Techniques

For advanced users, NGINX can be configured to perform various complex tasks:

# Creating a reverse proxy
sudo nano /etc/nginx/sites-available/your_domain
# and configure the 'location' block accordingly

# Setting up load balancing
sudo nano /etc/nginx/nginx.conf
# and configure the 'upstream' and 'server' directives

# Using NGINX for caching
# edit the server block
sudo nano /etc/nginx/sites-available/your_domain
# and configure appropriate 'location' blocks with caching directives

Whether using NGXIN for reverse proxying, load balancing, caching or even for creating advanced redirection logic, these capabilities make NGINX a powerful tool for modern web architectures.

Managing Services and Process

Occasionally, you might need to gracefully shutdown or upgrade NGINX without disrupting the service:

# To quickly stop and start the service
sudo systemctl reload-or-restart nginx

# Graceful shutdown of NGINX
sudo nginx -s quit

# To forcefully terminate NGINX processes
sudo nginx -s stop

The ‘-s’ flag is used with different parameters to communicate different signals to the NGINX process.

Conclusion

Being versatile and robust, NGINX commands offer extensive functionality to manage web services effectively. This guide distilled the essential knowledge base required to handle everyday tasks associated with NGINX on an Ubuntu system. Mastering these commands can significantly enhance your ability to manage and optimize NGINX for your specific needs. With NGINX’s flexibility and your command-line prowess, your web services will run seamlessly and efficiently.