Apache Configuration
Quick Recipes for using Apache
Contents
The default configuration for a fresh Apache2 installation is usually stored in httpd.conf. On OpenSuSE this is found in "/etc/apache2/". On Fedora the file is found in "/etc/httpd/conf/". Additional configurations are traditionally stored in ".conf" files, but the server needs to know where to find these. On OpenSuSE to include additional configurations files edit /etc/sysconfig/apache2, adding the following:
APACHE_CONF_INCLUDE_FILES="/etc/apache2/httpd.conf.local"
Alternatively, for a Fedora installation, /etc/httpd/conf/httpd.conf will already have an include statement, to which you can append an additional Include:
Include conf.d/*.conf
#Additional include for my local experiments
Include conf.d/httpd.conf.local
When you create an additional configuration in a ".conf" file, add an Include statement to the httpd.conf.local with the full path to the configuration file
Include /path/to/my/custom/config.conf
The additional configuration files can be stored anywhere on your filesystem, but they must be accessible by the user running the Apache server, as must the content being server. All the directories in the path must therefore have +x permissions for apache to be able to access the content. On Fedora, if you are serving content from your home directory, you will need to grant +x permissions on /home and the appropriate subdirectories. If you are using SELinux, you will also need to enable "Allow httpd to read user content"
Serve Static Content
Top BottomCreate a project folder (myweb/drdsl), then create an apache configuration file (myweb/drdsl/etc/httpd.conf):
Alias /drdsl/ /home/dr00/myweb/drdsl/html/ <Directory /home/dr00/myweb/drdsl/html/> Options Multiviews Indexes FollowSymlinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.1 </Directory>
Add an include statement for this file in "httpd.conf.local":
Include /home/dr00/myweb/drdsl/etc/httpd.conf
Now you can place static html files in myweb/drdsl/html/ and access them via the server as http://localhost/drdsl/filename.html
Explanations
- <Directory /home/dr00/myweb/drdsl/html/>
- The directory block allows you to set properties for a named directory. Using '/' as the directory name, sets the default values for all directories.
- Options Multiviews
- Allows content negotiation from client, for example specifying language preferences in 'GET' request by setting 'Accept-Language' values.
- Options Indexes
- Sets the default action for when a request is made for a directory rather the a filename. Default filenames to return for a directory request are specified with 'DirectoryIndex'. If no match for DirectoryIndex files are found, then 'Options Indexes' will allow Apache to return a table of contents for the directory.
- Options FollowSymlinks
- The server will follow symbolic links but retains pathname to match against <Directory> directive
- AllowOverride
- Determines which settings can be modified by values in .htaccess files. 'None' forbids any settings being altered. Other values: 'All', 'AuthConfig' (authentication), 'FileInfo', 'Indexes', 'Limit' (access priviledges), and 'Options'.
- Order Deny, Allow
- If a request matches a <Deny> directive the request is denied unless it also matches an <Allow> directive. If neither are matched, then the request is allowed.
- Order Allow, Deny
- If a request matches an <Allow> directive it is allowed unless is also matches a <Deny> directive. If neither are matched, then the request is rejected
Alternatively, serve files from filesystem by creating a something.conf file in the /etc/apache2/conf.d/ directory as follows:
Alias /pydoc "/usr/share/doc/packages/python/html/python-docs-html" <Directory "/usr/share/doc/packages/python/html/python-docs-html"> AllowOverride None Order allow,deny Allow from all </Directory>
Enable CGI Scripts
Top BottomCreate a project folder (myweb/drdsl/cgi-bin) for your scripts, then create an apache configuration file (myweb/drdsl/etc/script.conf):
Alias /drdsl-bin/ /home/dr00/myweb/drdsl/cgi-bin/ <Location /drdsl-bin > SetHandler cgi-script Order deny,allow Deny from all Allow from 127.0.0.1 Options Indexes ExecCGI </Location>
Add an include statement for this file in /etc/apache2/http.conf.local:
Include /home/dr00/myweb/drdsl/etc/script.conf
Scripts should now be accessible via http://localhost/drdsl-bin/filename.cgi,but you will need to ensure that you add execute permissions for the group running apache (chmod 755).
Explanation
- <Location /drdsl-bin>
- The location block allows you to apply directives by URL. In this instance the URL is defined in the preceding 'Alias' directive. Location directives are processed after Directory, .htaccess and Files.
- SetHandler
- Associates a 'handler' to specified files. Setting this value to 'cgi-script' forces identified files to be treated as cgi-scripts and therefore handled by mod_cgi.
- Option ExecCGI
- Execution of CGI scripts is permitted
Mod Perl
Top BottomTo configure perl scripts to run under mod_perl
Alias /drdsl-perl/ /home/dr00/myweb/drdsl/cgi-perl/ <Directory /home/dr00/myweb/drdsl/cgi-perl> SetHandler perl-script PerlResponseHandler ModPerl::Registry PerlSendHeader on Options ExecCGI order deny,allow deny from all allow from 127.0.0.1 </Directory>
You will need to enable add 'perl' to the list of APACHE_MODULES in /etc/sysconfig/apache2, and also remember to 'INCLUDE' the configuration file in /etc/apache2/httpd.conf.local:
Include /home/dr00/myweb/drdsl/etc/mod_perl.conf
Explanation
- <SetHandler perl-script>
- Files in identified by this block will be treated as perl scripts.
- <PerlResponseHandler ModPerl::Registry>
- Enables registry scripts, that is perl scripts will be run by the ModPerl::Registry module.
Enable Embedded PHP
Top Bottominstall apache2-mod_php5. Create a file called hellophp.php in your document root directory:
<?php phpinfo(); ?>
