Go to content Go to navigation Go to search

2. Configuration

July 30th, 2010 by jde

Basic configuration

In the early days there was only one configuration file for Apache (httpd.conf). Over time it has been split into several files, each containing a specific category of directives (httpd-vhosts.conf, httpd-ssl.conf etc, etc). The original httpd.conf is still available at the root of the Apache configuration directory /usr/local/etc/apache22. The others are located in the /extra folder.

Open /usr/local/etc/apache22/httpd.conf and change the following values:

ServerAdmin your_email@address.tld
ServerName www.address.tld:80
ServerRoot "/usr/local"
DocumentRoot "/usr/local/www"
Directory "/usr/local/www"
  AllowOverride None
DirectoryIndex index.php index.html

ServerAdmin: Apache will display this address in the browser if something goes wrong.
ServerName: This is the site that will be displayed if none of the virtual hosts matches the request.
AllowOverride: If set to “None” any .htaccess files will be ignored. If you want maximum performance, you should avoid htaccess files and use the *.conf files instead.
DirectoryIndex: Add filenames which must be recognized as site index files. This is the file Apache will display if the request does not contain a specific page.

Virtual host configuration

The place to add virtual hosts is /usr/local/etc/apache22/extra/httpd-vhosts.conf:

The directives in httpd.conf are global… That is, they are implied for virtual hosts unless another value is specified.
Directives inside a virtual host overwrites any global directive and applies only to the virtual host where it is specified.

The following example is a namebased virtual host. Note the * in the first line. This is where you would put an IP address if you wanted to create an IP-based virtual host.
The former is most widely used since most people only have one IP address for the server.

<VirtualHost *:80>
ServerAdmin webmaster@domain.tld
DocumentRoot /usr/local/www/domain.tld
ServerName www.domain.tld
ErrorLog /usr/local/www/logs/www.domain.tld-error_log
CustomLog /usr/local/www/logs/www.domain.tld-access_log common
</VirtualHost>

This will tell Apache to look in /usr/local/www/domain.tld/ if a request is made for www.domain.tld. Also note ErrorLog and CustomLog – this is where error and access logs are written.

You can add as many virtual hosts as you like. Just enclose each host between a <VirtualHost *:80> and </VirtualHost>

It’s just as easy to create subdomains:

<VirtualHost *:80>
ServerAdmin webmaster@example.tld
DocumentRoot /usr/local/www/mysub.example.tld
ServerName mysub.example.tld
ErrorLog /usr/local/www/logs/mysub.example.tld-error_log
CustomLog /usr/local/www/logs/mysub.example.tld-access_log common
</VirtualHost>

Note: If you plan to use a subdomain for mail accounts, you must create an A record in DNS, unless the main domain is specified in DNS with a wildcard. You must also create an MX record pointing to the subdomain.

Don’t forget to create all the directories referenced in the virtual host (just the directories – you don’t need to create the log files in advance, Apache will create them the first time they are needed).

If you want to prevent directory listings in folders not containing an index file, add this to the virtual host:

<Directory /usr/local/www/domain.tld>
Options -Indexes
</Directory>

You must restart Apache to make any changes take effect.

# apachectl stop
# apachectl start

… or

# apachectl restart

1. Installation

July 29th, 2010 by jde

FreeBSD 8.1
Apache 2.2

Installation

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

You are presented with a lot of options. If you are new to Apache you probably don’t know half of them. But it is safe to just install it with the default selection. You can always reinstall Apache if you missed something.

See if it starts without any complaints:

# rehash
# apachectl start

Start your browser at go to http://your.ip.address.here (e.g http://12.3.4.56). If you see the words “It works!”, you’re good to go :-)

Common startup errors

Error:

apr_sockaddr_info_get() failed for server.example.com
Could not reliably determine the servers fully qualified domain name using 127.0.0.1 for ServerName

Solution:

Configure /etc/hosts by replacing my.domain with your hostname. If you are not sure about your hostname, run the `hostname` command.
So if your hostname is penguin.myservers.com, this is what your hosts file should look like (replace the 12.3.4.56 IP with your public IP address).

::1               localhost localhost.penguin.myservers.com
127.0.0.1         localhost localhost.penguin.myservers.com
12.3.4.56         penguin.myservers.com penguin

Error:

[warn] (2)No such file or directory:Failed to enable the ‘httpready’ Accept Filter 

Solution:

# kldload accf_http

In /boot/loader.conf add this line:

accf_http_load="YES"