viernes, 25 de abril de 2014

Customizar Errores HTTP Apache2 y Ubuntu 14.04

Al no encontrar información decente decidí hacer esta entrada sobre como configurar los errores que suelen aparecer en nuestros sitios web.

Bueno abrimos el archivo localized-error-pages.conf que esta el directorio /etc/apache2/conf-avaible.

emacs -nw /etc/apache2/conf-available/localized-error-pages.conf

Una vez que abrimos el archivo, aparece todo comentado y lo que tenemos que hacer es buscar la linea en la que sale <IfModule mod_negotiation.c> y desde ahí borramos los # hasta antes de la linea que dice # vim: syntax=apache ts=4 sw=4 sts=4 sr noet 

<IfModule mod_negotiation.c>
        <IfModule mod_include.c>
                <IfModule mod_alias.c>

                        Alias /error/ "/usr/share/apache2/error/"

                        <Directory "/usr/share/apache2/error">
                                Options IncludesNoExec
                                AddOutputFilter Includes html
                                AddHandler type-map var
                                Order allow,deny
                                Allow from all
                                LanguagePriority en cs de es fr it nl sv pt-br ro
                                ForceLanguagePriority Prefer Fallback
                        </Directory>

                        ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
                        ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
                        ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
                        ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
                        ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
                        ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
                        ErrorDocument 410 /error/HTTP_GONE.html.var
                        ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
                        ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
                        ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
                        ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
                        ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
                        ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
                        ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
                        ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
                        ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
                        ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var
                </IfModule>
        </IfModule>
</IfModule>

Aqui tienes la opción de usar el archivo por default, poner un mensaje y/o un URL.

# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html

Los archivos por default están en el directorio /usr/share/apache2/error/ y ahí aparecen los archivos.

root@hp:~# ls /usr/share/apache2/error/
contact.html.var
HTTP_GONE.html.var
HTTP_NOT_FOUND.html.var
HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
HTTP_UNAUTHORIZED.html.var
README
HTTP_BAD_GATEWAY.html.var
HTTP_INTERNAL_SERVER_ERROR.html.var
HTTP_REQUEST_TIME_OUT.html.var
HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
HTTP_BAD_REQUEST.html.var
HTTP_LENGTH_REQUIRED.html.var
HTTP_NOT_IMPLEMENTED.html.var
HTTP_REQUEST_URI_TOO_LARGE.html.var
HTTP_VARIANT_ALSO_VARIES.html.var
HTTP_FORBIDDEN.html.var
HTTP_METHOD_NOT_ALLOWED.html.var
HTTP_PRECONDITION_FAILED.html.var
HTTP_SERVICE_UNAVAILABLE.html.var
include


En la carpeta include hay 3 archivos que puedes editar para darle estilo con CSS a nuestros mensajes de error.

Es muy importante tener habilitado el modulo de include el alias en nuestro apache.

root@hp:~# a2enmod alias
root@hp:~# a2enmod include

En el caso de que quieras modificar el email de administrador y el nombre del server lo puedes hacer haciendo unos cambios en el archivo 000-default.conf que se encuentra en el directorio /etc/apache2/sites-available/


emacs -nw /etc/apache2/sites-available/000-default.conf

Y nos muestra algo como lo siguiente:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that                                   
        # the server uses to identify itself. This is used when creating                                                         # redirection URLs. In the context of virtual hosts, the ServerName                                               
        # specifies what hostname must appear in the request's Host: header to                                    
        # match this virtual host. For the default virtual host (this file) this                                        
        # value is not decisive as it is used as a last resort host regardless.                                      
        # However, you must set it for any further virtual host explicitly.                                                                                                                                          
        ServerName NOMBRE_DEL_SERVIDOR.com

        ServerAdmin YO@gmail.com
        DocumentRoot /var/www/

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,                                          
        # error, crit, alert, emerg.                         
        # It is also possible to configure the loglevel for particular                                                  
        # modules, e.g.                                      
        #LogLevel info ssl:warn                                                                                                                                                                                    

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are                                                    
        # enabled or disabled at a global level, it is possible to                                                  
        # include a line for only one particular virtual host. For example the                                        
        # following line enables the CGI configuration for this host only                                               
        # after it has been globally disabled with "a2disconf".                                                 
        #Include conf-available/serve-cgi-bin.conf                                                                                      
</VirtualHost>

Una vez hecho todo lo anterior reiniciamos apache2.

root@hp:~# service apache2 restart

Espero y les sirva.
Saludos.

Referencias:
Urias, A. (2010). Personalizar los Errores de nuestro apache2. 25/04/2014, de http://www.amadourias.com/ Sitio web: http://www.amadourias.com/?p=101
guntbert. (2014). Why is my Apache not working after upgrading to Ubuntu 14.04?. 25/04/2014, de http://askubuntu.com/ Sitio web: http://askubuntu.com/questions/452042/why-is-my-apache-not-working-after-upgrading-to-ubuntu-14-04/452060#452060?newreg=b3ac952870a54429b5d36a3b834cf4d1
Haas, J. (2013). Default Settings. 25/04/2014, de http://linux.about.com/ Sitio web: http://linux.about.com/od/ubusrv_doc/a/ubusg25t05.htm