httpd.conf and Cpanel: How to customize settings

/ bocabit / internet

TerminalEl archivo httpd.conf es el que indica a Apache todos los paråmetros de configuración cuando este se inicia. Y entre otras cosas indica los módulos que debe cargar y los hosts que debe establecer. Lo normal es editar este archivo a mano, guardando copias de seguridad antes de realizar cada cambio. Sin embargo, si utilizamos Cpanel y por tanto tenemos instalado EasyApache, existe un método muy sencillo de introducir cambios en este fichero y que los cambios persistan cada vez que realicemos una actualización.

If we take a look at our server’s httpd.conf, which is usually located in the path (although it may vary, on CentOS at least it is located there):

usr/local/apache/conf/

It is quite likely that we will find blocks of code that look like this:

<VirtualHost XX.XX.XX.XX:80>
  ServerName myserver.domain.com
  ServerAlias www.myserver.domain.com
  DocumentRoot /home/SITENAME/public_html
  ServerAdmin webmaster@SITENAME.com
  UseCanonicalName Off
</VirtualHost>

1. Make changes within a VirtualHost

If we use Cpanel, within the declaration of a VirtualHost, we will have a commented phrase like the following:

# DO NOT EDIT. AUTOMATICALLY GENERATED. IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.

This phrase means that if we edit the file, every time we generate it again the changes we have made will be deleted, so we must include the changes using templates, that is, putting the changes in other .conf files that will be included in the main file once we compile it.

 # To customize this VirtualHost use an include file at the following location # Include "/usr/local/apache/conf/userdata/std/2/username/domain.com/*.conf"

To include the files as stated in the line above, we must create the necessary directories that correspond to the domain that would be affected and then one or more .conf files. We must keep in mind that the changes we make will be to add instructions within the virtualhost, so we will not be able to add a VirtualHost directive in this way (because it would be nested inside another and it cannot be done).

Once we have made the desired changes, we must verify that the changes made are correct, executing the command:

/scripts/verify_vhost_includes

If everything is correct, we will update the includes so that the httpd.conf file is updated. If the changes only affect one user:

/scripts/ensure_vhost_includes –user=<username>

If the changes affect all users:

/scripts/ensure_vhost_includes –all-users

And finally, restart Apache with the command:

/etc/init.d/httpd restart

2. Making changes outside of a VirtualHost

If we want to add directives outside of a VirtualHost, we must approach the problem differently. As in the previous case, the httpd.conf file should not be edited directly, so Cpanel has template files in which we will add our instructions and which will be loaded when we restart Apache.

The path where the includes must be entered is:

usr/local/apache/conf/includes

and we have 3 possibilities:

  • Pre-Main: The code will be added to the beginning of the httpd.conf. The files will be called “pre_main_1.conf”, “pre_main_2.conf”, etc; and will be included in order.
  • Pre-VirtualHost: The code will be added before the first VirtualHost. The files will be named “pre_virtualhost_1.conf”, “pre_virtualhost_2.conf”, etc.
  • Post-VirtualHost: The code will be added to the end of the httpd.conf file and the files will be called “post_virtualhost_1.conf”, “post_virtualhost_2.conf”, etc”.

If, for example, we wanted to create a new VirtualHost, we could add the code before the first existing VirtualHost, that is, using a “pre_virtualhost_x.conf” file.

After having added the desired changes, we must restart Apache:

/etc/init.d/httpd restart

When is this useful?

In this blog we have the address bocabit.com, but it is actually a domain pointed to another domain (bocabit.com). What we were interested in was for the subdomain to appear in the address bar, so I had to add a VirtualHost that referenced the same directory as the domain so that upon entering it would not return an error page.

It is also useful if we want to add routes accessible through http that are not in the public_html directories, such as svn, a directory with private downloads, etc.

Sources Cpanel: Adding custom configuration to httpd.conf CPanel EasyApache and Customizing httpd.conf in WHM / cPanel”)