Step 3: Set Up SSL Certificates
Adding a Subdomain in Nginx
1. Create a New Server Block Configuration
Create a new configuration file for your subdomain:
sudo nano /etc/nginx/sites-available/subdomain.your_domainAdd the following configuration:
server {
listen 80;
server_name subdomain.your_domain;
root /var/www/subdomain.your_domain;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}2. Create the Document Root Directory
sudo mkdir -p /var/www/subdomain.your_domain
sudo chown -R $USER:$USER /var/www/subdomain.your_domain
sudo chmod -R 755 /var/www/subdomain.your_domain3. Create a Sample Page
echo "<h1>Welcome to subdomain.your_domain!</h1>" | sudo tee /var/www/subdomain.your_domain/index.html
echo "<h1>Welcome to IVE domain!</h1>" | sudo tee /var/www/ive.localhost/index.html
sudo ln -s /etc/nginx/sites-available/ive.localhost /etc/nginx/sites-enabled/4. Enable the New Configuration
sudo ln -s /etc/nginx/sites-available/subdomain.your_domain /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxSSL Certificates for Multiple Domains
Using Let's Encrypt
Install Certbot:
sudo apt install certbot python3-certbot-nginxObtain an SSL certificate for your main domain and subdomain:
sudo certbot --nginx -d your_domain -d www.your_domain -d subdomain.your_domainTo add SSL to an additional subdomain later:
sudo certbot --nginx -d new.your_domainFollow the prompts to complete the installation. Certbot will automatically configure SSL for your Nginx configurations.
Using Wildcard Certificates
For multiple subdomains, consider using a wildcard certificate:
sudo certbot certonly --manual --preferred-challenges dns -d your_domain -d *.your_domain
sudo certbot certonly --manual --preferred-challenges dns -d hanapko.com -d *.hanapko.comThis requires DNS verification by adding a TXT record to your domain. After obtaining the wildcard certificate, update your Nginx configurations to use it:
server {
listen 443 ssl;
server_name subdomain.your_domain;
ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;
# Other SSL settings...
root /var/www/subdomain.your_domain;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}Renewing SSL Certificates
Certbot automatically renews certificates. To test the renewal process, you can run:
sudo certbot renew --dry-runVerifying SSL Configuration
Check your SSL configuration security:
sudo openssl s_client -connect your_domain:443 -tls1_3You can also use online tools like SSL Labs to verify your SSL configuration quality: https://www.ssllabs.com/ssltest/