Apache-2.4 Basic Configurations - Part-01

tikam02

Tikam Singh Alma

Posted on March 14, 2022

Apache-2.4 Basic Configurations - Part-01

Files and directories

The buildings block and the base of the apache server configurations all are managed and controlled by the Directives which are defined in each configuration file. Directives are a set of functions and each one has its parameters. These are the default directories and configuration files, which needs to be configured for all the purpose of the webserver configurations and setup.

  • apache2.conf: The main Apache2 configuration file. This file is used to control the webserver's global configuration and it is the center point of the webserver where all functions are imported.

  • httpd.conf: The httpd.conf file is the old name of apache2.conf which is still supported in older versions. In news versions, only apache2.conf is used.

  • conf-available: 
    This is the directory that contains all the additional configurations which can be applied to the global scope of the webserver, the configurations added on conf-available will reflect on all the Hosts and Virtual hosts directives.
    All the server-side scripting and programmed server-side codes are stored here to extend the web server's functionality on the global scope.

  • conf-enabled: This directory is symlinked to /etc/apache2/conf-available. Whenever any updates or configurations are added and that configuration file is symlinked, it will be enabled the next time apache2 is restarted.

  • envvars: The local variable declarations and global variables are stored in ENVVARs
    Location -

/var/etc/apache2/ENVVARS
Enter fullscreen mode Exit fullscreen mode
  • mods-available: This directory includes all the apache2 modules which are available and that need to be configured. Location :
/var/etc/apache2/mods-available
Enter fullscreen mode Exit fullscreen mode
  • mods-enabled: This directory is symlinked to /etc/apache2/mods available is in charge of ensuring that if the modules configuration file is changed or amended, the symlinked configurations are enabled the next time apache2 begins.

  • ports.conf: This directory manages and determines which ports apache2 is listening on.

  • sites-available: 
    This is the main directory where the web application and web page configurations files are stored, that is domain.conf, etc.
    And other files which extend the main domain.conf or web applications, SSL.conf, firewall.conf configurations files, etc.
    This director is used to configure the files for webserver Virtual Hosts.
    VirtualHost basically enables us to host and serve multiple web applications and web pages on defined multiple domains and ports.

  • sites-enabled: like mods-enabled, sites-enabled contains symlinks to the /etc/apache2/sites-available directory.

  • magic: The initial few bytes or extensions of a file received or delivered are used to determine the MIME types in this configuration file. This configuration file detects which type or extension of files and contents are received and need to be sent and upon that extension, type developers can add functions to optimize the contents.

List all the files and directories using this command :

$cd /etc/apache2
$ls
Enter fullscreen mode Exit fullscreen mode

Global configuration setup

These are the parameters that can be configured server-wide and these parameters have different values which are very important for the overall web server performance,apache2.conf file is the main file from which these parameters are configured.

Timeout

The TimeOut directive currently defines the amount of time Apache will wait for three things:

  • The total amount of time it takes to receive a GET request.
  • The amount of time between receipt of TCP packets on a POST or PUT request.
  • The amount of time between ACKs on transmissions of TCP packets in responses.

By default, this parameter is set to "300", which means that the server has a maximum of 300 seconds to complete one request

.

KeepAlive
The Keep-Alive extension to HTTP/1.0 and the persistent connection feature of HTTP/1.1 provide long-lived HTTP sessions which allow multiple requests to be sent over the same TCP connection.
Apache provides this configuration to set the custom keep-alive setup.
This option, if set to "On", will allow each connection to remain open to handle multiple requests from the same client.
If this is set to "Off", each request will have to establish a new connection, which can result in significant overhead depending on your setup and traffic situation.

MaxKeepAliveRequests
This controls how many separate requests each connection will handle before dying. Keeping this number high will allow Apache to serve content to each client more effectively.
Setting this value to 0 will allow Apache to serve an unlimited amount of requests for each connection.

KeepAliveTimeout
This setting specifies how long to wait for the next request after finishing the last one. If the timeout threshold is reached, then the connection will die.
This just means that the next time content is requested, the server will establish a new connection to handle the request for the content that makes up the page the client is visiting.

MPM Configuration
The next section specifies the configuration of the MPM (Multi-Processing Module) options. You can cross-reference which section your Apache installation was compiled with by exiting into the terminal and typing:

apache2 -l
Compiled in modules: core.c mod_log_config.c mod_logio.c prefork.c http_core.c mod_so.c
As you can see, in this server, "prefork. c" is a module that was compiled in and is also in the "apache2.conf" file. Your installation may have multiple to choose from, but only one can be selected.

You can see apache2.conf file, where the global configurations are set.

What is virtualhost in apache?

Virtual Host files are the main directives where we can configure which application file the server run, what domains it should use, which modules it should use, and more use and it dictates how the Apache web server will respond to various domain requests.
Apache webserver VirtualHost is very customizable and you can add multiple hosts that are multiple sites with different ports and different configurations in a single configuration file and configure domain and subdomains.
This is the file where most of the configurations for the application server and static website will be done, from adding SSL, caching, security to defining use thread processes for applications.

