Installing an Apache web server in FreeBSD

I will show you here how to install an Apache 2.4 web server in FreeBSD using the ports.
The Apache web server is a software that can serve websites and web applications.
If you would like to learn more about the Apache web server here is the link to the Apache project.

FreeBSD basics

First, let me explain about FreeBSD package managers in a nutshell.

FreeBSD has two package management systems: pkg and ports.

What is a port?

A Port is a collection of files designed to automate compiling and installing an application from source code.
The files that comprise a port, contain the following instructions:

  1. Where to download the file(s) and how to extract them.
  2. Are there any patches available? If they are, how to implement them?
  3. Instructions on how to compile.
  4. Finally, install the application utilizing the pkg commands.

What is a Package?

A FreeBSD package is a pre-assembled software that holds the following items:

  • the application,
  •  the config files,
  •  the documentation.

Packages can be managed with the pkg commands. They are often smaller and easier to install than ports.

Note: During installation and setup, I will be using the root account.

Preparations:

Before we start, we should update the port’s collection to receive the latest and most secure code. To do this, I prefer the portsnap command.

#portsnap auto
#whereis apache24
apache24: /usr/ports/www/apache24
# cd /usr/ports/www/apache24

If you want to learn more about ports please check my post FreeBSD Basic Setup and Management of Ports

Compiling and Installing

In the Apache directory, we need to run the make command with the following options: install, clean.
Firstly, the install option performs the following tasks:

  1. download the source code
  2. patch the code
  3. compile the code
  4. create the package
  5. install the package.

In the end, if the installation is successful, the clean command will delete any temp files generated during this process.

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

The command above will compile and install the Apache web server version 2.4.
To automatically start the Apache web server when the system starts, this line needs to be added to the /etc/rc.conf file.

apache24_enable="yes"

It would be great if we get going with the configuration. Create a virtual host.

Configuring the Apache web server

On FreeBSD, apache configuration files are located in /usr/local/etc/apache24

root@test:/usr/local/etc/apache24 # ls
Includes                extra                   httpd.conf.sample       magic.sample            mime.types.sample
envvars.d               httpd.conf              magic                   mime.types              modules.d
root@test:/usr/local/etc/apache24 #

I suggest you take a look at httpd.conf file. Please, take some time to read the comments. This config file is documented in great detail.
As you can see in the comments “DocumentRoot” is where Apache looks for the websites.
NOTE: If you change DocumnetRoot, you are required to make a change to the “Directory” directive.

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/usr/local/www/apache24/data"
<Directory "/usr/local/www/apache24/data">

Let us start our web server and see what we got.

root@test:/usr/local/etc/apache24 # apachectl start
Performing sanity check on apache24 configuration:
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Starting apache24.
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
root@test:/usr/local/etc/apache24 #

Testing the web server

Reflect on that I did not set up the FQDN as the server name in the http.d conf file.
The web server has started with an ip of 127.0.0.1.
To verify that the server is running, I will use the “curl” command.

root@test:/usr/local/etc/apache24 # curl 127.0.0.1
<html><body><h1>It works!</h1></body></html>
root@test:/usr/local/etc/apache24 #

We are done!
A working HTML web server.
Final note: Please remember to make a copy of the httpd.conf file before making any changes to it.