Managing a web server for multiple domains can quickly become repetitive and time-consuming. Automating this process with bash scripts not only saves time but also reduces the risk of errors during setup. In this guide, we’ll break down two bash scripts: one for setting up Nginx server blocks and another for configuring Let's Encrypt SSL certificates.
Why Automate Nginx and SSL Setup?
Automation brings numerous benefits to server management:
- Speed: Quickly deploy new domains with minimal manual input.
- Consistency: Standardize server configurations.
- Error Reduction: Avoid misconfigurations by automating repetitive tasks.
- Scalability: Easily handle multiple domains on a single server.
Prerequisites
Before using the scripts, ensure:
- You are running a Linux server (e.g., Ubuntu 22.04).
- Nginx is installed (
sudo apt install nginx
). - You have root access to the server.
- A domain name points to your server's IP address.
Script 1: setup_nginx_server.sh
The first script automates the creation of directories, sample files, and Nginx server blocks for a new domain.
#!/bin/bash
set -e
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
echo "Enter the domain name (e.g., example.com):"
read DOMAIN
echo "Enter any additional domains (e.g., www.example.com) separated by space or leave blank:"
read ADDITIONAL_DOMAINS
WEB_ROOT="/var/www/$DOMAIN/html"
echo "Creating directory structure..."
mkdir -p "$WEB_ROOT"
chmod -R 755 "/var/www/$DOMAIN"
echo "Creating a sample index.html file..."
echo "<html><head><title>Welcome to $DOMAIN!</title></head><body><h1>Success! $DOMAIN is working!</h1></body></html>" > "$WEB_ROOT/index.html"
NGINX_CONF="/etc/nginx/sites-available/$DOMAIN"
echo "Creating Nginx server block..."
cat > $NGINX_CONF <<EOL
server {
listen 80;
server_name $DOMAIN $ADDITIONAL_DOMAINS;
root $WEB_ROOT;
index index.html index.htm index.nginx-debian.html;
location / {
try_files \$uri \$uri/ =404;
}
}
EOL
echo "Enabling site..."
ln -s $NGINX_CONF /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
echo "Nginx server block for $DOMAIN has been created successfully!"
How to Use
- Save the Script:
nano setup_nginx_server.sh
- Make it Executable:
chmod +x setup_nginx_server.sh
- Run the Script:
sudo ./setup_nginx_server.sh
Script 2: setup_lets_encrypt.sh
The second script installs Certbot (if not already installed), acquires SSL certificates from Let's Encrypt, and sets up automatic renewal.
#!/bin/bash
set -e
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
echo "Enter the domain name (e.g., example.com):"
read DOMAIN
echo "Enter any additional domains (e.g., www.example.com) separated by space or leave blank:"
read ADDITIONAL_DOMAINS
if ! [ -x "$(command -v certbot)" ]; then
echo "Certbot not found. Installing Certbot and Nginx plugin..."
apt update
apt install -y certbot python3-certbot-nginx
fi
certbot --nginx -d $DOMAIN $ADDITIONAL_DOMAINS --non-interactive --agree-tos -m admin@$DOMAIN
(crontab -l 2>/dev/null; echo "0 12 * * * /usr/bin/certbot renew --quiet") | crontab -
certbot renew --dry-run
echo "SSL certificate setup and renewal configuration completed for $DOMAIN!"
How to Use
- Save the Script:
nano setup_lets_encrypt.sh
- Make it Executable:
chmod +x setup_lets_encrypt.sh
- Run the Script:
sudo ./setup_lets_encrypt.sh
Conclusion
Automating Nginx server setup and SSL configuration with bash scripts makes server management more efficient and error-free. These scripts not only speed up the deployment of new domains but also ensure your sites are secure with HTTPS by default.
By splitting the functionality into two scripts, you gain flexibility in handling server configurations and SSL setups independently. Whether you're managing a single server or deploying numerous domains, this approach will streamline your workflow.