Apache 2 Web Server on Debian 6 (Squeeze)

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Deprecated

This guide has been deprecated and is no longer being maintained.

Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

This tutorial explains how to install and configure the Apache web server on Debian 6 (Squeeze).

Note that if you’re looking to install a full LAMP stack, you may want to consider using our LAMP guide for Debian 6.

Before You Begin

Issue the following commands to make sure your hostname is set properly:

hostname
hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

All of the commands in this article should be executed either as root or as a user with sudo access.

Install Apache 2

  1. Make sure your package repositories and installed programs are up to date by issuing the following commands:

    apt-get update
    apt-get upgrade --show-upgraded
    
  2. Enter the following command to install the Apache 2 web server, its documentation, and a collection of utilities:

    apt-get install apache2 apache2-doc apache2-utils
    

Optional: Install Support for Scripting

The following commands install Apache support for server-side scripting in PHP, Ruby, Python, and Perl. Support for these languages is optional based on your server environment.

To install Ruby support, issue the following command:

apt-get install libapache2-mod-ruby

To install Perl support, issue the following command:

apt-get install libapache2-mod-perl2

To install Python support, issue the following command:

apt-get install libapache2-mod-python

If you need support for MySQL in Python, you will also need to install Python MySQL support:

apt-get install python-mysqldb

Your PHP application may require additional dependencies included in Debian. To check for available PHP dependencies run this command:

apt-cache search php

The output of that command will show you a list of package names and descriptions. To install them, issue the following command:

apt-get install libapache2-mod-php5 php5 php-pear php5-xcache

To install the php5-suhosin package, which provides additional security for your PHP installation, issue the following command:

apt-get install php5-suhosin

If you want to run PHP with MySQL, then you should also install MySQL support:

apt-get install php5-mysql

Configure Apache for Named-Based Virtual Hosting

Apache supports name-based virtual hosting, which allows you to host multiple domains on a single server with a single IP. (IP-based hosting is also available.) In this example, you’ll create two virtually hosted sites, example.net and example.org.

Follow these instructions:

  1. Run this command to disable the default Apache virtual host:

    a2dissite default
    
  2. Each virtual host needs its own configuration file in the /etc/apache2/sites-available/ directory. Create the file for example.net, called /etc/apache2/sites-available/example.net, with the following content. Be sure to replace example.net with your own domain name.

    nano /etc/apache2/sites-available/example.net
    
    File: /etc/apache2/sites-available/example.net
    1
    2
    3
    4
    5
    6
    7
    8
    
    <VirtualHost *:80>
         ServerAdmin webmaster@example.net
         ServerName example.net
         ServerAlias www.example.net
         DocumentRoot /srv/www/example.net/public_html/
         ErrorLog /srv/www/example.net/logs/error.log
         CustomLog /srv/www/example.net/logs/access.log combined
    </VirtualHost>
    Note

    If you would like to enable Perl support, add the following lines to the VirtualHost entry, right above the closing </VirtualHost> tag:

    File: /etc/apache2/sites-available/example.net
    1
    2
    
    Options ExecCGI
    AddHandler cgi-script .pl
  3. Now you’ll make the configuration file for the second domain. Create the file for example.org, called /etc/apache2/sites-available/example.org, with the following content. Be sure to replace example.org with your own domain name.

    nano /etc/apache2/sites-available/example.org
    
    File: /etc/apache2/sites-available/example.org
    1
    2
    3
    4
    5
    6
    7
    8
    
    <VirtualHost *:80>
         ServerAdmin admin@example.org
         ServerName example.org
         ServerAlias www.example.org
         DocumentRoot /srv/www/example.org/public_html/
         ErrorLog /srv/www/example.org/logs/error.log
         CustomLog /srv/www/example.org/logs/access.log combined
    </VirtualHost>
    Note
    Some basic options are specified for both example.net and example.org, including the location for the website files: under /srv/www/. You can add (or remove) additional configuration options, such as the Perl support shown in Step 2, on a site-by-site basis.
  4. Create the directories for example.net’s website files and logs by executing the following commands:

    mkdir -p /srv/www/example.net/public_html
    mkdir /srv/www/example.net/logs
    
  5. Create the directories for example.org’s website files and logs by executing the following commands:

    mkdir -p /srv/www/example.org/public_html
    mkdir /srv/www/example.org/logs
    
  6. Enable the sites by issuing these commands:

    a2ensite example.net
    a2ensite example.org
    
  7. Finally, restart the Apache server to initialize all the changes:

    /etc/init.d/apache2 restart
    

