Subdomain Setup Guide

User
Ok how do we setup the subdomain ?
Assistant
Setting up a subdomain involves configuring your DNS, web server, and optionally your application to respond correctly to the subdomain. Here's how to set up a subdomain step-by-step:

### **1. Configure the DNS**
1. **Login to your domain registrar or DNS provider** (e.g., GoDaddy, Namecheap, Cloudflare).
2. Find the section for managing DNS records (often called "DNS Management" or "Zone Editor").
3. Add a new **A Record** or **CNAME Record**:
– **A Record**:
– **Name**: `subdomain` (replace with the subdomain, e.g., `blog`).
– **Value**: The IP address of your server (same as the main domain's server if it's hosted there).
– **CNAME Record** (if using a hosting provider or pointing to another domain):
– **Name**: `subdomain`.
– **Value**: The domain you want it to point to (e.g., `your-main-domain.com`).
4. Save the changes.

### **2. Configure the Web Server**
Depending on your web server (e.g., Nginx, Apache), you need to configure it to handle the subdomain.

#### **For Nginx**:
1. Create a new server block configuration file:

sudo nano /etc/nginx/sites-available/subdomain.yourdomain.com

2. Add the configuration:

server {
       server_name subdomain.yourdomain.com;

       root /var/www/subdomain; # Change to the subdomain's directory
       index index.html index.php;

       location / {
           try_files $uri $uri/ =404;
       }

       location ~ .php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # Adjust PHP version if needed
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
       }
   }

3. Enable the configuration:

sudo ln -s /etc/nginx/sites-available/subdomain.yourdomain.com /etc/nginx/sites-enabled/

4. Test and reload Nginx:

sudo nginx -t
   sudo systemctl reload nginx

#### **For Apache**:
1. Create a new virtual host file:

sudo nano /etc/apache2/sites-available/subdomain.yourdomain.com.conf

2. Add the configuration:

<VirtualHost *:80>
       ServerName subdomain.yourdomain.com

       DocumentRoot /var/www/subdomain
       <Directory /var/www/subdomain>
           Options Indexes FollowSymLinks
           AllowOverride All
           Require all granted
       </Directory>

       ErrorLog ${APACHE_LOG_DIR}/subdomain_error.log
       CustomLog ${APACHE_LOG_DIR}/subdomain_access.log combined
   </VirtualHost>

3. Enable the site and reload Apache:

sudo a2ensite subdomain.yourdomain.com.conf
   sudo systemctl reload apache2

### **3. Set Up SSL for Subdomain**
Use Let's Encrypt to secure the subdomain with HTTPS:

sudo certbot --nginx -d subdomain.yourdomain.com

or

sudo certbot --apache -d subdomain.yourdomain.com

### **4. Configure Your Application**
Ensure your application can recognize the subdomain. In Laravel, you might need to add a route group for the subdomain:

Route::domain('subdomain.yourdomain.com')->group(function () {
    Route::get('/', function () {
        return 'Welcome to the subdomain!';
    });
});

### **5. Test the Subdomain**
1. Open your browser and navigate to `http://subdomain.yourdomain.com` (or `https://` if SSL is configured).
2. Verify that it resolves to the expected content.

If you encounter issues, double-check your DNS, server, and application configurations. Let me know if you need assistance with any specific step!

User
We can setup it from Cpanel
Correct above sentence
Assistant
We can set it up from cPanel.
User
I think we already have access for web hosting ,Let me check and get back to you.
Correct above sentence
Assistant
I think we already have access to web hosting. Let me check and get back to you.

Leave a Comment

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