El 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â)