Installing and Configuring Nginx
Now that you understand what Nginx is and why it's a powerful web server, let's get it running on your system. In this lesson, you'll install Nginx on different operating systems, verify the installation, and learn basic service management commands.
By the end of this lesson, you'll be able to:
- Install Nginx on Ubuntu/Debian, CentOS/RHEL, and macOS
- Start, stop, and reload Nginx services
- Verify Nginx is running correctly
- Access the default welcome page
Installation Methods
Nginx can be installed using package managers on most operating systems. Let's cover the most common platforms.
Installing on Ubuntu/Debian
# Update package list
sudo apt update
# Install Nginx
sudo apt install nginx -y
# Check installation version
nginx -v
On fresh Ubuntu systems, you might need to enable and start the service manually:
sudo systemctl enable nginx
sudo systemctl start nginx
Installing on CentOS/RHEL
- CentOS 7
- CentOS 8/RHEL
# Add EPEL repository
sudo yum install epel-release -y
# Install Nginx
sudo yum install nginx -y
# Start and enable service
sudo systemctl start nginx
sudo systemctl enable nginx
# Install Nginx
sudo dnf install nginx -y
# Start and enable service
sudo systemctl start nginx
sudo systemctl enable nginx
Installing on macOS
# Install Nginx using Homebrew
brew install nginx
# Start Nginx service
brew services start nginx
Verifying Your Installation
After installation, verify that Nginx is running correctly.
Check Service Status
# Check if Nginx is running
sudo systemctl status nginx
# Alternative method using process check
ps aux | grep nginx
Test the Default Page
Open your web browser and navigate to http://localhost or http://127.0.0.1. You should see the Nginx welcome page:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
You can also test using the command line:
# Test with curl
curl -I http://localhost
# Expected response headers:
# HTTP/1.1 200 OK
# Server: nginx/1.18.0
# Content-Type: text/html
Basic Service Management
Learn how to control the Nginx service on your system.
Starting and Stopping
# Start Nginx
sudo systemctl start nginx
# Stop Nginx
sudo systemctl stop nginx
# Restart Nginx (downtime)
sudo systemctl restart nginx
# Reload Nginx (no downtime, new config)
sudo systemctl reload nginx
Use reload instead of restart when applying configuration changes to avoid service interruption for active connections.
Enable on Boot
# Enable Nginx to start on system boot
sudo systemctl enable nginx
# Disable auto-start
sudo systemctl disable nginx
Configuration File Locations
Understanding where Nginx stores its files is crucial for proper management.
Main Configuration Files
# Main configuration file
/etc/nginx/nginx.conf
# Site configurations (Ubuntu/Debian)
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/
# Default web root
/var/www/html/
# Log files
/var/log/nginx/access.log
/var/log/nginx/error.log
Quick Configuration Test
Before applying any configuration changes, always test your syntax:
sudo nginx -t
# Expected output if syntax is correct:
# nginx: configuration file /etc/nginx/nginx.conf test is successful
Common Pitfalls
- Port conflicts: Apache or other services might be using port 80. Stop conflicting services or configure Nginx to use a different port
- Firewall blocking: Ensure your firewall allows HTTP (port 80) and HTTPS (port 443) traffic
- Permission issues: Nginx needs read access to web files and write access to log directories
- SELinux restrictions: On RHEL/CentOS, SELinux might block Nginx operations; check logs and adjust policies if needed
- IPv6 configuration: Some systems require explicit IPv6 configuration in Nginx for
localhostaccess
Summary
You've successfully installed Nginx on your system and learned essential service management commands. You can now start, stop, reload the service, verify it's running correctly, and test configuration syntax. These fundamentals prepare you for the next lesson where we'll dive into Nginx's configuration structure.
Managing and Reloading Nginx Configuration
What command should you use to apply Nginx configuration changes without service interruption?