Note that whenever you create or edit any virtual host file, you’ll need to reload Apache’s configuration. You can do this without restarting the server with the following command:

/etc/init.d/apache2 reload

Congratulations! You have now installed Apache on your Debian Linode and configured it for virtual hosting.

Install Apache Modules

One of Apache’s strengths is its ability to be customized with modules. The default installation directory for Apache modules is the /etc/apache2/mods-available/ directory.

To get a list of available Apache modules in the Debian repository, run the following command:

apt-cache search libapache2*

To install one of these modules, run this command:

apt-get install [module-name]

Modules, after being installed, should be enabled and ready to use, although you may need to apply additional configuration options depending on the module. Consult the Apache module documentation for more information regarding the configuration of specific modules.

An easy way to see which modules are installed is to run a list command on the directory:

ls -al /etc/apache2/mods-available/

To enable an installed module, run the following command:

a2enmod [module-name]
Note
In the /etc/apache2/mods-available/ directory, files have .load and .conf extensions. Module names do not include the extensions.

To disable a module that is currently enabled, run this command:

a2dismod [module-name]

Order of Configuration Options

In the default installation of Apache 2 on Debian, the main configuration file is /etc/apache2/apache2.conf. However, Apache loads configuration files directives from a number of other files as well, in a specific order. The configuration files are read in the following order, with later directives overriding earlier ones:

  1. /etc/apache2/apache2.conf
  2. Files with .load or .conf extensions in the /etc/apache2/mods-enabled/ directory.
  3. /etc/apache2/httpd.conf (Blank by default.)
  4. /etc/apache2/ports.conf
  5. Files in the /etc/apache2/conf.d/ directory.
  6. Files in the /etc/apache2/sites-enabled/ directory.
  7. Per-directory .htaccess files.

Later files take precedence over earlier ones. Within a directory, files are read in order alphabetically.

Apache will follow symbolic links to read configuration files, so it’s possible to put files in other locations as well.

Generally, as specified in our LAMP Guide for Debian 6 (Squeeze) and elsewhere, you should create configuration files for your virtual hosts in the /etc/apache2/sites-available/ directory, then use the a2ensite tool to symbolically link to files in the sites-enabled/ directory. This allows for a clear and specific per-site configuration.

We recommend that you not modify these files:

  • /etc/apache2/httpd.conf
  • files in /etc/apache2/mods-enabled/
  • /etc/apache2/apache2.conf

In practice, the vast majority of your configuration options should go in site-specific virtual host configuration files. If you need to set a system-wide configuration option or aren’t using virtual hosting, the best practice is to specify options in files created beneath the conf.d/ directory.

For more help with conflicting directives, see our Apache Troubleshooting article.

Multi-Processing Module

The default Apache configuration uses a tool called MPM-worker. This multi-processing module can handle a large number of requests quickly by using multiple threads per worker process. However, this use of multiple threads is not compatible with some PHP extensions. When PHP is installed, MPM-worker is replaced with MPM-prefork, which allows Apache to handle requests without threading for greater compatibility with some software. Furthermore, using MPM-prefork allows Apache to isolate requests in separate processes so that if one request fails for some reason, other requests will be unaffected.

For more complex setups, however, we recommend that you consider using an alternate MPM module called ITK. mpm-itk is quite similar to prefork, but it goes one step further and runs the processes for each site under a distinct user account. This is particularly useful in situations where you’re hosting a number of distinct sites that you need to isolate based on user privileges.

  1. Install the mpm-itk module:

    apt-get install apache2-mpm-itk
    
  2. Open the /etc/apache2/sites-available/example.net file for editing:

    nano /etc/apache2/sites-available/example.net
    

    Add the following lines to the file’s <VirtualHost > block:

    File: /etc/apache2/sites-available/example.net
    1
    2
    3
    
    <IfModule mpm_itk_module>
       AssignUserId webeditor webgroup
    </IfModule>

    In this example, webeditor is the name of the user for example.net, and webgroup is the name of the group that owns example.net. Remember that you must create the user accounts and groups using the useradd command.

  3. Repeat Step 2 for your other websites.

  4. Reload the Apache configuration:

    /etc/init.d/apache2 reload
    

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.