Install and Configure Apache on Ubuntu Server

Apache is the first choice for many as open source web server due to its functionality and ability to deal with huge traffic.

Install Apache

Let’s connect to our Ubuntu server and give a simple command to install Apache2 :

Only the first package is necessary for the operation but it can be convenient to install the second one, apache2-doc, to have inside the server all the official documentation always available. The same documentation is also available on the Apache2 website.

The installation is already finished and we can immediately go to test its success by connecting from another PC of the same network to our web server.

We then open our favorite browser and type the IP address or hostname of the server. As you will see, a default Apache page will respond to warn us that everything is working:

If we have also installed the apache2-doc package then we will also have the official documentation.

Configure Apache

The Apache2 configuration is managed through directives written on text files. In Ubuntu, these configuration files and folders are all found in the /etc/apache2 directory.
So let’s see in detail what are these configuration files that determine the operation of Apache2:

  • apache2.conf: is the main configuration file. Inside we find all the global settings or those that will be applied to each site on our server.
  • ports.conf: is the file that indicates on which ports the server is listening for incoming requests. At the time of installation, the ports are the standard ones: 80 for http and 443 for https. This file is called directly within apache2.conf.
  • envvars: in this file the environment variables used in the configuration files are defined.
  • sites-available : this folder contains the configuration files of all Virtual Host (VH), that is all the sites on the server. VHs allow you to have multiple sites within the same web server as we’ll see later.
  • sites-enabled: this folder contains symbolic links to sites-available and represents the list of sites actually accessible to clients.
  • mods-available: this folder contains the configuration files for the web server modules.
  • mods-enabled: as for sites-enabled, the symbolic links to mods-available of the modules actually enabled and started within the Apache server is also contained here.
READ  Logrotate: Forced Rotation of Logs

After each change to a configuration file, the service must be restarted for the changes to take effect. To do this, we will type in the terminal.

The main directives of Apache2

Here it is impossible to analyze analytically the different directives provided by Apache2 (they are really many), we will limit ourselves, therefore, to consider the main ones.

We will find these variables directly in the apache2.conf file (if they are to be considered global and concern, that is, the entire web server) or within the configuration files of the individual Virtual Host (if they concern individual websites hosted within the our server).

  • ServerRoot: defines the root directory for Apache configurations. The default value is /etc/apache2
  • DocumentRoot: indicates the directory containing the website requested from the server. In our Ubuntu distro the default value is /var/www
  • ServerAdmin: Indicates the server administrator’s e-mail address. It will appear in the error messages that clients receive.
  • ServerName: indicates the hostname to which the Apache server must respond.
  • ServerAlias indicates alternative names to reach for a host.
  • Listen: defines the port on which the server is listening.
  • User and Group: define the user and the group that owns the process.
  • ErrorLog: indicates the path where to log the error log files.
  • CustomLog: indicates the path where to record the access log file and its format.
  • LogLevel: defines the level of detail of the logs.
  • Allow from: indicates to which IP addresses or hostname is allowed to access the site.
  • Deny from: indicates which IP addresses or hostnames are not allowed to access the site.
  • PidFile: the file that contains the PID of the parent Apache2 process.
  • TimeOut: the idle time after which Apache2 closes a client connection.
  • KeepAlive: indicates whether persistent connections are allowed or not (On / Off), ie able to serve more than one request from the same client.
  • MaxKeepAliveRequests: the maximum number of requests that a client can make to the server on a single connection. Set to 0 requests can be unlimited.
  • KeepAliveTimeout: indicates the number of seconds that a client must wait before making another request on the same connection.
  • Includes: inclusion guidelines. Apache2 uses these directives to declare configuration parameters written to external files (for example, ports.conf ).
READ  Install Docker on Ubuntu 16.04 Xenial Xerus

These are just some directives available in Apache2, the ones most often used. As usual, however, we recommend consulting the official documentation to get a wider overview.

Authored By Imran Yousaf

I am Imran Yousaf, a computer geek, founder of the site Smashinglab.com. I am a die hard fond of open-source software and Linux operating system. In addition to Linux, I am interested in everything related to information technology and modern science.

Leave a Reply

Your email address will not be published. Required fields are marked *