Configuring Apache Web Server on Linux

Embed Size (px)

Citation preview

  • 7/31/2019 Configuring Apache Web Server on Linux

    1/3

    My article Installing Apache Web server on Linux told you where to download Apache and explained how toinstall it on your Linux machine. Now Ill follow up with the basics of configuring the Apache Web server. Illalso show you how to make sure Apache starts automatically at boot time.

    As with many Linux-based programs, configuration for Apache is done through the placement of directives inplain text files. In this case, the files are known as httpd.conf , srm.conf , and access.conf . Their location andimportance may differ based on the particular installation, but the format remains the same. Through thesefiles, you may choose where to serve your Web documents from, what modules are dynamically loaded atstart-up, and even which port Apache will listen on.

    First, lets look at the reasoning behind the three separate configuration files. This has an almost Holy Trinity-like mystique in the Apache community. They are basically a legacy of the NCSA 1.3 Web server and can behandled in many different ways.

    One option is to use httpd.conf as the server-wide configuration, srm.conf as controlling naming andresources, and access.conf as setting access control for the server. Another approach is to use onlyhttpd.conf , which will support any directive that can be placed in either of the other two files. By placing theoptions AccessConfig /dev/null and ResourceConfig /dev/null in httpd.conf , you can tell Apache to look only toit for its configuration.

    Another interesting method is to name directories httpd.conf , srm.conf , and access.conf , respectively. Theserver will automatically parse any files and subdirectories located there into the aforementioned configurationfiles. This will let you break down items such as VirtualHosts to a very specific degree.

    Now that weve looked at why the configuration files exist in their current state, lets see where theyrelocated. Although the Apache root directory will vary depending on installation options, these files will usuallyreside in the /conf subdirectory. For example, if you installed Apache with all defaults, the configuration fileswould be located in /usr/local/apache/conf .

    This does not mean that you must use those particular files every time. The f command-line option allows for Apache to load its configuration from a user-defined file at run time. Also, preconfigured packages fromdistributions such as Red Hat and Debian may install to different directories. Under the current version of Debian, the configuration files can be found in /etc/apache. If youre using Red Hat, you may want to check under /etc/httpd/conf .

    If youve run more than one version of Linux before, you know that the directory structure and file locationscan vary. When in doubt, running locate httpd or locate apache with an updated database of files can behelpful in pinpointing the exact location.

    Directives are entered into the configuration files one per line and are case-sensitive. Placing a backslash atthe end of the command can extend longer entries into multiple lines. If you are not quite sure you enteredthe directives correctly, Apache provides for some basic error checking of your syntax by runningapachectlconfigtest. If Apache sees a problem with a particular command, it will give you the line number.Remember that you must still run apachectl start to initialize the server. Lets take a look at some standard

    Apache directives taken from httpd.conf (which you can open with the text editor of your choice, such as vi orpico ): # ServerType is either inetd or standalone.

    # Port: The port the standalone listens to. For ports < 1023, you will# need httpd to be run as root initially.

    http://www.techrepublic.com/article.jhtml?id=r00220010626noo01.htmhttp://www.techrepublic.com/article.jhtml?id=r00220010626noo01.htmhttp://www.techrepublic.com/article.jhtml?id=r00220010626noo01.htm
  • 7/31/2019 Configuring Apache Web Server on Linux

    2/3

  • 7/31/2019 Configuring Apache Web Server on Linux

    3/3

    #! /bin/bash## apache Start the apache HTTP server.#NAME=apachePATH=/bin:/usr/bin:/sbin:/usr/sbinDAEMON=/usr/sbin/apacheSUEXEC=/usr/lib/apache/suexecPIDFILE=/var/run/$NAME.pidCONF=/etc/apache/httpd.confAPACHECTL=/usr/sbin/apachectl trap "" 1export LANG=C test -f $DAEMON || exit 0test -f $APACHECTL || exit 0 if egrep -q -i "^[[:space:]]*ServerType[[:space:]]+inet" $CONFthen

    exit 0fi case "$1" in

    start)echo -ne "Starting web server: $NAME.\n"$APACHECTL start;;

    stop)

    echo -ne "Stopping web server: $NAME.\n"$APACHECTL stop;;

    exit 0

    There will be as many ways to load Apache at boot time as there are versions of Linux. You may need toconsult your specific documentation if the above suggestions do not work on your Linux system.

    The Apache Web server, as with any full-featured program, has a variety of configuration options. Stored inplain text files, the main configuration files are easily accessible through the text editor of your choice. Inaddition to the comments in the config files, Apache provides excellent documentation on directives andconfiguration options at http://httpd.apache.org/docs .

    This article introduced the basics of setting up Apache on a Linux system. Hopefully, Ive provided you withenough fundamental knowledge to configure and start your server. However, I would recommend taking afurther look at the documentation before beginning your configuration and making note of the features youwill need. That way, you can configure your Apache server to do exactly what you need it to do.

    http://httpd.apache.org/docshttp://httpd.apache.org/docs