HTTPD - Apache2 Web Server - in - Ubuntu - 11.04
HTTPD - Apache2 Web Server - in - Ubuntu - 11.04
Official Documentation
Previous | Next
Apache is the most commonly used Web Server on Linux systems. Web Servers are used to serve Web Pages requested by client computers. Clients typically request and view Web Pages using Web Browser applications such as Firefox, Opera, or Mozilla. Users enter a Uniform Resource Locator (URL) to point to a Web server by means of its Fully Qualified Domain Name (FQDN) and a path to the required resource. For example, to view the home page of the Ubuntu Web site a user will enter only the FQDN. To request specific information about paid support, a user will enter the FQDN followed by a path. The most common protocol used to transfer Web pages is the Hyper Text Transfer Protocol (HTTP). Protocols such as Hyper Text Transfer Protocol over Secure Sockets Layer (HTTPS), and File Transfer Protocol (FTP), a protocol for uploading and downloading files, are also supported. Apache Web Servers are often used in combination with the MySQL database engine, the HyperText Preprocessor (PHP) scripting language, and other popular scripting languages such as Python and Perl. This configuration is termed LAMP (Linux, Apache, MySQL and Perl/Python/PHP) and forms a powerful and robust platform for the development and deployment of Web-based applications. Installation Configuration HTTPS Configuration References
Installation
The Apache2 web server is available in Ubuntu Linux. To install Apache2: 1. At a terminal prompt enter the following command: s u d oa p t g e ti n s t a l la p a c h e 2
Configuration
Apache2 is configured by placing directives in plain text configuration files. These directives are separated between the following files and directories: 1. apache2.conf: the main Apache2 configuration file. Contains settings that are global to Apache2. 2. conf.d: contains configuration files which apply globally to Apache2. Other packages that use Apache2 to serve content may add files, or symlinks, to this directory. 3. envvars: file where Apache2 environment variables are set. 4. httpd.conf: historically the main Apache2 configuration file, named after the httpd daemon. The file can be used for user specific configuration options that globally effect Apache2. 5. mods-available: this directory contains configuration files to both load modules and configure them. Not all modules will have specific configuration files, however. 6. mods-enabled: holds symlinks to the files in / e t c / a p a c h e 2 / m o d s a v a i l a b l e . When a module configuration file is symlinked it will be enabled the next time apache2 is restarted. 7. ports.conf: houses the directives that determine which TCP ports Apache2 is listening on. 8. sites-available: this directory has configuration files for Apache2 Virtual Hosts. Virtual Hosts allow Apache2 to be configured for multiple sites that have separate configurations. 9. sites-enabled: like mods-enabled, s i t e s e n a b l e dcontains symlinks to the / e t c / a p a c h e 2 / s i t e s a v a i l a b l edirectory. Similarly when a configuration file in sites-available is symlinked, the site configured by it will be active once Apache2 is
https://help.ubuntu.com/11.04/serverguide/httpd.html
1/5
10/3/12
restarted.
In addition, other configuration files may be added using the Include directive, and wildcards can be used to include many configuration files. Any directive may be placed in any of these configuration files. Changes to the main configuration files are only recognized by Apache2 when it is started or restarted. The server also reads a file containing mime document types; the filename is set by the TypesConfig directive, and is / e t c / m i m e . t y p e sby default.
Basic Settings
This section explains Apache2 server essential configuration parameters. Refer to the Apache2 Documentation for more details. 1. Apache2 ships with a virtual-host-friendly default configuration. That is, it is configured with a single default virtual host (using the VirtualHost directive) which can modified or used as-is if you have a single site, or used as a template for additional virtual hosts if you have multiple sites. If left alone, the default virtual host will serve as your default site, or the site users will see if the URL they enter does not match the ServerName directive of any of your custom sites. To modify the default virtual host, edit the file / e t c / a p a c h e 2 / s i t e s a v a i l a b l e / d e f a u l t . The directives set for a virtual host only apply to that particular virtual host. If a directive is set server-wide and not defined within the virtual host settings, the default setting is used. For example, you can define a Webmaster email address and not define individual email addresses for each virtual host. If you wish to configure a new virtual host or site, copy that file into the same directory with a name you choose. For example: s u d oc p/ e t c / a p a c h e 2 / s i t e s a v a i l a b l e / d e f a u l t/ e t c / a p a c h e 2 / s i t e s a v a i l a b l e / m y n e w s i t e Edit the new file to configure the new site using some of the directives described below. 2. The ServerAdmin directive specifies the email address to be advertised for the server's administrator. The default value is webmaster@localhost. This should be changed to an email address that is delivered to you (if you are the server's administrator). If your website has a problem, Apache2 will display an error message containing this email address to report the problem to. Find this directive in your site's configuration file in /etc/apache2/sites-available. 3. The Listen directive specifies the port, and optionally the IP address, Apache2 should listen on. If the IP address is not specified, Apache2 will listen on all IP addresses assigned to the machine it runs on. The default value for the Listen directive is 80. Change this to 127.0.0.1:80 to cause Apache2 to listen only on your loopback interface so that it will not be available to the Internet, to (for example) 81 to change the port that it listens on, or leave it as is for normal operation. This directive can be found and changed in its own file, / e t c / a p a c h e 2 / p o r t s . c o n f 4. The ServerName directive is optional and specifies what FQDN your site should answer to. The default virtual host has no ServerName directive specified, so it will respond to all requests that do not match a ServerName directive in another virtual host. If you have just acquired the domain name ubunturocks.com and wish to host it on your Ubuntu server, the value of the ServerName directive in your virtual host configuration file should be ubunturocks.com. Add this directive to the new virtual host file you created earlier (/ e t c / a p a c h e 2 / s i t e s a v a i l a b l e / m y n e w s i t e ). You may also want your site to respond to www.ubunturocks.com, since many users will assume the www prefix is appropriate. Use the ServerAlias directive for this. You may also use wildcards in the ServerAlias directive. For example, the following configuration will cause your site to respond to any domain request ending in .ubunturocks.com. S e r v e r A l i a s * . u b u n t u r o c k s . c o m 5. The DocumentRoot directive specifies where Apache2 should look for the files that make up the site. The default value is /var/www. No site is configured there, but if you uncomment the RedirectMatch directive in / e t c / a p a c h e 2 / a p a c h e 2 . c o n frequests will be redirected to /var/www/apache2-default where the default Apache2 site awaits. Change this value in your site's virtual host file, and remember to create that directory if necessary! Enable the new VirtualHost using the a2ensite utility and restart Apache2: s u d oa 2 e n s i t em y n e w s i t e s u d o/ e t c / i n i t . d / a p a c h e 2r e s t a r t Be sure to replace mynewsite with a more descriptive name for the VirtualHost. One method is to name the file after the ServerName directive of the VirtualHost. Similarly, use the a2dissite utility to disable sites. This is can be useful when troubleshooting configuration problems with multiple VirtualHosts: s u d oa 2 d i s s i t em y n e w s i t e
https://help.ubuntu.com/11.04/serverguide/httpd.html
2/5
10/3/12
Default Settings
This section explains configuration of the Apache2 server default settings. For example, if you add a virtual host, the settings you configure for the virtual host take precedence for that virtual host. For a directive not defined within the virtual host settings, the default value is used. 1. The DirectoryIndex is the default page served by the server when a user requests an index of a directory by specifying a forward slash (/) at the end of the directory name. For example, when a user requests the page http://www.example.com/this_directory/, he or she will get either the DirectoryIndex page if it exists, a server-generated directory list if it does not and the Indexes option is specified, or a Permission Denied page if neither is true. The server will try to find one of the files listed in the DirectoryIndex directive and will return the first one it finds. If it does not find any of these files and if Options Indexes is set for that directory, the server will generate and return a list, in HTML format, of the subdirectories and files in the directory. The default value, found in / e t c / a p a c h e 2 / m o d s a v a i l a b l e / d i r . c o n fis "index.html index.cgi index.pl index.php index.xhtml index.htm". Thus, if Apache2 finds a file in a requested directory matching any of these names, the first will be displayed. 2. The ErrorDocument directive allows you to specify a file for Apache2 to use for specific error events. For example, if a user requests a resource that does not exist, a 404 error will occur, and per Apache2's default configuration, the file / u s r / s h a r e / a p a c h e 2 / e r r o r / H T T P _ N O T _ F O U N D . h t m l . v a r will be displayed. That file is not in the server's DocumentRoot, but there is an Alias directive in / e t c / a p a c h e 2 / a p a c h e 2 . c o n fthat redirects requests to the /error directory to / u s r / s h a r e / a p a c h e 2 / e r r o r / . To see a list of the default ErrorDocument directives, use this command: g r e pE r r o r D o c u m e n t/ e t c / a p a c h e 2 / a p a c h e 2 . c o n f 3. By default, the server writes the transfer log to the file / v a r / l o g / a p a c h e 2 / a c c e s s . l o g . You can change this on a persite basis in your virtual host configuration files with the CustomLog directive, or omit it to accept the default, specified in / e t c / a p a c h e 2 / a p a c h e 2 . c o n f . You may also specify the file to which errors are logged, via the ErrorLog directive, whose default is / v a r / l o g / a p a c h e 2 / e r r o r . l o g . These are kept separate from the transfer logs to aid in troubleshooting problems with your Apache2 server. You may also specify the LogLevel (the default value is "warn") and the LogFormat (see / e t c / a p a c h e 2 / a p a c h e 2 . c o n ffor the default value). 4. Some options are specified on a per-directory basis rather than per-server. Options is one of these directives. A Directory stanza is enclosed in XML-like tags, like so: < D i r e c t o r y / v a r / w w w / m y n e w s i t e > . . . < / D i r e c t o r y > The Options directive within a Directory stanza accepts one or more of the following values (among others), separated by spaces: 1. ExecCGI - Allow execution of CGI scripts. CGI scripts are not executed if this option is not chosen. Most files should not be executed as CGI scripts. This would be very dangerous. CGI scripts should kept in a directory separate from and outside your DocumentRoot, and only this directory should have the ExecCGI option set. This is the default, and the default location for CGI scripts is / u s r / l i b / c g i b i n . 2. Includes - Allow server-side includes. Server-side includes allow an HTML file to include other files. This is not a common option. See the Apache2 SSI HOWTO for more information. 3. IncludesNOEXEC - Allow server-side includes, but disable the #exec and #include commands in CGI scripts. 4. Indexes - Display a formatted list of the directory's contents, if no DirectoryIndex (such as index.html) exists in the requested directory. For security reasons, this should usually not be set, and certainly should not be set on your DocumentRoot directory. Enable this option carefully on a per-directory basis only if you are certain you want users to see the entire contents of the directory. 5. Multiview - Support content-negotiated multiviews; this option is disabled by default for security reasons. See the Apache2 documentation on this option. 6. SymLinksIfOwnerMatch - Only follow symbolic links if the target file or directory has the same owner as the link.
httpd Settings
https://help.ubuntu.com/11.04/serverguide/httpd.html
3/5
10/3/12
Apache2 Modules
Apache2 is a modular server. This implies that only the most basic functionality is included in the core server. Extended features are available through modules which can be loaded into Apache2. By default, a base set of modules is included in the server at compile-time. If the server is compiled to use dynamically loaded modules, then modules can be compiled separately, and added at any time using the LoadModule directive. Otherwise, Apache2 must be recompiled to add or remove modules. Ubuntu compiles Apache2 to allow the dynamic loading of modules. Configuration directives may be conditionally included on the presence of a particular module by enclosing them in an <IfModule> block. You can install additional Apache2 modules and use them with your Web server. For example, run the following command from a terminal prompt to install the MySQL Authentication module: s u d oa p t g e ti n s t a l ll i b a p a c h e 2 m o d a u t h m y s q l See the / e t c / a p a c h e 2 / m o d s a v a i l a b l edirectory, for additional modules. Use the a2enmod utility to enable a module: s u d oa 2 e n m o da u t h _ m y s q l s u d o/ e t c / i n i t . d / a p a c h e 2r e s t a r t Similarly, a2dismod will disable a module: s u d oa 2 d i s m o da u t h _ m y s q l s u d o/ e t c / i n i t . d / a p a c h e 2r e s t a r t
HTTPS Configuration
The mod_ssl module adds an important feature to the Apache2 server - the ability to encrypt communications. Thus, when your browser is communicating using SSL, the https:// prefix is used at the beginning of the Uniform Resource Locator (URL) in the browser navigation bar. The mod_ssl module is available in apache2-common package. Execute the following command from a terminal prompt to enable the mod_ssl module: s u d oa 2 e n m o ds s l There is a default HTTPS configuration file in / e t c / a p a c h e 2 / s i t e s a v a i l a b l e / d e f a u l t s s l . In order for Apache2 to provide HTTPS, a certificate and key file are also needed. The default HTTPS configuration will use a certificate and key generated by the ssl-cert package. They are good for testing, but the auto-generated certificate and key should be replaced by a certificate specific to the site or server. For information on generating a key and obtaining a certificate see Certificates To configure Apache2 for HTTPS, enter the following: s u d oa 2 e n s i t ed e f a u l t s s l The directories / e t c / s s l / c e r t sand / e t c / s s l / p r i v a t eare the default locations. If you install the certificate and key in another directory make sure to change SSLCertificateFile and SSLCertificateKeyFile appropriately. With Apache2 now configured for HTTPS, restart the service to enable the new settings:
https://help.ubuntu.com/11.04/serverguide/httpd.html
4/5
10/3/12
References
1. Apache2 Documentation contains in depth information on Apache2 configuration directives. Also, see the apache2-doc package for the official Apache2 docs. 2. See the Mod SSL Documentation site for more SSL related information. 3. O'Reilly's Apache Cookbook is a good resource for accomplishing specific Apache2 configurations. 4. For Ubuntu specific Apache2 questions, ask in the #ubuntu-server IRC channel on freenode.net. 5. Usually integrated with PHP and MySQL the Apache MySQL PHP Ubuntu Wiki page is a good resource. Previous | Next
The material in this document is available under a free license, see Legal for details For information on contributing see the Ubuntu Documentation Team wiki page. To report a problem, visit the bug page for Ubuntu Documentation
https://help.ubuntu.com/11.04/serverguide/httpd.html
5/5