Apache - Local Installation

Apache is possibly the world's most popular server software. We currently use Apache 2.4.

Official Apache HTTP Server home page

Official Apache Server documentation

Apache's server process is called httpd, short for "HTTP daemon."

NB: Debian Linux, from which Ubuntu is derived, maintains a version of Apache in its standard repositories that has a significantly different configuration scheme from the version provided by the Apache Foundation and available on OS X/MacOS, RHEL, Fedora, etc. Since the VMs run Ubuntu, and you're highly likely to be using OS X, it's important to be aware of this distinction. If you'd like to deploy the website locally to a Debian-derived Linux distribution (e.g. Debian, Ubuntu, or Mint), you have two choices:
  1. Install Debian-style Apache from the normal repositories. In this case, see the pages for Apache on the VMs, which use Ubuntu 14 and might give better guidance on installation and configuration. This is recommended. It's not that hard.
  2. Circumvent package management and install normal Apache by downloading it from apache.org. The guide here should help for configuration, but you're on your own for installation and any potential package conflicts that arise.

Installation

OS X

The following notes were created while installing Apache on OS X 10.11 (El Capitan), though they should be generalizable.

[TO DO: insert notes]

Fedora 23

Apache is preinstalled under /etc/httpd/:

$ httpd -v
Server version: Apache/2.4.18 (Fedora)
Server built:   Jan  4 2016 08:15:18

Apache Configuration

In standard Apache, the global configuration file is httpd.conf. In Debian-style Apache, that file is called apache2.conf. These are found in
OS location
OS X El Capitan /etc/apache2/httpd.conf
Fedora 23 /etc/httpd/conf/httpd.conf
Ubuntu 14/16 /etc/apache2/apache2.conf
Before you edit this file, make a backup of the default.

You'll need to change three things:

1. Identify Apache as localhost

In the global config file (httpd.conf or apache2.conf), look for a line with the ServerName keyword; something like

#ServerName www.example.com:80

commented out. If it's there, uncomment it and change it to

ServerName localhost

This identifies the Apache server as "localhost," allowing your browser to access the local website that it serves using the URL localhost.

On Ubuntu , there may not be a commented ServerName directive in apache2.conf. You can add it there, but a better way is to create an external config file like conf-available/localhost.conf and add it there, since apache2.conf includes config files in conf-enabled/. On the VMs, for example (Ubuntu 14), this is accomplished via the file sites-available/i2u2.conf.

Apache loads its config files when it starts, so changes you make to them don't take effect until then. Enable the new file with
$ a2enconf localhost.conf
and then reload Apache with

OS X, Ubuntu $ sudo service apache2 reload

Fedora 23 $ sudo systemctl reload httpd.service

(use start instead of reload if Apache isn't already running. Use status instead of reload to see if it's running.)

Go ahead and try it! Enter localhost in the address bar of your browser. You should get some form of official "Apache Test Page" that confirms the Apache HTTP Server is functioning.

Apache Configuration: Proxy and AJP

Once Apache is functioning, there are two more configuration changes to make.

2. Enable modules for proxying

Apache will act as a proxy for Tomcat, so we must enable the Apache modules that make this possible. They are mod_proxy (or proxy_module, depending on context) that allows proxying in general and mod_proxy_ajp (or proxy_ajp_module) that specifically allows proxying via AJP, which is what we're using to interface with Tomcat. Fortunately these are highly popular modules, so they may already be enabled with your default installation, or else they'll be easily enabled if not.

For OS X, you'll find a large block of LoadModule statements in httpd.conf that should include the two lines
LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_ajp_module libexec/apache2/mod_proxy_ajp.so

(they won't necessarily be adjacent). Make sure they're uncommented.

For Fedora 23, these modules are loaded via the auxiliary config file /etc/httpd/conf.modules.d/00-proxy.conf which is included in httpd.conf via the command
Include conf.modules.d/*.conf

They are already enabled in the default installation.

For Ubuntu, the module files should appear in mods-available/ (in the form of proxy.load and proxy_ajp.load). They probably will not be listed in mods-enabled/, but check to see. If they aren't, enable them with
$ sudo a2enmod proxy
$ sudo a2enmod proxy_ajp

(you'll be reminded to run $ service apache2 restart to enact the changes).

3. Configure the AJP module

Now we configure the AJP module to interface with Tomcat the way we want. For Apache 2.2 and lower, the desired configuration is given by
ProxyRequests Off
<Proxy *>
    Order deny,allow
    Deny from all
    Allow from localhost
</Proxy>
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

The Order/Allow/Deny directives were deprecated in Apache 2.4 in favor of a simpler Require directive. For Apache 2.4 and later, the configuration is
ProxyRequests Off
<Proxy *>
    Require local
</Proxy>
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009

ProxyRequests Off keeps Apache from being used as a forward proxy, meaning that external clients can't connect to Apache for the purpose of forwarding requests to other servers (this is important).

The block allows only requests from the local environment to use Apache as a proxy; the wildcard * indicates that this block applies to all proxy requests. Note that the directive is Require local, not Require localhost: the local keyword applies to all requests from the local environment, no matter what you put for the ServerName directive in the global configuration (we use ServerName localhost because it's conventional, not because it's required).

ProxyPass activates Apache to work as a reverse proxy, which is the type that enables it to front for Tomcat. External clients are not aware that their connection is being proxied or what kind of negotiations Apache and Tomcat are performing behind the scenes. The first argument is a local path in Apache's namespace, here the root directory "=/=". The second argument is the URL in the "remote" server's namespace, here Tomcat running on the very same computer, to which requests are forwarded. That is, all requests coming in to Apache (requests for any file within its root directory /) are fowarded via AJP to Tomcat's root directory address localhost:8009/, which is the port we configure Tomcat to listen on using AJP in its config file server.xml (see Tomcat Installation).

ProxyPassReverse channels Tomcat's responses back through Apache to the client in the same manner.

As with the ServerName directive, we put additional configuration into separate files loaded by the global config file where possible, rather than into httpd.conf or apache2.conf itself.

For OS X, add a line to httpd.conf that includes external configuration files:

Include /private/etc/apache2/other/*.conf

This will load all .conf files in /etc/apache2/other/ whenever httpd.conf itself is loaded. Now, make such a file for AJP by creating the other/ folder and a file ajp.conf that contains the appropriate block given above.

For Fedora 23, edit the existing proxy configuration file /etc/httpd/conf.modules.d/00-proxy.conf. Below the block of LoadModule directives, copy the appropriate block of Proxy directives given above.

For Ubuntu 14/16, add the appropriate block to the existing config file mods-available/proxy.conf immediately after the sample block that's commented out.

Enable Changes

Remember to reload Apache after changing configuration files. Bear in mind that after you load the Proxy directives for AJP configuration, the Apache test page will no longer appear for localhost because connections are now proxied to the Tomcat, instead. If Tomcat isn't yet working, then you'll get an error when you access localhost (most likely a "Service Unavailable"). If it is working and configured, then you'll get the Tomcat test page.

Reference

Basic commands to start, stop, reload, and check the status of Apache are

OS X, Ubuntu $ sudo service apache2 (start | stop | reload | restart | status)

Fedora 23 $ sudo systemctl (start | stop | reload | restart | status) httpd.service

If you've installed the package www-browser, which the apache2 package officially suggests, then Ubuntu additionally has

Ubuntu $ sudo apachectl (start | stop | reload | restart | status)

-- Main.JoelG - 2016-06-16

Comments

 
Topic revision: r10 - 2017-02-01, JoelG
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback