Apache Web Server on Debian 8 (Jessie)

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.
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.

The Apache HTTP Web Sever (Apache) is an open source web application for deploying web servers. This guide explains how to install and configure an Apache web server on Debian 8.

If instead you would like to install a full LAMP (Linux, Apache, MySQL and PHP) stack, please see the LAMP on Debian 8 guide.

Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Before You Begin

  1. Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.

    To check your hostname run:

    hostname
    hostname -f
    

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

  2. Update your system:

    sudo apt-get update && sudo apt-get upgrade
    

Install Apache

Install the Apache 2 web server, its documentation, and a collection of utilities:

sudo apt-get install apache2 apache2-doc apache2-utils

Configure the Multi-Processing Module

Apache 2.4 offers various multi-processing modules (MPMs) to handle connections. The default MPM is the event module, although the prefork module is still recommended if you’re using standard PHP.

The Prefork Module

  1. Open /etc/apache2/mods-available/mpm_prefork.conf in your text editor and edit the values as needed. The following is optimized for a 2GB Linode:

    File: /etc/apache2/mods-available/mpm_prefork.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    # prefork MPM
    # StartServers: number of server processes to start
    # MinSpareServers: minimum number of server processes which are kept spare
    # MaxSpareServers: maximum number of server processes which are kept spare
    # MaxRequestWorkers: maximum number of server processes allowed to start
    # MaxConnectionsPerChild: maximum number of requests a server process serves
    
    <IfModule mpm_prefork_module>
            StartServers              4
            MinSpareServers           20
            MaxSpareServers           40
            MaxRequestWorkers         200
            MaxConnectionsPerChild    4500
    </IfModule>
  2. On Debian 8, the event module is enabled by default. This will need to be disabled, and the prefork module enabled:

    sudo a2dismod mpm_event
    sudo a2enmod mpm_prefork
    
  3. Restart Apache:

    sudo systemctl restart apache2
    

The Event Module

If you choose to keep the event module enabled, these settings are suggested for a 2GB Linode.

  1. Open /etc/apache2/mods-available/mpm_event.conf in your text editor and edit the values as needed:

    File: /etc/apache2/mods-available/mpm_event.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    # event MPM
    # StartServers: initial number of server processes to start
    # MinSpareThreads: minimum number of worker threads which are kept spare
    # MaxSpareThreads: maximum number of worker threads which are kept spare
    # ThreadsPerChild: constant number of worker threads in each server process
    # MaxRequestWorkers: maximum number of worker threads
    # MaxConnectionsPerChild: maximum number of requests a server process serves
    <IfModule mpm_event_module>
            StartServers             2
            MinSpareThreads          25
            MaxSpareThreads          75
            ThreadLimit              64
            ThreadsPerChild          25
            MaxRequestWorkers        150
            MaxConnectionsPerChild   3000
    </IfModule>
  2. Restart Apache:

    sudo systemctl restart apache2
    

Configure Apache for Virtual Hosting

Apache supports name-based virtual hosting, which allows you to host multiple domains on a single server with a single IP. Although there are different ways to set up virtual hosts, the method below is recommended.

  1. Disable the default Apache virtual host:

    sudo a2dissite 000-default.conf
    
  2. Create an example.com.conf file in /etc/apache2/sites-available with your text editor, replacing instances of example.com with your own domain URL in both the configuration file and in the file name:

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

    Repeat this process for any other domains you host.

    Note

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

    File: /etc/apache2/sites-available/example.com.conf
    1
    2
    
    Options ExecCGI
    AddHandler cgi-script .pl
  3. Create directories for your websites and websites’ logs, replacing example.com with your own domain information:

    sudo mkdir -p /var/www/example.com/public_html
    sudo mkdir /var/www/example.com/logs
    

    Repeat the process if you intend on hosting multiple websites on your Linode.

  4. Enable the site:

    sudo a2ensite example.com.conf
    
  5. Restart Apache:

    sudo systemctl restart apache2
    

Apache Mods and Scripting

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.

  1. List available Apache modules:

    sudo apt-cache search libapache2*
    
  2. Install any desired modules:

    sudo apt-get install [module-name]
    
  3. All mods are located in the /etc/apache2/mods-avaiable directory. Edit the .conf file of any installed module if needed, then enable the module:

    sudo a2enmod [module-name]
    

    To disable a module that is currently enabled:

    a2dismod [module-name]
    

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:

  • Perl support:

    sudo apt-get install libapache2-mod-perl2
    
  • Python support:

    sudo apt-get install libapache2-mod-python
    
  • PHP support:

    sudo apt-get install php5 php-pear
    

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.