How to make custom changes to Virtualhost settings in httpd.conf on a WHM/Cpanel VPS without losing the changes
On a WHM/cpanel VPS server the httpd.conf is automatically generated from cpanel scripts based on templates and you are not supposed to change this file directly. Any changes that you make in the httpd.conf file will be lost when cpanel re-generates the file or when cpanel is updated. This is an inconvenience especially when your application requires you to make virtualhost modifications in the configuration. However cpanel provides an alternative way to edit the httpd.conf file.
If you open /usr/local/apache/conf/httpd.conf
you will see that there is documentation about the location where you can add additional configuration options for each virtualhosts. Each domain on the cpanel server can have its own custom configuration for the virtualhost corresponding to the domain. The main httpd.conf file would then inculde these custom configuration if is present on the server. The commented documentation says in httpd.conf says that custom settings will be included from /usr/local/apache/conf/userdata/std/2/username/domain.com/*.conf
. So any files with .conf extension in the said path can be used to add custom virtualhost configuration.
You have to first create a directory correspondging to the path above. You can do this as follows
mkdir -p /usr/local/apache/conf/userdata/std/2/username/example.com cd /usr/local/apache/conf/userdata/std/2/username/example.com vi virtualhost.conf
Remember to change example.com
to the domain for which you are going to create the custom virtualhost configuration and username
to the cpanel username of the user corresponding to the domain.
Once you are done with making the modification you can re-create the httpd conf file by running the following command
/scripts/ensure_vhost_includes --all-users
That is all and your custom changes should be live and they will not be overwritten the next time cpanel recreates httpd.conf