Question

Make subdomain sites seperate (eg, sub1.site.com and sub2.site.com)

In for example, CPanel, I can easily redirect a subdomain to a folder so that it looks like two different sites.

On my DigitalOcean control panel, I can set a DNS A record to point a subdomain at an IP address, but I can’t figure out how to make it go to a specific folder. My motivation is so I can put a ‘fun and crazy’ website seperate from my ‘professional work’ website.

I have looked at using .htaccess, but Apache recommends editing the httpd.conf file as I have access to that (I am on Fedora). I can make a redirect from subsite.site.com to site.com/subsite, however I would really like subsite.site.com to show different content to subsite2.site.com and also different to site.com.

I can see some indication in Digital Ocean Apps that I can configure up to 3 sites this way for ‘free’, but I do not know which app to deploy or how to do it there.

I am on a DO Droplet running Fedora and Apache.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 4, 2024
Accepted Answer

Hey Matt! 👋

To achieve what you’re aiming for, where each subdomain (like sub1.site.com and sub2.site.com) points to a separate directory on your server, you’ll want to use Apache Virtual Hosts. This allows you to configure Apache to serve different content based on the subdomain.

You can check out the guide here:

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-20-04

But overall the process is similar on Fedora.

Here’s a quick guide to setting this up:

  • In your DigitalOcean control panel, make sure you’ve set up A records for each subdomain (sub1.site.com, sub2.site.com, etc.) that point to your Droplet’s IP address. It sounds like you’ve already done this step!

  • On your Droplet, create separate directories to hold the files for each subdomain. For example:

    sudo mkdir -p /var/www/sub1
    sudo mkdir -p /var/www/sub2
    
  • Place your website files in each respective directory.

  • Open Apache’s configuration file for virtual hosts. On Fedora, you may need to edit the file located at /etc/httpd/conf/httpd.conf, or, if Virtual Hosts are in a separate directory, look under /etc/httpd/conf.d/.

  • Add separate <VirtualHost> blocks for each subdomain. Here’s an example configuration:

    <VirtualHost *:80>
        ServerName sub1.site.com
        DocumentRoot /var/www/sub1
    
        <Directory /var/www/sub1>
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName sub2.site.com
        DocumentRoot /var/www/sub2
    
        <Directory /var/www/sub2>
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    
  • This tells Apache to serve files from /var/www/sub1 when someone visits sub1.site.com, and files from /var/www/sub2 for sub2.site.com.

  • Save the configuration file and then test it for syntax errors:

    sudo apachectl configtest
    
  • If everything looks good, restart Apache to apply the changes:

    sudo systemctl restart httpd
    

If you want to secure these subdomains with HTTPS, you can use Let’s Encrypt to generate SSL certificates for each subdomain. Certbot can automate this for Apache. Just run:

sudo dnf install certbot python3-certbot-apache
sudo certbot --apache -d sub1.site.com -d sub2.site.com

This will configure SSL for your subdomains, making them accessible via HTTPS.

Now, visit http://sub1.site.com and http://sub2.site.com. Each subdomain should load content from its respective directory.

On another note, if you’re interested in using DigitalOcean’s App Platform instead of managing this setup manually, you could deploy separate apps for each subdomain using their individual Git repositories. App Platform allows you to map custom domains (or subdomains) to different apps, and it automatically handles SSL for you.

You can check out DigitalOcean App Platform and see if that’s a better fit for your project if you’d prefer a managed setup.

Let me know how it goes or if you have more questions!

- Bobby

KFSys
Site Moderator
Site Moderator badge
November 7, 2024

heya,

To serve different content for each subdomain (e.g., sub1.site.com and sub2.site.com) on your Fedora-based DigitalOcean Droplet running Apache, you can set up virtual hosts in Apache’s configuration. Here’s how:

  1. Set up DNS:

    • In your DigitalOcean DNS settings, add A records for each subdomain (sub1 and sub2), pointing them to your Droplet’s IP address. This step directs traffic for these subdomains to your server.
  2. Configure Apache:

    • Open your Apache configuration directory (usually /etc/httpd/conf.d/ on Fedora). You’ll create separate configuration files or add virtual host blocks for each subdomain in httpd.conf.
  3. Create Virtual Host Blocks:

    • Each subdomain needs a separate <VirtualHost> entry. This directs traffic for each subdomain to a specific directory on your server. Here’s an example setup:
<VirtualHost *:80>
    ServerName sub1.site.com
    DocumentRoot /var/www/sub1  # Adjust to the folder for sub1

    <Directory /var/www/sub1>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName sub2.site.com
    DocumentRoot /var/www/sub2  # Adjust to the folder for sub2

    <Directory /var/www/sub2>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
  • Create the Directories:

    • Make sure the folders (/var/www/sub1 and /var/www/sub2 in this example) exist and contain the content for each subdomain.
  • Enable and Restart Apache:

    • After setting up the configuration, restart Apache to apply the changes:
sudo systemctl restart httpd

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.