25.7. Apache HTTP Server
Poprzedni | Rozdział 25. Network Servers | Następny

## 25.7. Apache HTTP Server

Contributed by Murray Stokely.

### 25.7.1. Overview

FreeBSD is used to run some of the busiest web sites in the world. The majority of web servers on the Internet are using the Apache HTTP Server. Apache software packages should be included on your FreeBSD installation media. If you did not install Apache when you first installed FreeBSD, then you can install it from the `www/apache13` or `www/apache20` port.

Once Apache has been installed successfully, it must be configured.

### Uwaga:

This section covers version 1.3.X of the Apache HTTP Server as that is the most widely used version for FreeBSD. Apache 2.X introduces many new technologies but they are not discussed here. For more information about Apache 2.X, please see http://httpd.apache.org/.

### 25.7.2. Configuration

The main Apache HTTP Server configuration file is installed as `/usr/local/etc/apache/httpd.conf` on FreeBSD. This file is a typical UNIX® text configuration file with comment lines beginning with the `#` character. A comprehensive description of all possible configuration options is outside the scope of this book, so only the most frequently modified directives will be described here.

`ServerRoot "/usr/local"`

  This specifies the default directory hierarchy for the Apache installation. Binaries are stored in the `bin` and `sbin` subdirectories of the server root, and configuration files are stored in `etc/apache`.

`ServerAdmin you@your.address`

  The address to which problems with the server should be emailed. This address appears on some server-generated pages, such as error documents.

`ServerName www.example.com`

  `ServerName` allows you to set a host name which is sent back to clients for your server if it is different to the one that the host is configured with (i.e., use `www` instead of the host's real name).

`DocumentRoot "/usr/local/www/data"`

  `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.

It is always a good idea to make backup copies of your Apache configuration file before making changes. Once you are satisfied with your initial configuration you are ready to start running Apache.

### 25.7.3. Running Apache

Apache does not run from the inetd super server as many other network servers do. It is configured to run standalone for better performance for incoming HTTP requests from client web browsers. A shell script wrapper is included to make starting, stopping, and restarting the server as simple as possible. To start up Apache for the first time, just run:

`#` `/usr/local/sbin/apachectl start`

You can stop the server at any time by typing:

`#` `/usr/local/sbin/apachectl stop`

After making changes to the configuration file for any reason, you will need to restart the server:

`#` `/usr/local/sbin/apachectl restart`

To restart Apache without aborting current connections, run:

`#` `/usr/local/sbin/apachectl graceful`

Additional information available at apachectl(8) manual page.

To launch Apache at system startup, add the following line to `/etc/rc.conf`:

apache_enable="YES"

If you would like to supply additional command line options for the Apache `httpd` program started at system boot, you may specify them with an additional line in `rc.conf`:

apache_flags=""

Now that the web server is running, you can view your web site by pointing a web browser to `http://localhost/`. The default web page that is displayed is `/usr/local/www/data/index.html`.

### 25.7.4. Virtual Hosting

Apache supports two different types of Virtual Hosting. The first method is Name-based Virtual Hosting. Name-based virtual hosting uses the clients HTTP/1.1 headers to figure out the hostname. This allows many different domains to share the same IP address.

To setup Apache to use Name-based Virtual Hosting add an entry like the following to your `httpd.conf`:

NameVirtualHost *

If your webserver was named `www.domain.tld` and you wanted to setup a virtual domain for `www.someotherdomain.tld` then you would add the following entries to `httpd.conf`:

<VirtualHost *> ServerName www.domain.tld DocumentRoot /www/domain.tld </VirtualHost> <VirtualHost *> ServerName www.someotherdomain.tld DocumentRoot /www/someotherdomain.tld </VirtualHost>

Replace the addresses with the addresses you want to use and the path to the documents with what you are using.

For more information about setting up virtual hosts, please consult the official Apache documentation at: http://httpd.apache.org/docs/vhosts/.

### 25.7.5. Apache Modules

There are many different Apache modules available to add functionality to the basic server. The FreeBSD Ports Collection provides an easy way to install Apache together with some of the more popular add-on modules.

#### 25.7.5.1. mod_ssl

The mod_ssl module uses the OpenSSL library to provide strong cryptography via the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS v1) protocols. This module provides everything necessary to request a signed certificate from a trusted certificate signing authority so that you can run a secure web server on FreeBSD.

If you have not yet installed Apache, then a version of Apache 1.3.X that includes mod_ssl may be installed with the `www/apache13-modssl` port. SSL support is also available for Apache 2.X in the `www/apache20` port, where it is enabled by default.

#### 25.7.5.2. Dynamic Websites with Perl & PHP

In the past few years, more businesses have turned to the Internet in order to enhance their revenue and increase exposure. This has also increased the need for interactive web content. While some companies, such as Microsoft®, have introduced solutions into their proprietary products, the open source community answered the call. Two options for dynamic web content include mod_perl & mod_php.

##### 25.7.5.2.1. mod_perl

The Apache/Perl integration project brings together the full power of the Perl programming language and the Apache HTTP Server. With the mod_perl module it is possible to write Apache modules entirely in Perl. In addition, the persistent interpreter embedded in the server avoids the overhead of starting an external interpreter and the penalty of Perl start-up time.

mod_perl is available a few different ways. To use mod_perl remember that mod_perl 1.0 only works with Apache 1.3 and mod_perl 2.0 only works with Apache 2. mod_perl 1.0 is available in `www/mod_perl` and a statically compiled version is available in `www/apache13-modperl`. mod_perl 2.0 is avaliable in `www/mod_perl2`.

##### 25.7.5.2.2. mod_php

Written by Tom Rhodes.

PHP, also known as „PHP: Hypertext Preprocessor” is a general-purpose scripting language that is especially suited for Web development. Capable of being embedded into HTML its syntax draws upon C, Java™, and Perl with the intention of allowing web developers to write dynamically generated webpages quickly.

To gain support for PHP5 for the Apache web server, begin by installing the `www/mod_php5` port.

This will install and configure the modules required to support dynamic PHP applications. Check to ensure the following sections have been added to `/usr/local/etc/apache/httpd.conf`:

LoadModule php5_module libexec/apache/libphp5.so
AddModule mod_php5.c <IfModule mod_php5.c> DirectoryIndex index.php index.html </IfModule> <IfModule mod_php5.c> AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps </IfModule>

Once completed, a simple call to the `apachectl` command for a graceful restart is needed to load the PHP module:

`#` `apachectl graceful`

The PHP support in FreeBSD is extremely modular so the base install is very limited. It is very easy to add support using the `lang/php5-extensions` port. This port provides a menu driven interface to PHP extension installation. Alternatively, individual extensions can be installed using the appropriate port.

For instance, to add support for the MySQL database server to PHP5, simply install the `databases/php5-mysql` port.

After installing an extension, the Apache server must be reloaded to pick up the new configuration changes:

`#` `apachectl graceful`

Poprzedni | Początek rozdziału | Następny
25.6. Domain Name System (DNS) | Spis treści | 25.8. File Transfer Protocol (FTP)

All FreeBSD documents are available for download at http://ftp.FreeBSD.org/pub/FreeBSD/doc/

Questions that are not answered by the documentation may be sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.