Installing and Configuring Nginx with SSL on RPM-Based and DEB-Based Linux Systems
Installing Nginx
RPM-Based Systems (e.g., CentOS, Fedora, RHEL)
- Install Nginx:
- Enable and Start Nginx:
Code:
sudo systemctl enable nginx sudo systemctl start nginx
DEB-Based Systems (e.g., Ubuntu, Debian)
- Install Nginx:
Code:
sudo apt update sudo apt install nginx
- Enable and Start Nginx:
Code:
sudo systemctl enable nginx sudo systemctl start nginx
Understanding /etc/nginx/nginx.conf
The file /etc/nginx/nginx.conf is the main configuration file for Nginx. It defines the global settings, such as user permissions, worker processes, and logging. It also includes directives for configuring server blocks, which determine how Nginx handles requests for different domains and paths.
HTML Directory Path
The default directory where Nginx serves HTML files is typically:
- RPM-Based Systems: /usr/share/nginx/html
- DEB-Based Systems: /var/www/html
Naming the Initial Home Page
The initial home page should always be named index.html (or index.php if you're using PHP). While Linux doesn't care about filename extensions, web pages typically need to end in .html to be correctly interpreted by browsers.
Opening the Firewall
DEB-Based Systems (using ufw)
- Allow HTTP Traffic:
Code:
sudo ufw allow 80/tcp sudo ufw reload
RPM-Based Systems (using firewall-cmd)
- Allow HTTP Traffic:
Code:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --reload
Accessing Your Web Server
If you're running Nginx on your local computer with a GUI, you can browse to your web server using the localhost address (
http://localhost).
If you've opened your firewall on port 80, other computers on your local network (including Windows, Apple Mac, and your cell phone) can browse to your web server by typing in the IP address (e.g.,
http://10.0.1.12).
SSL and Port 443
Port 80 typically does not use SSL, meaning its pages are not encrypted. Port 443, on the other hand, is used for SSL/TLS and provides encrypted communication. When you visit a website using HTTPS, you'll see a little padlock icon in the browser near the URL, indicating that the connection is secure.
Installing SSL Modules for Nginx
RPM-Based Systems (e.g., CentOS, Fedora, RHEL)
- Install the SSL Module:
Code:
sudo yum install nginx-mod-http-ssl
- Restart Nginx:
Code:
sudo systemctl restart nginx
DEB-Based Systems (e.g., Ubuntu, Debian)
- Install the SSL Module:
Code:
sudo apt update sudo apt install nginx sudo apt install nginx-extras
- Restart Nginx:
Code:
sudo systemctl restart nginx
Creating Your Own Self-Signed SSL Certificate
- Generate the Private Key:
Code:
openssl genpkey -algorithm RSA -out /etc/ssl/private/nginx-selfsigned.key
- Generate the Certificate Signing Request (CSR):
Code:
openssl req -new -key /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.csr
- Generate the Self-Signed Certificate:
Code:
openssl x509 -req -days 365 -in /etc/ssl/certs/nginx-selfsigned.csr -signkey /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
Configuring Nginx to Use the Self-Signed Certificate
- Edit the Nginx Configuration:
Code:
server { listen 443 ssl; server_name your_domain.com;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
location / {
root /var/www/html;
index index.html index.htm;
}
}
- Restart Nginx:
Code:
sudo systemctl restart nginx
Using ufw (Uncomplicated Firewall)
- Allow HTTPS traffic:
- Enable the firewall (if not already enabled):
- Check the status to confirm the rule is added:
Using firewall-cmd (FirewallD)
- Add the rule to allow HTTPS traffic:
Code:
sudo firewall-cmd --permanent --add-port=443/tcp
- Reload the firewall to apply the changes:
Code:
sudo firewall-cmd --reload
- Verify the rule is added:
Code:
sudo firewall-cmd --list-ports
Difference Between Self-Signed and CA Authority Signed Certificates
- Self-Signed Certificates:
- Generated by the user.
- Not trusted by default by browsers and operating systems.
- Suitable for internal testing or development environments.
- No cost involved.
- CA Authority Signed Certificates:
- Issued by a trusted Certificate Authority (CA).
- Trusted by default by browsers and operating systems.
- Suitable for production environments where trust and security are critical.
- Involves a cost for the certificate.
Testing SSL in Your Browser
- Access Your Web Server Using HTTPS: Open your web browser and navigate to https://your_domain.com or https://your_server_ip. If everything is set up correctly, you should see your website served over HTTPS.
- Check for the Padlock Icon: Look for the padlock icon in the browser's address bar. This indicates that the connection is secure and encrypted using SSL/TLS.
I hope this guide helps!