Skip to main content

Installing and Compiling Nginx

1. Installing from Package Managers

OS Command Notes
Ubuntu/Debian sudo apt update && sudo apt install nginx Installs latest stable version from official repos
CentOS/RHEL/Fedora sudo yum install nginx or sudo dnf install nginx Use dnf for Fedora 30+
macOS (Homebrew) brew install nginx Installs latest stable version

Example: Verify Installation

nginx -v

2. Installing from Official Repository

OS Repository URL Notes
Ubuntu 20.04+ http://nginx.org/packages/ubuntu/ Adds nginx.org repo for latest stable
CentOS/RHEL 7+ http://nginx.org/packages/rhel/ Adds nginx.org repo for latest stable

Example: Add Nginx Repo on Ubuntu

sudo apt install -y curl gnupg2 ca-certificates lsb-release
echo "deb http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
echo "deb http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

3. Compiling from Source

Step Description Notes
Download Source curl -O http://nginx.org/download/nginx-1.26.1.tar.gz Latest stable version as of 2026
Extract Archive tar -xzf nginx-1.26.1.tar.gz Unpacks source files
Configure Build ./configure --with-http_ssl_module Enables SSL support
Compile make Builds nginx binary
Install sudo make install Installs to /usr/local/nginx

Example: Configure with Custom Modules

./configure --with-http_ssl_module --with-stream_geoip_module

4. Adding Custom Modules

Module Type Description Installation Method
Third-party Module External module not included in default nginx build Compile with --add-module=path/to/module
Dynamic Module Module that can be loaded at runtime Use --add-dynamic-module=path/to/module

Example: Add OpenResty Module

./configure --add-module=/path/to/ngx_http_lua_module

5. Verifying Installation

Verification Method Command Description
Version Check nginx -v Prints nginx version
Config Test nginx -t Tests configuration syntax
Status Info nginx -T Prints full config including includes

Example: Test Configuration

nginx -t

6. Understanding Build Configuration

Configuration Option Description Default Value
--with-http_ssl_module Enables HTTPS support Disabled
--with-stream Enables stream module (TCP/UDP proxy) Disabled
--with-debug Enables debug logging Disabled
--prefix Installation directory /usr/local/nginx

Example: Full Build Configuration

./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-stream --with-debug

7. Installing Dynamic Modules

Module Type Description Build Flag
Dynamic Module Can be loaded at runtime via load_module directive --add-dynamic-module=path/to/module
Static Module Built into the nginx binary at compile time --add-module=path/to/module

Example: Enable Dynamic Module

./configure --add-dynamic-module=/path/to/ngx_http_lua_module

8. Setting Up Service Management

OS Service Name Command
Ubuntu/Debian nginx sudo systemctl start nginx
CentOS/RHEL nginx sudo systemctl start nginx
macOS (Homebrew) homebrew.mxcl.nginx brew services start nginx

Example: Start Nginx Service

sudo systemctl start nginx

9. Configuring Auto-start

OS Command Description
Ubuntu/Debian sudo systemctl enable nginx Starts nginx on boot
CentOS/RHEL sudo systemctl enable nginx Starts nginx on boot
macOS (Homebrew) brew services enable nginx Starts nginx on login

Example: Enable Auto-start on Boot

sudo systemctl enable nginx

10. Installing on Docker

Docker Command Description Notes
Pull Image docker pull nginx:latest Downloads latest stable Nginx image
Run Container docker run -d --name my-nginx -p 80:80 nginx Runs Nginx container with port mapping
Custom Config docker run -v /host/config:/etc/nginx/conf.d nginx Mounts custom config directory

Example: Run Nginx in Docker

docker run -d --name my-nginx -p 80:80 nginx

11. Installing on Alpine Linux

Package Manager Command Description
apk sudo apk add nginx Installs Nginx from Alpine package repository
apk sudo apk add nginx-mod-http-geoip2 Installs additional modules

Example: Install Nginx on Alpine

sudo apk add nginx

12. Upgrading Nginx Binary

Method Description Notes
Package Manager Use apt upgrade or yum update Updates to latest available version
Manual Compile Recompile from source with newer version Must re-enable modules if changed
Docker Replace image tag with newer version Ensure config is compatible

Example: Upgrade via Package Manager

sudo apt update && sudo apt upgrade nginx

Was this article helpful?