Nginx For Beginners
Install Config
Summary
In this module, you’ll learn how to install, configure, and secure Nginx on Linux using native package managers—no custom compilation required.
Why use package managers?
Package managers likeapt
,yum
, ordnf
handle dependencies, updates, and safe removal for you, so you’re not reinventing the wheel when deploying software such as Nginx.Note
Compiling from source can give you fine-grained control, but it increases maintenance overhead. For most production environments, the prebuilt packages are reliable and secure.
Installing Nginx on Linux
While Windows and macOS can be used for local testing, production deployments typically run on Linux distributions.- Ubuntu / Debian:
sudo apt update sudo apt install nginx
- CentOS / RHEL / Fedora:
sudo yum install nginx # CentOS/RHEL 7 sudo dnf install nginx # Fedora, CentOS/RHEL 8+
- Start and enable Nginx:
sudo systemctl start nginx sudo systemctl enable nginx
- Ubuntu / Debian:
Service Management with
systemctl
Use Nginx’s built-in controls to manage the daemon:sudo systemctl {start|stop|restart|reload} nginx sudo systemctl status nginx
Understanding nginx.conf Structure
The main configuration file is divided into four sections:- Global (user, worker_processes)
- events (worker_connections)
- http (MIME types, logging, gzip)
- server blocks (virtual hosts)
Serving Static Files & Virtual Hosts
Nginx excels at delivering static content with minimal overhead. You can host multiple sites on a single IP by defining separateserver
blocks. Always set a uniqueserver_name
to prevent Nginx from defaulting to the first file alphabetically.Customizing the Default Welcome Page
Replace the built-in Nginx page with a simple “Hello World” HTML:<!-- /var/www/html/index.html --> <!DOCTYPE html> <html> <head><title>Hello World</title></head> <body><h1>Hello, Nginx!</h1></body> </html>
Firewall Configuration
Ensure your firewall and any cloud security groups allow HTTP/HTTPS traffic only as needed:Distribution Firewall Tool Common Commands Ubuntu / Debian UFW sudo ufw allow 'Nginx Full'
<br>sudo ufw enable
CentOS / RHEL firewalld sudo firewall-cmd --permanent --add-service=http
<br>sudo firewall-cmd --reload
All (Cloud VPS) Security Group Configure ports 80/443 in AWS/GCP/Azure console or CLI Warning
Opening ports 80 and 443 without restrictions exposes your server to the public internet. Always verify your rule sets and consider limiting access by IP for staging environments.
Links and References
Watch Video
Watch video content
Practice Lab
Practice lab