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"
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.