Guide On How To Configure The Apache 24 Httpd Conf File On FreeBSD

Guide On How To Configure The Apache 24 Httpd Conf File On FreeBSD 2017-08-04T09:39:29+02:00

A Step By Step Guide On How To How To Configure The Apache 2.4 Httpd Conf File On FreeBSD
Oh man there are so many ways to configure this. Books have been written some good, some bad and probably some burned as well. I been giving this a lot of thought an decided to stick with a quick guide in order to get you running.

Apache works “Out Of The Box”. Just add a real ServerName or IP Address in /usr/local/etc/apache24/httpd.conf and you’re basically set.

The line you are looking for is this one.

#ServerName www.example.com:80

Change it to a “Real Domain Name” or IP address if you don’t have a real domain name use the name you have set up in your host file or the name that is being used in your rc.conf file. I’m using “myserver.local” as an example here.

ServerName myserver.local:80

Now the fun part begins when you want to run php as well. The “stock” configuration does not have you covered and even installing php will not alter all settings you need for php in your httpd.conf file. So if you would like to serve php files some adjustments are required.

First things first. Let’s set the apache ServerName I already described the procedure earlier, but here it is again. The line you are looking for is this one.

#ServerName www.example.com:80

Change it to a “Real Domain Name” or IP address. I’m using “myserver.local” as an example here.

ServerName myserver.local:80

Next when you install apache it does not enable mod_rewrite even though it was compiled with mod_rewrite. Let’s enable this first. Find this line.

#LoadModule rewrite_module libexec/apache24/mod_rewrite.so

And remove the # in front of the line like this.

LoadModule rewrite_module libexec/apache24/mod_rewrite.so

Next if you followed my guide om how to install apache with php7, you will have noticed that I’m using the “older” path for cgi-bin and we need to adjust the httpd.conf file accordingly. Fint this line.

ScriptAlias /cgi-bin/ "/usr/local/www/apache24/cgi-bin/"

And change it to

ScriptAlias /cgi-bin/ "/usr/local/www/cgi-bin/"

Next find these lines.

   
    AllowOverride None
    Options None
    Require all granted

And change them like this.


    AllowOverride None
    Options None
    Require all granted

Now let’s tell apache to serve php pages as well. Find these lines.

# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#

    DirectoryIndex index.html

#

Adjust these lines to reflect what type of pages this server should serve.

# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#

    DirectoryIndex index.php index.html index.htm default.htm default.html

#

The order“Does Matter”. Be aware if you have 1 index.php and 1 default.html in a web directory the pages will be served in the above order. This means if you want the webserver to show the default.html page it will not comply because there is a index.php page in there as well. The above configuration clearly states that the index.php file should be served first.

Right now some browsers may have trouble reading CMS systems CSS files and parse them correctly. There are 2 ways to solve this. Create a .htaccess file for each website using a CMS or fix this globally like below. Find this line.

    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.

And add the following lines.

    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
     AddType application/x-httpd-php .php
     AddType application/x-httpd-php-source .phps
     AddType text/css .css
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.

Allmost there.

The apache folder also contains an “extra” folder. There are some files we need to adjust in there.

First let us tweak ServerSignature to Off and ServerTokens to Prod. That way apache will stop displaying version number when it comes up with an error for some reason. We need to edit this file. /usr/local/etc/apache24/extra/httpd-default.conf

In this file find the following lines.

ServerTokens Full

And change them to the following.

ServerTokens Prod

Optional Reversed IP addresses in log files start:

While in the same file you can decide whether to enable looking up IP addresses and display the reveres IP address in the log files. This will require a bit more juice though as apache will need to do a lookup every time someone visits a site. In order to enable find this line.

HostnameLookups Off

And enable hostname lookup by setting it to on like this.

HostnameLookups On

Optional Reversed IP addresses in log files stop:

If you wonder why it doesn’t work it’s because the default configuration in apache does not load /usr/local/etc/apache24/extra/httpd-default.conf out of the box. We need to tell apache to load it everytime we start or restart apache. This is done in /usr/local/etc/apache24/httpd.conf

Find these lines.

# Various default settings
#Include etc/apache24/extra/httpd-default.conf

And remove the # mark for the line loading the httpd-default.conf file like this.

# Various default settings
Include etc/apache24/extra/httpd-default.conf

Also if you run virtual hosts and would like to use the httpd-vhosts.conf for this you need to uncomment this as well. Find this line.

# Virtual hosts
#Include etc/apache24/extra/httpd-vhosts.conf

And as before remove the # mark telling apache to load the httpd-vhosts.conf file like this.

# Virtual hosts
Include etc/apache24/extra/httpd-vhosts.conf

Once all is done restart apache for the changes to take effect.

apachectl restart

And we are done here.

Spell checkers don’t – Grammar checkers don’t either.