0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
JPPINTO
  • Home
  • Blog
  • Certifications
  • About
  • Contact
  • Shop
  • Gallery
  • Current Setup
Contact

Search

July 3, 2026 / Linux, Servers, Ubuntu

Install and Tune Nginx on Ubuntu for Web Hosting

Tags: linux, nginx, server setup, ubuntu, web server
Featured image for Install and Tune Nginx on Ubuntu for Web Hosting

Nginx is a fast web server and reverse proxy that is commonly used for WordPress, PHP apps, static sites, APIs, and load-balanced web services.

This guide shows a clean Nginx install flow for Ubuntu, including service enablement, basic tuning, configuration backup, validation, and restart.

The examples use generic paths and values. Adjust them for your own server.

What the Script Does

The deployment script this article is based on does four main things:

  • Installs Nginx with apt.
  • Enables the nginx service.
  • Backs up /etc/nginx/nginx.conf.
  • Updates common settings such as worker_connections, keepalive_timeout, and client_max_body_size.
  • Runs nginx -t before restarting the service.

Install Nginx

Run the install commands as root or with sudo:

sudo apt update
sudo apt install -y nginx
sudo systemctl enable nginx

Start or restart Nginx:

sudo systemctl restart nginx
sudo systemctl is-active --quiet nginx

If the last command returns no output, the service is active. You can also check status with:

sudo systemctl status nginx

Back Up the Nginx Config

Before editing Nginx, save a timestamped backup:

sudo mkdir -p /opt/server-backups/nginx
timestamp="$(date +%Y%m%d%H%M%S)"
sudo cp /etc/nginx/nginx.conf "/opt/server-backups/nginx/nginx.conf.before.${timestamp}.bak"

Backups are useful because a bad Nginx edit can stop the web server from reloading.

Tune Common Nginx Settings

This example sets:

  • worker_connections to 4096.
  • keepalive_timeout to 65.
  • client_max_body_size to 64M.

You can apply those settings with:

sudo sed -i "s/worker_connections .*/worker_connections 4096;/" /etc/nginx/nginx.conf

if sudo grep -q "keepalive_timeout" /etc/nginx/nginx.conf; then
    sudo sed -i "s/keepalive_timeout .*/keepalive_timeout 65;/" /etc/nginx/nginx.conf
else
    sudo sed -i "/http {/a \    keepalive_timeout 65;" /etc/nginx/nginx.conf
fi

if sudo grep -q "client_max_body_size" /etc/nginx/nginx.conf; then
    sudo sed -i "s/client_max_body_size .*/client_max_body_size 64M;/" /etc/nginx/nginx.conf
else
    sudo sed -i "/http {/a \    client_max_body_size 64M;" /etc/nginx/nginx.conf
fi

client_max_body_size is especially important for sites that accept uploads. If it is too small, uploads may fail with 413 Request Entity Too Large.

Validate Before Restarting

Always test the config before restarting:

sudo nginx -t

If the syntax test succeeds, restart Nginx:

sudo systemctl restart nginx
sudo systemctl is-active --quiet nginx

Then save an after-change backup:

sudo cp /etc/nginx/nginx.conf "/opt/server-backups/nginx/nginx.conf.after.${timestamp}.bak"

Full Script

Here is the full Bash script used for this Nginx install and tuning workflow:

#!/usr/bin/env bash

__show_script_usage() {
  cat <<'__SCRIPT_USAGE__'
# Install-Nginx.sh

Installs and configures Nginx for the Linux hosting stack.

## Example Usage

```bash
cd /opt/DevOps/Scripts
sudo bash ./Install-Nginx.sh
```

## Notes

This changes `/etc/nginx/nginx.conf`, validates Nginx, writes backups under `/opt/DevOps/Backups/Nginx`, and restarts Nginx.
__SCRIPT_USAGE__
}

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
  case "${1:-}" in
    -h|--help|--usage)
      __show_script_usage
      exit 0
      ;;
  esac
fi

# ============================================================
# Variables
# ============================================================

DEVOPS_BACKUP_DIR="/opt/DevOps/Backups"

NGINX_WORKER_CONNECTIONS="4096"
NGINX_KEEPALIVE_TIMEOUT="65"
NGINX_CLIENT_MAX_BODY_SIZE="64M"

# ============================================================
# Safety
# ============================================================

set -euo pipefail

echo "Starting Nginx install and configuration..."

if [ "$EUID" -ne 0 ]; then
    echo "ERROR: Please run this script with sudo."
    exit 1
fi

# ============================================================
# Install Nginx
# ============================================================

echo "Updating package lists..."
apt update

echo "Installing Nginx..."
apt install -y nginx

echo "Enabling Nginx..."
systemctl enable nginx

# ============================================================
# Backup Before Config
# ============================================================

echo "Creating Nginx backup directory..."
mkdir -p "${DEVOPS_BACKUP_DIR}/Nginx"

TIMESTAMP="$(date +%Y%m%d%H%M%S)"

echo "Backing up Nginx config before changes..."
cp /etc/nginx/nginx.conf "${DEVOPS_BACKUP_DIR}/Nginx/nginx.conf.before.${TIMESTAMP}.bak"

# ============================================================
# Configure Nginx
# ============================================================