Apache comes with a default virtual host file called 000-default.conf that we can use as a base point for experiments and testing deployments.

Default VirtualHost Configurations
Get it in terminal using the command:

sudo cat  /etc/apache2/sites-available/000-default.conf
Enter fullscreen mode Exit fullscreen mode

Configuration

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

How to configure virtualhost directive.

Within the Virtual Host definition, there are definitions for how the server handles different directories within the file system.

Lets's learn about each of the parameters inside VirtualHost:

ServerName

establishes the base domain that should match this virtual host definition. This will most likely be your domain.

ServerName www.your-domain.com
Enter fullscreen mode Exit fullscreen mode

ServerAdmin

The "ServerAdmin" option specifies a contact email that should be used when there are server problems.

ServerAdmin name@your-domain.com
Enter fullscreen mode Exit fullscreen mode

DocumentRoot

Directive of the webserver contents n files. This is the directory from which Apache will read the contents that the visitor will access over the browser. Or in other words, this is the directory that forms the tree of directories that will be accessible over the web.
The "DocumentRoot" option specifies where the content that is requested for this Virtual Host will be located. The default Virtual Host is set up to serve content out of the "/var/www" directory on Ubuntu.

DocumentRoot /var/www/html
OR
DocumentRoot /home/user/index.html
Enter fullscreen mode Exit fullscreen mode

LogLevel

The Apache error log has a logging level that filters the messages sent to the log. You specify the level with the LogLevel setting.

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#

LogLevel warn
OR
LogLevel error
Enter fullscreen mode Exit fullscreen mode

Include

Server Side Includes (SSI) concept allows you to configure your webpage or your virtualhost configure to use external files or documents to include in live files and configurations. They are mainly used to serve dynamic content on web pages and reuse HTML codes. When you need to change the navigation bar you change just the include file, not every web page on your server.

include virtual="/header.html"
Enter fullscreen mode Exit fullscreen mode

Let's assume you have a simple hello world index.html file in /home/web/index.html directory and wanted Log level: warn of any warning.
And logs on directory /home/logs/

Open default configuration file edit it and follow the next steps to deploy it.

sudo nano  /etc/apache2/sites-available/000-default.conf
Enter fullscreen mode Exit fullscreen mode

Configuring VirtualHost

<VirtualHost *:80>
        ServerName www.your-domain.com
        ServerAdmin name@your-domain.com
        DocumentRoot /home/web/index.html
        LogLevel warn

        ErrorLog /home/logs/error.log
        CustomLog /home/logs/access.log combined
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Enabling site configurations in apache

Once you have a Virtual Host file that meets your requirements, you can use enable the configuration and make it live.

create a symbolic link in the "sites-enabled" directory to an existing file in the "sites-available" directory, using the command

Sites-enabled manages which configuration files of virtual hosts will be live whenever the server is started.
This can be useful when you want to maintain virtual-host configuration files, but don't want the sites to be live, or are in a deployment process.

cd /etc/apache2/sites-enabled

ln -s ../sites-available/000-default.conf 000-default.conf
Enter fullscreen mode Exit fullscreen mode

Reload the apache

sudo service apache2 reload
Enter fullscreen mode Exit fullscreen mode

Enable the virtual host to make it live/

sudo a2ensite virtual_host_file_name
Enter fullscreen mode Exit fullscreen mode

Our configuration file is 000-default.conf

sudo a2ensite 000-default.conf
Enter fullscreen mode Exit fullscreen mode

After enabling a site, issue the following command to tell Apache to re-read its configuration files, allowing the change to propagate:

sudo service apache2 reload
Enter fullscreen mode Exit fullscreen mode

There is also a companion command for disabling a Virtual Host. It operates by removing the symbolic link from the "sites-enabled" directory:

sudo a2dissite virtual_host_file_name
Enter fullscreen mode Exit fullscreen mode

Again, reload the configuration to make the change happen:

sudo service apache2 reload
Enter fullscreen mode Exit fullscreen mode

Modules can be enabled or disabled by using the "a2enmod" and "a2dismod" commands respectively. They work in the same way as the "site" versions of these commands.

Remember to reload your configuration changes after modules have been enabled or disabled as well.

Try to host a simple index.html file and make it live to test the configuration using this command:

apachectl configtest
OR
httpd -t
Enter fullscreen mode Exit fullscreen mode

Before using any software and frameworks it is always better to read the basic concepts, their parameters, and settings. This practice saves a lot of debugging and time and is good for setting up the project configuration. Knowing clear concepts and starting implementation is always better than jumping off directly to programming by seeing the code on the web.

💖 💪 🙅 🚩
tikam02
Tikam Singh Alma

Posted on March 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related