apache configuration information


You probably have configured apache multiple times, a lot of times we ignore the inside details behind some important configuration, we just know what needs to be done but we are unaware of finer details.

 


Apache directory structure

Whenever you install apache web server ((yum/apt-get), it will create a directory under /etc/ (used to configure Apache web server environment/settings).

/etc/httpd/ (for rhel/centos…)

/etc/apache2/ (for Ubuntu)

under above directories, you might see some files and directories like:

httpd.conf or apache2.conf  #### has most of the settings – main file for apache

ports.conf #### incoming ports (80, 443 etc), it can be configured

envvars ### for pid file, user, user group, logdir etc..

directorirs – mods-enabled, conf-enabled, sites-enabled, mods-available ,conf-available, sites-available..

all above *enabled directories point to a symlink to corresponding *available dirs (for configuration, modules.. etc).

symlinks/settings can be done manually or they can be controlled by

  • a2enmod/a2dismod
  • a2ensite/a2dissite and
  • a2enconf/a2disconf

you will see multiple references for *enabled directories in httpd.conf.

Directive

Explanation

AliasSometimes you might have files stored outside document root, like files uploaded by users, ftp directory or some files in other filesystem (like nfs etc), you can use 'Alias' directive to point URL to other filesystem
ex.
Alias "/url" "/efs/ftp/"

above thing means anything like www.example.com/url/abc.gzip will internally point to /efs/ftp/abc.gzip

Also check AliasMatch directive which can use regular expressions.
LoadModulesyntax: LoadModule module filename

where filename is path to module fromServerRoot
ErrorDocumentsyntax: ErrorDocument error-code file

Where error-code is like 500, 404 etc and file is name of file to show once error code is triggered.

example

ErrorDocument 404 /not_found.htmlTherefore, if you use an ErrorDocument 401 directive, then it must refer to a local document.

Important Note: Always use local path with ErrorDocument else webserver will redirect (if path starts with http) which will work (client will be redirected to URL) but client will NOT receive error code, client will get redirect code.
ServerRootServerRoot tells the location of apache modules, configuration files...

typically its like /etc/apache2 in ubuntu and /etc/httpd in CentOS.
DocumentRootDocumentRoot specifies where to look for webserver html content! it's the root directory where your webserver will point. You can setup multiple Document root in a single webserver for different virtualhosts!
DirectoryIndexindex.html index.php
Reverse ProxyIn the following example, when clients request documents under the /foo/ directory, the server fetches those documents from the /bar/ directory on internal.example.com and returns them to the client as if they were from the local server.
ProxyPass"/foo/" "http://internal.example.com/bar/"

 


The following lines prevent .htaccess and .htpasswd files from being

# viewed by Web clients.
#

<Files ".ht*">
Require all denied
</Files>


Typical setup – VirtualHost

<VirtualHost *:80>
ServerName www.bitarray.io
ServerAlias bitarray.io
DocumentRoot /var/www/html/
ErrorLog /var/log/error.log
CustomLog /var/log/requests.log combined
</VirtualHost>


How to enable cgi scripts?

<Directory "/var/www/html/cgi/">
    Options +ExecCGI
    AddHandler cgi-script .cgi
</Directory>

Above configuration means, anything ending as .cgi in /var/www/html/cgi can be executed as a script (why? because options handler is ser to +ExecCGI)

Note: LoadModule cgid_module modules/mod_cgid.so (module has to be enabled)


How to restart apache server?

# systemctl restart httpd

or if apachectl is available

#apachectl restart


Redirecting/rewrite URLs

Redirecting URLs is quite popular and is widely used – Here are some use cases

  • You want to redirect because path/URL changed
  • You want to rewrite URLs (like used by WordPress and most of the blogging, forum, content management software etc.. why? When you create a website one option is to create a static page for each URL, another option is to create a single dynamic page which will query Database based on URL like if URL is https://www.bitarray.io/index.py?page=apache&id=2 (when user will type this URL in browser your index.py script will display content based on parameters apache and id=2). This setup doesn’t require rewrite but URL is ugly and is not SEO friendly instead you will prefer https://www.bitarray.io/apache/2 (this requires rewriting URL.. user will type this URL in the browser but internally it will behave like calling https://www.bitarray.io/index.py?page=apache&id=2)

Examples:

in the .htaccess file, you can add

RewriteEngine On
Redirect 301 /xyz.html /category/xyz.html

above will redirect (301, permanent redirect and browser URL will change) to new URL

in .htaccess (treat all .html files as .php files)

RewriteEngine On
<FilesMatch "\.html$">
    ForceType application/x-httpd-php
</FilesMatch>

use directory paths and refer paths to scripts (URL will NOT change in the browser, this is a very popular way of creating pages)

RewriteEngine On
RewriteBase /

RewriteRule ^user/profile/([^/]+)$ /forum/profile.php?username=$1
RewriteRule ^tags/(.*)/page/(.*)$ /forum/tags.php?tag=$1&page=$2

+ There are no comments

Add yours