When managing a server that hosts multiple websites, setting up a robust and secure Nginx server is a crucial step. Not only does Nginx offer high performance and scalability, but combining it with Let's Encrypt provides free SSL certificates to ensure your websites are secure. In this guide, we’ll walk through setting up an Nginx server that can handle multiple domains and secure them using Let's Encrypt.
Why Choose Nginx for Multiple Domains?
Nginx is a powerful web server known for its speed, efficiency, and ability to handle many concurrent connections. When it comes to hosting multiple domains, Nginx's server block
configuration (similar to Apache's virtual hosts
) makes it incredibly flexible and straightforward.
Key Benefits of Nginx:
- High Performance: Efficiently handles static and dynamic content.
- Scalability: Supports thousands of simultaneous connections.
- Security: Pairs well with tools like Let's Encrypt to provide HTTPS.
- Flexibility: Easily manages multiple domains on a single server.
Prerequisites
Before we start, ensure you have:
- A Linux server (e.g., Ubuntu 22.04)
- Root access to the server
- A domain name pointing to your server's IP address
- Nginx installed (
sudo apt install nginx
) - Certbot and the Nginx plugin installed (
sudo apt install certbot python3-certbot-nginx
)
Step-by-Step Guide
1. Setting Up Nginx Server Blocks
sudo mkdir -p /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
sudo nano /etc/nginx/sites-available/example.com
Add the following configuration:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
Activate the configuration by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
2. Setting Up Let's Encrypt SSL Certificates
sudo certbot --nginx -d example.com -d www.example.com
3. Automating SSL Certificate Renewal
sudo crontab -e
Add the following line:
0 12 * * * /usr/bin/certbot renew --quiet
Conclusion
Setting up an Nginx server for multiple domains and securing them with Let's Encrypt SSL certificates is not only feasible but also straightforward. With Nginx's flexibility and Let's Encrypt's free SSL offerings, you can ensure your websites are both scalable and secure.