How to create dynamic subdomain and install SSL certificate – 2024 Easy Way

Blog VPS SERVER

In this guide, we’ll walk through the steps to create dynamic subdomains on a VPS and install SSL certificates using Let’s Encrypt with DNS challenge. Follow the steps below to set up your VPS for dynamic subdomains and secure them with SSL:


Step 1: Create a Wildcard DNS Record

  1. Go to your DNS provider and create a wildcard * A record.
  2. Point the wildcard record to your VPS’s IP address. This allows any subdomain (e.g., randomtext.domain.com) to resolve to your VPS.

Step 2: Verify DNS Changes

  1. Confirm that the domain is correctly pointed to your VPS using DNSChecker.org.
  2. Ensure the A record for *.yourdomain.com resolves to your VPS’s IP address.

Step 3: Set Up Nginx for Dynamic Subdomains

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name *.yourdomain.com;

    client_max_body_size 20M;

    root /var/www/wildcard;
    error_log /var/www/wildcard/error.log;
    access_log /var/www/wildcard/access.log;


    try_files $uri $uri/ /index.php?$args;
    index index.php index.html;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}
  1. Log in to your VPS and create a basic Nginx above configuration for handling dynamic subdomains.
  2. Configure the server block in Nginx to accept any subdomain (server_name *.yourdomain.com;).

Step 4: Test Nginx Configuration

  1. Run the command nginx -t to check for any configuration errors.
  2. If no errors are found, restart Nginx by running sudo systemctl restart nginx.

Step 5: Access Your Dynamic Subdomain

  • Now, visit any subdomain, for example, randomtext.yourdomain.com. You should see your website’s content loading under the dynamically generated subdomain.

Step 6: Install SSL Certificates (Let’s Encrypt with DNS Challenge)

  1. Use Certbot to issue SSL certificates for your wildcard domain (*.yourdomain.com) using the DNS challenge method.
  • install certbot : sudo apt install certbot python3-certbot-nginx
  • Command: sudo certbot certonly --manual --preferred-challenges=dns -d "*.yourdomain.com".
  1. Follow the instructions to add the DNS TXT record as required by Let’s Encrypt for domain validation.

Step 7: Add SSL Configuration in Nginx

  1. Update your Nginx configuration to use the SSL certificates.
  • Add the SSL certificate paths in the Nginx server block for your dynamic subdomains.
   ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

Step 8: Verify and Restart Nginx

  1. Run nginx -t again to ensure there are no errors in your SSL configuration.
  2. If no issues are found, restart Nginx to apply the changes: sudo systemctl restart nginx.

Step 9: Success!

  • Your dynamic subdomains are now secured with SSL certificates. You can now access any subdomain like randomtext.yourdomain.com over HTTPS.

By following these steps, you’ve successfully set up dynamic subdomains with SSL certificates on your VPS!

Leave a Reply

Your email address will not be published. Required fields are marked *