echo "Applying Nginx settings..."

sed -i "s/worker_connections .*/worker_connections ${NGINX_WORKER_CONNECTIONS};/" /etc/nginx/nginx.conf

if grep -q "keepalive_timeout" /etc/nginx/nginx.conf; then
    sed -i "s/keepalive_timeout .*/keepalive_timeout ${NGINX_KEEPALIVE_TIMEOUT};/" /etc/nginx/nginx.conf
else
    sed -i "/http {/a \    keepalive_timeout ${NGINX_KEEPALIVE_TIMEOUT};" /etc/nginx/nginx.conf
fi

if grep -q "client_max_body_size" /etc/nginx/nginx.conf; then
    sed -i "s/client_max_body_size .*/client_max_body_size ${NGINX_CLIENT_MAX_BODY_SIZE};/" /etc/nginx/nginx.conf
else
    sed -i "/http {/a \    client_max_body_size ${NGINX_CLIENT_MAX_BODY_SIZE};" /etc/nginx/nginx.conf
fi

# ============================================================
# Validate Nginx
# ============================================================

echo "Testing Nginx configuration..."
nginx -t

# ============================================================
# Backup After Config
# ============================================================

echo "Backing up Nginx config after changes..."
cp /etc/nginx/nginx.conf "${DEVOPS_BACKUP_DIR}/Nginx/nginx.conf.after.${TIMESTAMP}.bak"

# ============================================================
# Start Nginx
# ============================================================

echo "Starting Nginx..."
systemctl restart nginx

echo "Checking Nginx status..."
systemctl is-active --quiet nginx

echo "Nginx install and configuration complete."

Troubleshooting

If Nginx fails to start, check the config test:

sudo nginx -t

Check the service logs:

sudo journalctl -u nginx -xe

Restore the backup if needed:

sudo cp /opt/server-backups/nginx/nginx.conf.before.YYYYMMDDHHMMSS.bak /etc/nginx/nginx.conf
sudo nginx -t
sudo systemctl restart nginx

Quick Reference

sudo apt update
sudo apt install -y nginx
sudo systemctl enable nginx
sudo nginx -t
sudo systemctl restart nginx

Nginx changes should always follow the same rhythm: back up, edit, validate, restart, and verify.

Post Views: 91
<- Fix Windows Line Endings and Make Shell Scripts Executable in Linux
Install PHP-FPM and Common PHP Extensions on Ubuntu ->

Categories

  • Active Directory (5)
  • AI (3)
  • Amazon Cloud Services (1)
  • AWS (2)
  • Blazor (1)
  • C# (C-Sharp) (3)
  • CI/CD Pipelines (1)
  • Cloud (1)
  • Cloudflare (2)
  • Codex (1)
  • Containers (4)
  • Deployment (2)
  • Development (5)
  • DNS (1)
  • Docker (3)
  • Email (1)
  • Family (1)
  • General (5)
  • IIS 6.0 (4)
  • IIS 7.0 (10)
  • IIS 8.0 (1)
  • Infrastructure as Code (IaC) (1)
  • Kubernetes (3)
  • Linux (9)
  • Microsoft 365 (2)
  • MySQL (1)
  • Office 2010 (1)
  • PHP (1)
  • PowerShell (11)
  • Productivity (1)
  • Security (1)
  • Servers (9)
  • SharePoint 2007 (8)
  • SharePoint 2010 (19)
  • SharePoint 2013 (2)
  • SharePoint Online (1)
  • SMTP (4)
  • SQL Server 2008 (1)
  • SQL Server 2008 R2 (1)
  • SQL Server 2012 (2)
  • SQL Server 2019 (1)
  • SSL (1)
  • Travel (1)
  • Troubleshooting (1)
  • Ubuntu (9)
  • Uncategorized (1)
  • URL Rewrite (2)
  • Visual Studio 2019 (1)
  • Visual Studio Code (1)
  • Windows 10 (7)
  • Windows 2003 (9)
  • Windows 2008 (18)
  • Windows 2012 (6)
  • Windows 7 (3)
  • Windows Firewall (1)
  • Windows Vista (1)
  • WordPress (3)
  • WP-CLI (3)

Recent Posts

  • GPT-5.6 Sol Changes How We Prompt—and How We Write AGENTS.md
  • Protect a Domain That Does Not Send Email with SPF, DMARC, and Null MX
  • Turning Our Atlanta Vacation Photos into a Video with PowerShell
  • Bulk Create Cloudflare Origin CA Certificates with PowerShell
  • Test a Cloudflare Global API Key Connection with PowerShell

Advertisement

Tags

agents.md ai coding agents aws bash cloudflare cloud storage codex context engineering developer workflow dev to production dns externalize blob externalize sharepoint data full installation http redirect https IIS IIS installation index server configuration installing cumulative updates linux load balance central administration microsoft 365 nginx powerpoint powershell redirect http to https s3 server setup sharepoint 2010 cumulative updates sharepoint 2010 farm build sharepoint 2010 farm configuration sharepoint 2010 farm installation sharepoint data externalization SMTP ssl storagepoint ubuntu web server windows Windows 7 windows server 2008 wordpress wp-cli x86
© 2026 JPPinto.com. All rights reserved.