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.
Step 1: Enable Necessary Apache Modules
Ensure Apache has the required modules enabled:
bashCopy codesudo a2enmod vhost_alias
sudo systemctl restart apache2
Step 2: Create Directory Structure
Create separate directories for each subdomain’s content:
bashCopy codesudo mkdir -p /var/www/sub1.example.com/public_html
sudo mkdir -p /var/www/sub2.example.com/public_html
Set appropriate permissions:
bashCopy codesudo chown -R $USER:$USER /var/www/sub1.example.com/public_html
sudo chown -R $USER:$USER /var/www/sub2.example.com/public_html
sudo chmod -R 755 /var/www
Step 3: Add Test Content
Add an index.html
file to each subdomain directory to test:
bashCopy codeecho "<h1>Welcome to Sub1</h1>" | sudo tee /var/www/sub1.example.com/public_html/index.html
echo "<h1>Welcome to Sub2</h1>" | sudo tee /var/www/sub2.example.com/public_html/index.html
Step 4: Create Virtual Host Files
Create separate configuration files for each subdomain:
bashCopy codesudo nano /etc/apache2/sites-available/sub1.example.com.conf
Add the following content for sub1.example.com
:
apacheCopy code<VirtualHost *:80>
ServerName sub1.example.com
DocumentRoot /var/www/sub1.example.com/public_html
<Directory /var/www/sub1.example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/sub1-error.log
CustomLog ${APACHE_LOG_DIR}/sub1-access.log combined
</VirtualHost>
Repeat for sub2.example.com
:
bashCopy codesudo nano /etc/apache2/sites-available/sub2.example.com.conf
Add the following content:
apacheCopy code<VirtualHost *:80>
ServerName sub2.example.com
DocumentRoot /var/www/sub2.example.com/public_html
<Directory /var/www/sub2.example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/sub2-error.log
CustomLog ${APACHE_LOG_DIR}/sub2-access.log combined
</VirtualHost>
3. Enable Virtual Hosts and Restart Apache
Enable the new Virtual Host configurations:
bashCopy codesudo a2ensite sub1.example.com.conf
sudo a2ensite sub2.example.com.conf
Disable the default configuration if not needed:
bashCopy codesudo a2dissite 000-default.conf
Restart Apache to apply changes:
bashCopy codesudo systemctl restart apache2
4. Verify DNS Configuration
Ensure DNS records for your subdomains are pointing to your server’s IP address:
sub1.example.com A <server_ip>
sub2.example.com A <server_ip>
You can verify this using tools like dig
or nslookup
:
bashCopy codedig sub1.example.com
5. Test Your Setup
Open a web browser and visit:
http://sub1.example.com
http://sub2.example.com
You should see the respective content for each subdomain.
6. Optional: Enable HTTPS
For secure connections, set up HTTPS using Certbot and Let’s Encrypt:
bashCopy codesudo apt update
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
Follow the prompts to install SSL certificates for your subdomains.