Multiple Subdomains After making configuration changes, always test your setup to ensure that the subdomains are accessible and serving content as expected. You can use tools like curl
, wget
, or simply access the subdomains via a web browser.
- DNS Configuration: Ensure that the DNS records for your domain and subdomains are properly configured to point to the IP address of your Apache server.
- Apache Configuration:
- Create Virtual Hosts: Set up virtual host configurations for each subdomain in your Apache configuration files.
- Enable Virtual Hosts: Enable the virtual host configurations so Apache knows to serve content for each subdomain.
Here’s a step-by-step guide Multiple Subdomains
- DNS Configuration:
- Access your DNS management panel provided by your domain registrar or DNS hosting provider.
- Add A or CNAME records for each subdomain pointing to the IP address of your Apache server.
subdomain1.example.com A 192.0.2.1 subdomain2.example.com A 192.0.2.1
- Apache Configuration:
- SSH into your Apache server.Navigate to the Apache configuration directory. On many systems, this is located at
/etc/apache2/sites-available/
.Create configuration files for each subdomain.
- SSH into your Apache server.Navigate to the Apache configuration directory. On many systems, this is located at
Save and exit the configuration files.
Enable Virtual Hosts:
Use the a2ensite command to enable each virtual host configuration:
sudo a2ensite subdomain1.example.com.conf sudo a2ensite subdomain2.example.com.conf- Restart Apache to apply the changes:
sudo systemctl restart apache2
sudo systemctl restart apache2
- Make sure that the directories specified as
DocumentRoot
in each virtual host configuration exist. You can create them if they don’t.
Permissions (if necessary):
- Ensure that the Apache process has appropriate permissions to read files from the
- DocumentRoot directories of each subdomain.
SSL/TLS Configuration:
- If you want to secure your subdomains with SSL/TLS, you’ll need to obtain SSL certificates for each subdomain. You can use Let’s Encrypt for free SSL certificates.
- Once you have the SSL certificates, configure SSL/TLS settings in your Apache virtual host configurations. Update the
<VirtualHost>
blocks to listen on port 443 and specify the paths to your SSL certificates. - Ensure that
mod_ssl
is enabled in Apache.
Wildcard Subdomains:
- If you have a large number of subdomains or plan to add more dynamically, consider using wildcard subdomains. This allows any subdomain of your domain to resolve to your server.
- For example, you can create a virtual host configuration for
*.example.com
to handle all subdomains underexample.com
.
Directory Index and Options:
- Configure directory index and options for each virtual host. You can specify default files to be served when a directory is accessed (
DirectoryIndex
) and set various options likeFollowSymLinks
orIndexes
according to your requirements.
Logging:
- Customize logging settings for each virtual host if needed. You can specify separate error logs and access logs for each subdomain to better track and manage traffic.
.htaccess Configuration:
- Utilize
.htaccess
files in each subdomain’s DocumentRoot directory for additional configuration or security settings specific to that subdomain. Ensure thatAllowOverride
directive is set appropriately in your virtual host configuration to allow.htaccess
overrides.
ServerAlias:
- If you want your subdomains to share the same content as the main domain, you can use the
ServerAlias
directive in your virtual host configuration. This allows multiple domain names to be associated with the same virtual host.