Installing Apache 2.4

From their wiki page: Apache HTTP Server, colloquially called Apache (/əˈpætʃiː/ ə-PA-chee), is free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

Getting Apache installed to get a web server running!

Installing Apache2.4

Make sure you update ports and then run the following commands:


# cd /usr/ports/www/apache24
# make install clean

Configuring apache2

Lets edit the httpd.conf file:


# vi /usr/local/etc/apache24/httpd.conf

Scroll down and change the following settings. The optional settings I will put OPTIONAL before the setting:


OPTIONAL: Listen 80 - You can change this default option if you have more than one apache server running on your network

User www - Changes what user apache runs as

Group www - Changes what group apache runs as

ServerAdmin you@example.com - change you@example.com to your email address.

DocumentRoot "/usr/local/www/apache24/data" - I don't usually use the default path. I put my www documents on a seperate drive.

Directory "/usr/local/www/apache24/data" - Change this to the same path as DocumentRoot (See above)

<Directory /usr/local/www/apache24/> Change this to the root of your vhosts folder

DirectoryIndex index.html index.html.var - add any pages you would use. For instance, add index.php if you use php pages

OPTIONAL: #CustomLog /var/log/httpd-access.log combined - I usually leave this commented unless you want to use this to track users looking at your site

ScriptAlias /cgi-bin/ "/usr/local/www/cgi-bin/" - change this to your cgi-bin path

Directory "/usr/local/www/cgi-bin"> - change this to the same path as ScriptAlias /cgi-bin above

OPTIONAL: This will make your directory listings look a lot better

Include etc/apache22/extra/httpd-autoindex.conf

We now need to tell Apache to run on startup. Please run the following:


# echo 'apache24_enable="YES"' >> /etc/rc.conf

Now lets tell apache to start:


# /usr/local/etc/rc.d/apache24 start

If you get no errors, apache should be running. Look at the page by opening a browser to http://localhost or replace localhost with the IP or the actual hostname of the box. If you went with the DocumentRoot defaults, You will see an apache test page until you get your site up and going. If you are behind a router or firewall, make sure you forward the apache port (Port 80) to the FreeBSD box otherwise you won't be able to get there from here.

Configuring SSL

You have 2 ways to setup apache with ssl. if you have configured qmail with Certbot you will already have the server cert and key already on your system. You will just have to copy the certs over to the proper apache folders as seen below.

If you have your own ssl certs from godaddy or whoever, copy the .pem and .key to the folders listed.

Let's get SSL Configured and Installed:


# mkdir /usr/local/etc/apache24/ssl.key
# mkdir /usr/local/etc/apache24/ssl.crt
# chmod 0700 /usr/local/etc/apache24/ssl.key
# chmod 0700 /usr/local/etc/apache24/ssl.crt

Lets copy the certs to the right place as seen below. Please replace domain.com with your domain :-)


# cd /usr/local/etc/letsencrypt/live/domain.com
# cp fullchain.pem /usr/local/etc/apache24/ssl.crt/domain.com.pem
# cp privkey.pem /usr/local/etc/apache24/ssl.key/domain.com.key

Now to give them the right permissions as well:


# chmod 0400 /usr/local/etc/apache24/ssl.crt/domain.com.crt
# chmod 0400 /usr/local/etc/apache24/ssl.key/domain.com.key

We will now want to copy the default httpd-ssl.conf from the extras folder to the Includes folder:


# cd /usr/local/etc/apache24/extra
# vi httpd-ssl.conf

Now modify the following:


DocumentRoot "/usr/local/www/data" - Change the path to your httpd.conf document root.

ServerName www.example.com:443 - Change www.example.com to your domain name.

ServerAdmin you@example.com Change this to your email address

ErrorLog /var/log/httpd-error.log - You can leave this or comment it out.

TransferLog /var/log/httpd-access.log - You can leave this or comment it out.

SSLCertificateFile "/usr/local/etc/apache24/ssl.crt/domain.com.pem"

SSLCertificateKeyFile "/usr/local/etc/apache24/ssl.key/domain.com.key"

#SSLSessionCache "shmcb:/var/run/ssl_scache(512000)"

Next we need to open up /usr/local/etc/apache24/httpd.conf and uncomment the following lines:


LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so

LoadModule ssl_module libexec/apache24/mod_ssl.so

Include etc/apache24/extra/httpd-ssl.conf

Now run the following:


# /usr/local/etc/rc.d/apache24 restart

The start means it will start in ssl mode to serve both http:// and https:// addresses. This used to be /usr/local/etc/rc.d/apache24 sslstart but that command has been depreciated.

Configuring php for Apache

This section is pretty easy. Just run the following:


# cd /usr/ports/lang/php70
# make install clean

We will want to set the time zone in the php.ini. Lets copy the file over and then edit php.ini.


# cd /usr/local/etc/
# cp php.ini-production php.ini
# vi php.ini

Inside the php.ini set the following. If you're not EST then see the following supported timezones:

http://php.net/manual/en/timezones.php


date.timezone = 'America/New_York'

If you do not set the timezone messages will not appear with a timestamp in dovecot.

Next, we want to configure apache to use php 7.0


# cd /usr/ports/www/mod_php70
# make install clean

Next go to DirectoryIndex and make sure index.php is part of it like so:


DirectoryIndex index.php index.html

Now we need to add the following section to the very end of the http.conf:


<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>

We now need to load the accf http module as seen below.


# echo 'accf_http_load="YES"' >> /boot/loader.conf

Now rather than rebooting the box, we can load the module manually by running:


# kldload accf_http

and then if you start Apache, You won't get that error.

Now run the following command to restart apache.


# /usr/local/etc/rc.d/apache24 restart

You will now have apache with SSL and PHP support!