Category: Apache

  • ionos Magento Internal Server Error

    ionos Magento Internal Server Error

    After migrating a Magento site to ionos shared hosting, internal pages of the site stopped working with “Internal Server Error”.

    ionos magento internal server error

    The problem is due to .htaccess file. The error is fixed by editing pub/.htaccess file

    vi pub/.htaccess
    

    Find

    #RewriteBase /magento/
    

    Replace with

    RewriteBase /
    

    Many static files were also not loading, to fix it, edit the file

    vi pub/static/.htaccess
    

    Find

    #RewriteBase /magento/pub/static/
    

    Replace with

    RewriteBase /static/
    
  • How to debug Apache VirtualHost not working

    How to debug Apache VirtualHost not working

    On an Apache webserver running in Ubuntu, I added a VirtualHost entry for a domain name. But instead of showing the page, it shows the website from the default Apache VirtualHost (000-default.conf). At first, I was thinking it is because Apache was somehow not able to handle Named Virtual Hosts. So I created another domain name on the server, that worked fine. Only this specific domain name VirtualHost did not work.

    To see Apache VirtualHost configuration, run

    apachectl -S
    

    When I run the above command, the domain name with the problem was listed as the default website. I changed 000-default.conf, no domain name was specified in the file. The problem was because the hostname of the server is the same as the domain name I was trying to set up.

    To fix this, I change the hostname of the webserver with the command

    hostname-ctl set-hostname NEW_HOSTNAME_HERE
    

    Restart Apache web server

    systemctl restart apache2
    

    See Apache

  • Running Apache VirtualHost under separate user with mpm-itk

    Running Apache VirtualHost under separate user with mpm-itk

    mpm-itk allow you to run Apache VirtualHost under a specific user/group instead of under the Apache user/group. On Debian/Ubuntu Apache web server is run under user www-data. When you host multiple websites under an Apache server, running all sites under the same www-data user allows a hacker to access files of other sites if one of the sites is hacked. Having apache VirtualHost run as it own user give user-level isolation for each of your website. This also avoids permission-related errors due to apache running as a different user than the user you use to upload the files.

    mpm-itk is non-threaded, it works file with mod_php. It works very similarly to mod_ruid2, which is removed from the latest Debian due to a security issue.

    On Debian/Ubuntu, you can install it with

    apt install libapache2-mpm-itk
    

    During the installation, the apache module gets enabled by default, you can enable/disable it with command

    a2dismod mpm_itk
    a2enmod mpm_itk
    

    To activate mpm-itk, all you need to do is add the following code to the Apache VirtualHost entry of your website.

    
        AssignUserID USERNAME GROUP
    
    

    I normally create a user with the command

    useradd -m --shell /bin/bash --home /home/DOMAIN_NAME USERNAME
    

    Then create a VirtualHost like the following

    vi /etc/apache2/sites-available/DOMAIN_NAME.conf
    

    Add

    
        ServerName DOMAIN_NAME
        ServerAlias www.DOMAIN_NAME
        ServerAdmin info@DOMAIN_NAME
        DocumentRoot /home/DOMAIN_NAME/html
        CustomLog ${APACHE_LOG_DIR}/DOMAIN_NAME.log combined
        ErrorLog ${APACHE_LOG_DIR}/DOMAIN_NAME-error.log
        Header always append X-Frame-Options SAMEORIGIN
        
            AssignUserID USERNAME USERNAME
        
        
            Options All -Indexes
            AllowOverride All
            Require all granted
            Order allow,deny
            allow from all
        
    
    

    Enable VirtialHost with

    a2ensite DOMAIN_NAME
    

    Create website folders

    mkdir /home/DOMAIN_NAME/html/
    chown -R USERNAME:USERNAME /home/DOMAIN_NAME/
    chmod -R 755 /home/DOMAIN_NAME/
    

    Restart Apache webserver

    systemctl restart apache2
    

    Back to Apache

  • Apache Location

    Apache Location

    To only allow requests from a specific IP to a location, use

    
      Require ip 59.92.71.53 51.38.246.115
    
    
    
    
      Require ip 59.92.71.53 51.38.246.115
    
    

    Add custom headers based on location

    
      Require ip 59.92.71.53 51.38.246.115
      Header edit Location ^/browse /internal/stream/browse
               AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text/html text/plain text/xml
               Substitute "s#\"/(fields|browse|machine|entries)#\"/internal/stream/$1#i"
    
    
  • Apache Proxy

    Apache Proxy

    To proxy requests in Apache

    <VirtualHost *:80>
    ServerName youdomain.com
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass "/" "http://localhost:9292/"
    ProxyPassReverse "/" "http://localhost:9292/"
    </VirtualHost>​

    The ProxyPreserveHost directive in Apache’s configuration is used in the context of a reverse proxy setup. When this directive is set to On, it tells Apache to pass the original Host header from the client request to the proxied server

    ProxyRequests Off – This directive turns off forward proxying. In forward proxying, the proxy server forwards client requests to the internet. By setting ProxyRequests Off, you are ensuring that Apache only acts as a reverse proxy, not a forward proxy.

    To proxy on cpanel server, use

    mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/USERNAME/DOMAIN.EXTN/
    vi /etc/apache2/conf.d/userdata/ssl/2_4/USERNAME/DOMAIN.EXTN/proxy.conf

    Add the following to the proxy.conf, change ports as needed

    ProxyPreserveHost On
    ProxyPass "/" "http://localhost:9292/"
    ProxyPassReverse "/" "http://localhost:9292/"

    Rebuild Apache config

    /scripts/rebuildhttpdconf
    service httpd restart

  • 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

  • React/Angular Application showing 404 error on refresh

    On Apache server, React Application works fine, but if you refresh a page, it shows 404 error. This is because the application use BrowserRouter. To fix the error, create a .htaccess file with following content

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]
    

    Put this on the folder where your application index.html is or in Apache Virtual Host entry.

    See htaccess

  • Restart Apache if failed

    This bash script check web site every 5 minutes, restart the web server if web site is not responding.

    Create a file

    mkdir /usr/serverok
    vi /usr/serverok/check_httpd.sh
    

    Add

    #!/bin/bash
    # Author: Yujin Boby
    # Web: https://serverok.in
    # Email: [email protected]
    
    /usr/bin/wget --tries=1 --timeout=30 -O /dev/null https://YOUR_DOMAIN.EXTN/
    
    if [ $? -ne 0 ]; then
        systemctl restart httpd
        datetime=`date "+%Y%m%d %H:%M:%S"`
        echo $datetime "failure">>/var/log/sok-web.log
    fi
    

    In above code, replace https://YOUR_DOMAIN.EXTN/ with your sites actual url.

    Make it executable

    chmod 755 /usr/serverok/check_httpd.sh
    

    Create a cronjob

    crontab -e
    

    Add

    */5 * * * *  /usr/serverok/check_httpd.sh > /dev/null 2>&1
    

    See Server Monitoring

  • 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

  • Fix Canonical URL using .htaccess

    When you host a web site, many web hosting providers allow your web site access using multiple urls like yourdomain.com, www.yourdomain.com. If you have a dedicated IP address, your web site become available on the IP address also. It is bad for SEO to have a site available in multiple URLs. If search engine index your web site contents using differnt URLs, that will affect your page ranking.

    To ge beter search ranking, you need to deicde which URL you want search engines to index. This can be with www or with out www, it is more of a personal choice. In my case, i decided to use with out www (https://serverok.in) to make URL shorter. Once you decided the URL you need google to index, you need to redirect all other URLs used to access your web site to this URL. This is called Canonical url.

    On Apache web server, you can do this using .htacess file. Create an .htaccess file with following content

    RewriteEngine on
    RewriteCond %{SERVER_NAME} !=www.yourdomain.com
    RewriteRule ^ https://www.yourdomain.com%{REQUEST_URI} [END,NE,R=permanent]
    

    In above code, replace www.yourdomain.com with the Canonical URL of your web site. If you don’t use www, then remove the www from the redirect code. What the code doing is check if visitor is using proper Canonical URL to access your web site, if not redirect it to Canonical url. In third line, we redirect to https, if you don’t have SSL installed, change it to “http” instead of “https”.

    See redirect