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
nginxservice. - Backs up
/etc/nginx/nginx.conf. - Updates common settings such as
worker_connections,keepalive_timeout, andclient_max_body_size. - Runs
nginx -tbefore 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_connectionsto4096.keepalive_timeoutto65.client_max_body_sizeto64M.
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.