Tag: Apache

  • How to Create Custom 404 Error Page in Apache

    A 404 error happens when a visitor comes to your website and the page requested is not found on your website. This happens when a link changed and some other pages or websites still link to your old web page URL.

    When “HTTP/1.1 404 Not Found” error occurs, web servers show a basic error message saying the page is not found on the server. Most visitors will leave your website when this happens. A properly built custom 404 page allows your site to engage the visitor, present them useful information, so they stay on the website, explore other pages instead of leaving the website.

    It is good practice to track 404 errors, set a 301 redirect for these pages to the proper destination, so your site visitors can find whatever they are looking for with fewer efforts.

    To create a custom 404 error page in the Apache web server, create a .htaccess file with the following content

    ErrorDocument 404 /404.html
    

    Create an HTML page with your custom 404 error message, save it as 404.html.

    Now when you access a page that is not present on your website, you will see this custom 404 error page.

    See htaccess

  • Find what MPM model Apache is using

    Find what MPM model Apache is using

    Apache is the most popular web server, it is Open Source and modular. Multi-Processing Modules (MPM) in Apache decides how Apache listens to the network and handles the incoming requests. Apache have many MPM Modules (Multi-Processing Modules), the most popular among them are Prefork, worker, and Event. You can only activate one Apache MPM at any time.

    The most popular MPMs available on Linux servers are

    • Prefork
    • Worker
    • Event

    To find out which MPM you are using

    On CentOS, run

    [root@server22 ~]# httpd -M | grep mpm
     mpm_worker_module (shared)
    [root@server22 ~]# 
    

    On Ubuntu

    root@server1:~# a2query -M
    prefork
    root@server1:~# 
    

    Or

    root@server1:~# a2query -m | grep mpm
    mpm_prefork (enabled by maintainer script)
    root@server1:~# 
    

    Or

    root@server1:~# apachectl -M | grep mpm
     mpm_prefork_module (shared)
    root@server1:~# 
    

    You can also find this information from the Apache Status page provided by mod_info

    To enable mod_info, in httpd.conf, add

    
    SetHandler server-info
    
    

    Now visit

    http://your-server-ip/server-info
    

    See Apache

  • Starting httpd: (99)Cannot assign requested address

    On a server, when i restart apache, i got the error

    # service httpd restart
    Stopping httpd: [ OK ]
    Starting httpd: (99)Cannot assign requested address: make_sock: could not bind to address 67.223.236.11:80
    no listening sockets available, shutting down [FAILED]
    

    The error was due to Apache trying to bind to an IP address, that is no longer present in the server.

    This is fixed by modifying /etc/httpd/conf/httpd.conf

    Replace

    Listen 67.223.236.11:80
    

    With

    Listen *:80
    

    See Apache

  • Apache Auto Renew SSL on Password Protected site

    I have a web site that is password protected using Apache basic autenticiation.

    I used following code in Apache config to password protect.

    
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/demo-sok-htpaswd
        Require valid-user
    
    

    The problem is when SSL need auto renew, it need url like http://domain/.well-known/ to be accessable with out any password for domain ownership verification.

    To allow .well-known folder to be accessable with out password, i added following code in apache configuration file.

    
        Require all granted
    
    

    After restarting apache, urls startng with .well-known work with out needing any password.

    See Apache, SSL, LetsEncrypt

  • Apache disable directory index

    Apache disable directory index

    When Apache Directory Index is enabled, if you browse to a url that have no index file present, you will see list of all files. This is not good for securiy as hackers can see all files present in the directory.

    apache directory index

    One way to disable this directory listing is create a file with name index.html or index.php (whatever file name that is specified in Apache DirectoryIndex) with no content.

    Another solution is to disable directory indexing for the web site in Apache virtual host configuration, for this, under Options -Indexes

    Here is an example apache configuration file

    
        ServerName serverok.in
        ServerAlias www.serverok.in
        ServerAdmin [email protected]
        DocumentRoot /home/serverok.in/html
        CustomLog ${APACHE_LOG_DIR}/serverok.in.log combined
        ErrorLog ${APACHE_LOG_DIR}/serverok.in-error.log
        
            Options All -Indexes
            AllowOverride All
            Require all granted
            Order allow,deny
            allow from all
        
    
    

    Method 2: disable autoindex module

    Directory listing is generated by apache module mod_autoindex.

    You can disable this module to disable directory listing generation

    On Ubuntu/Debian

    a2dismod autoindex -f
    systemctl restart apache2
    

    See Apache

  • Change Apache User in Ubuntu

    On Ubuntu/Debian server, apache run as user www-data. When you install Apache web server on your local computer for developement purpose, it may be easier to run Apache as your user. If you run Apache as www-data user, you will need to chmod folders 777 for yoru web application to write to a folder like file upload, creating log files etc..

    To change Apache user, edit file

    vi /etc/apache2/envvars
    

    Find and replace www-data with your user name. You can do this with following sed command

    sed -i "s/www-data/USERNAME/g" /etc/apache2/envvars
    chown -R USERNAME:USERNAME /var/lib/php
    
  • Enable CORS in Apache

    To enable CORS in apache, add the following in VirtualHost or .htaccess

    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"
    

    If your apache installation don’t have mod_headers installed, you need it enabled with

    a2enmod headers
    systemctl restart apache2
    
  • apachectl status www-browser not found

    apachectl status www-browser not found

    On a Ubuntu server, run i run apachectl status, i get following error.

    root@server:~# apachectl status
    /usr/sbin/apachectl: 113: /usr/sbin/apachectl: www-browser: not found
    'www-browser -dump http://localhost:80/server-status' failed.
    Maybe you need to install a package providing www-browser or you
    need to adjust the APACHE_LYNX variable in /etc/apache2/envvars
    root@server:~#
    

    To fix error, install lynx text based browser.

    apt install lynx
    

    After installing lunx, apachectl status started working.

    apachectl status

    See Apache

  • Apache LogFormat show full domain name

    To show the full domain name in the Apache access log, you can use the following log format

    LogFormat "%h %l %u %t \"%m https://%v:%p%U%q %H\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" sok_log

    If you want to show the port also, use

    LogFormat "%h %l %u %t \"%m https://%v%U%q %H\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" sok_log

    To use this LogFormat for logging, edit Apache VirtualHost, add

    CustomLog /path/to/access.log sok_log

    See Apache

  • Password Protect Site using htaccess

    To password protect a web site or a sub folder using .htaccess, create a .htaccess file in the folder.

    vi .htaccess
    

    Add following content

    AuthType Basic
    AuthName "Restricted Content"
    AuthUserFile /etc/apache2/site-logins
    Require valid-user
    

    In this case, i used /etc/apache2/site-logins as AuthUserFile, this will store all user and password. You can change this file path to whatever you need. Make sure it is not accessable from public, so keep it outside of document root of your web site.

    Now create a user with command

    htpasswd -c /etc/apache2/site-logins USER_NAME_HERE