How to Manage Nginx Service for Frappe and ERPNext
Nginx Service ManagementLearn how to set up, stop, start, and reload the Nginx service for your Frappe or ERPNext application using Bench CLI commands and locate the Nginx configuration file.
Managing the Nginx web server is a critical task for any Frappe or ERPNext administrator. Nginx acts as a reverse proxy, serving web pages, handling SSL, and routing requests to the Frappe application. The Bench CLI provides convenient commands to simplify this process.
This guide covers the essential commands for setting up and controlling the Nginx service and shows you where to find the configuration file for advanced customizations.
bench setup nginx
sudo service nginx stop
sudo service nginx reload
sudo service nginx start
# Location of the config file:
frappe-bench/conf/nginx.confUnderstanding This Code
What It Does
This set of commands allows a system administrator to initialize the Nginx configuration for a Frappe bench, and then control the Nginx service (stop, reload, start). It also points to the location of the generated Nginx configuration file.
When To Use
Use these commands during the initial setup of a Frappe/ERPNext instance, when deploying changes that require a server restart or configuration reload, or when troubleshooting server-related issues like 502 Bad Gateway errors.
Prerequisites
- •A working Frappe bench installation
- •Sudo/root access on the server
- •Nginx installed on the server (usually handled by `bench setup production`)
Key Concepts
Important ideas to understand in this code
Bench CLI
The Bench Command Line Interface (CLI) is the primary tool for managing Frappe Framework sites. It automates tasks like installation, updates, site creation, and server process management, including Nginx, Supervisor, and Redis.
Learn moreNginx as a Reverse Proxy
In a Frappe setup, Nginx doesn't run the Python application directly. It acts as a reverse proxy, receiving HTTP/S requests from users and forwarding them to the Frappe application server (Gunicorn). It also efficiently serves static assets like CSS, JS, and images.
Learn morenginx.conf
The `frappe-bench/conf/nginx.conf` file is automatically generated by `bench setup nginx`. It contains server blocks and location directives tailored to your Frappe site, including SSL settings, upstream definitions, and static file locations.
Learn moreStep-by-Step Tutorial
Follow along to understand how this code works
Generate or Update Nginx Configuration
This is the first command you should run. It creates or updates the `nginx.conf` file inside your bench's `conf` directory based on the sites you have created. It will also attempt to create a symbolic link in the system's Nginx configuration directory (e.g., `/etc/nginx/sites-enabled/`).
bench setup nginxApply Configuration Changes
After modifying the configuration (or after running `bench setup nginx`), you need to tell Nginx to reload its settings without dropping connections. The `reload` command is the graceful way to do this.
sudo service nginx reloadRestarting the Nginx Service
If a simple reload isn't enough or if you need to perform maintenance, you can stop and start the service. This is a 'hard' restart and will briefly drop active connections. This is less common than `reload`.
sudo service nginx stop
sudo service nginx startLocate and Inspect the Configuration File
To make advanced changes, such as custom redirects, security headers, or SSL certificate paths, you need to edit the configuration file directly. It's located in the `conf` directory within your bench folder. After editing, remember to run `sudo service nginx reload`.
# Navigate to your bench directory first, e.g., cd ~/frappe-bench
cat conf/nginx.confCommon Issues & Solutions
Troubleshoot problems you might encounter