keepcalculator

06 Numbers and Powers

Subdomain Sharing Main Site Database: How-To

Card No. 06·67749 · Updated · Standard formula, shown below

This guide shows you how to create a subdomain (like shop.example.com or blog.example.com) that uses the same database as your main site. This is useful when you want a separate URL for a section of your site but need it to read and write the same data—for example, a mobile version, a customer portal, or a staging area that uses real data. You’ll learn the core concept and the exact steps for typical setups, along with common pitfalls and how to fix them.

What You’ll Need

  • Access to your domain’s DNS settings (usually through your domain registrar or hosting provider).
  • Access to your web server configuration (Apache, Nginx, or a hosting control panel like cPanel, Plesk, or a cloud console).
  • Your main site’s database credentials (host, database name, username, password).
  • Basic familiarity with your server’s file structure and how your main site’s code connects to the database.

Step-by-Step

  1. Create the subdomain in your DNS. Log in to your DNS provider and add an A record (or CNAME if you’re pointing to another host) for the subdomain. For example, if your main domain is example.com and you want app.example.com, create an A record with host app and the IP address of your server. If you’re using a hosting control panel, there’s often a “Subdomains” section that does this automatically.
  2. Configure your web server to serve the subdomain. In Apache, create a new virtual host block for the subdomain. In Nginx, create a new server block. Point the document root to a directory on your server—this can be the same directory as your main site or a new one, depending on your needs. If you’re using a control panel, use the “Subdomains” feature to create the directory and virtual host automatically.
  3. Copy or link your main site’s code to the subdomain’s directory. If you want the subdomain to run the exact same application, you can either copy the files or, better, create a symbolic link from the subdomain’s document root to the main site’s directory. This way, any updates to the main code are reflected on the subdomain immediately. For example, on Linux: ln -s /var/www/example.com /var/www/app.example.com (assuming the subdomain’s root is that directory).
  4. Set up the database connection for the subdomain. The key is that your subdomain’s code must use the same database credentials as your main site. If you copied the code, the configuration file (like wp-config.php for WordPress, .env for Laravel, or config.php for others) already contains the correct credentials. If you created a new configuration file, copy the database host, name, username, and password from your main site’s configuration. Ensure the database user has permissions to access the database from the server IP (or localhost if it’s the same server).
  5. Test the connection. Visit your subdomain in a browser. If the site loads and shows data from the main database, you’re done. If you see an error about database connection, check the credentials and the database host (if the database is on a separate server, the host might need to be a public IP or a hostname that the subdomain’s server can resolve).
  6. Handle cookies and sessions (if applicable). If your application uses sessions or cookies, you may need to adjust the cookie domain to work across both the main domain and the subdomain. For example, in PHP you can set session_set_cookie_params(['domain' => '.example.com']) to share sessions across all subdomains. In JavaScript, set document.cookie with domain=.example.com.
  7. Update any absolute URLs in your database. If your database contains URLs that point to the main domain (like in a content management system), they will still work when accessed via the subdomain, but you might want to use relative URLs or a dynamic base URL to avoid mixed content issues. Many applications have a setting for the site URL that you can override in the subdomain’s configuration.

Common Mistakes

  • Forgetting to create the DNS record – The subdomain won’t resolve without it. Always wait for DNS propagation (can take up to 48 hours, but usually less).
  • Using different database credentials – If you create a new configuration file and accidentally use a different database name or user, you’ll get connection errors.
  • Not setting the database host correctly – If the database is on a separate server, using localhost will fail. Use the correct hostname or IP.
  • Mixing up document roots – Pointing the subdomain to a different directory than intended can cause the subdomain to show a different site or a 404.
  • Ignoring file permissions – If the subdomain’s directory is not readable by the web server, you’ll get a 403 error.
  • Not considering SSL certificates – If your main site uses HTTPS, you’ll need a certificate for the subdomain too, or browsers will show a warning.

Tips

  • Use a symbolic link instead of copying files – This ensures the subdomain always runs the latest code without manual updates.
  • Set a base URL dynamically – Use a configuration that detects the current domain and sets the site URL accordingly, so links work on both domains.
  • Test on a staging subdomain first – Create a subdomain like staging.example.com to test changes before applying them to the main site.
  • Use environment variables – Store database credentials in environment variables (e.g., DB_HOST, DB_NAME) so you can easily switch between setups.
  • Monitor database load – Sharing a database means both sites hit the same database. If you expect high traffic, consider caching or a database replica.
  • Keep a backup – Before making changes, back up your configuration files and database, so you can revert if something goes wrong.

Troubleshooting

  • Subdomain doesn’t load (DNS error) – Check that the DNS record exists and points to the correct IP. Use ping or nslookup to verify. If you just created the record, wait a few hours.
  • Database connection error – Double-check the credentials, host, and that the database user has privileges. If the database is on a different server, ensure the database server allows remote connections from your subdomain’s server IP.
  • Subdomain shows the main site’s content but with broken links – This is usually because the application is generating absolute URLs with the main domain. Override the site URL in the subdomain’s configuration (e.g., in WordPress, define WP_HOME and WP_SITEURL).
  • 403 Forbidden error – Check file permissions on the subdomain’s directory. The web server user (e.g., www-data) needs read and execute permissions.
  • 404 Not Found – Ensure the document root is correct and that the application’s entry point (like index.php) exists.
  • SSL certificate mismatch – If you get a security warning, install a valid SSL certificate for the subdomain. Many hosts offer free Let’s Encrypt certificates.

Frequently Asked Questions

Can I share the database without sharing the code?

Yes. You can have a completely different application on the subdomain that connects to the same database. Just make sure the database schema is compatible and the credentials are correct.

Will sharing the database slow down my main site?

It can, if the subdomain generates heavy database queries. Use caching, optimize queries, and consider a separate database replica for read-heavy operations if needed.

How do I handle user logins across both domains?

Set the cookie domain to .example.com so the session cookie is sent to both the main domain and the subdomain. Also, ensure your session handling uses the same session name and storage.

Do I need a separate SSL certificate for the subdomain?

Yes, unless you have a wildcard certificate that covers *.example.com. Most modern browsers require a valid certificate for each subdomain.

Can I use a subdomain to create a mobile version of my site?

Yes, that’s a common use case. You can point the subdomain to the same code and use responsive design, or you can have a separate mobile-optimized codebase that reads from the same database.

What if my database is on a separate server?

You’ll need to configure the database host to the server’s IP or hostname, and ensure the database server allows remote connections from your subdomain’s server. Also, use a secure connection (SSL) for database traffic if possible.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →