Introduction
cPanel uses Apache virtual host configurations to manage individual domains hosted on the server. In some cases, you may need to apply a custom Apache configuration to a single domain without modifying the global Apache configuration or affecting other websites hosted on the server.
This guide explains how to modify the virtual host configuration for a specific domain on a cPanel server running Apache 2.4 using cPanel’s supported userdata configuration method. The example demonstrates how to add a custom DirectoryIndex directive for a single domain.
Prerequisites
Before modifying the virtual host configuration, ensure the following requirements are met:
- A CentOS 7 server.
- cPanel/WHM is installed and configured.
- Apache 2.4 is installed and enabled.
- Root SSH access to the cPanel server.
- The cPanel username and domain name for which the virtual host configuration needs to be modified.
- Basic knowledge of Apache configuration and Linux commands.
Implementation:
Log in to the server using SSH.
Create a directory for the cPanel user.
mkdir -p /usr/local/apache/conf/userdata/std/2_4/username/domain.tld/
where
username – cPanel username
domain.tld – Your domain name
Create a config file and append your virtual host changes
vi /usr/local/apache/conf/userdata/std/2_4/username/domain.tld/virtualhost.conf
Append your virtual host config change. In this post, we are just adding DirectoryIndex
DirectoryIndex default.html index.html index.php
Execute the command below to add the custom config.
/scripts/ensure_vhost_includes --user=username
Where username is the cPanel username
Verify the syntax of the included configuration. Please note that the config should always error free and Apache won’t start in case of an error.
/scripts/verify_vhost_includes
Restart the Apache service.
service httpd restart
Conclusion
Using cPanel’s userdata configuration method allows you to apply custom Apache virtual host settings to a single domain without directly modifying the main Apache configuration file. This approach helps ensure that custom settings remain specific to the required domain and are less likely to be overwritten during cPanel or Apache configuration updates.
After making any changes, always verify the Apache configuration for syntax errors before restarting the service. This helps prevent Apache startup failures and ensures that the custom virtual host configuration is applied correctly.
FAQ
1. Can I modify the Apache virtual host configuration for only one domain in cPanel?
Yes. You can use the cPanel userdata directory to apply custom Apache configuration to a specific domain without affecting other domains on the server.
2. What happens if there is an error in the custom Apache configuration?
Apache may fail to restart if the configuration contains syntax errors. Always run /scripts/verify_vhost_includes before restarting Apache.
3. Do I need to restart Apache after modifying the virtual host configuration?
Yes. After adding or modifying the configuration, restart Apache to apply the changes:
service httpd restart
Talk to our experts:
Looking for the right technology solution for your business?. Our experts can help with web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team here.