Saturday, September 24, 2022

How to Configure Apache on an Ubuntu or Debian VPS

In this article, we want to teach you How to configure Apache on an Ubuntu or Debian VPS. you can use all of the versions of Ubuntu and Debian like Ubuntu 20.04, Ubuntu 18.04, Debian 11, Debian 10, etc.

What is an Apache webserver?

Apache HTTP Server is a free and open-source web server that delivers web content through the internet.

How to Configure Apache on an Ubuntu or Debian VPS

To configure Apache on an Ubuntu or Debian VPS you need to install it on your server.

Note: To run commands you need to use sudo before each command.

Install Apache on Ubuntu or Debian

First of all, you should install Apache. If you don’t install it yet, follow these commands below:

sudo apt-get update
sudo apt-get install apache2

After the installation is completed, Type your IP address in your web browser then you will see the default Apache index page.

Note: If you didn’t see a page like the below picture, check your VPS firewall and open port 80 and 443.

apache testpage on debian
The Apache File Hierarchy in Ubuntu and Debian

At this point, you can configure the Apache. You can see configuration files in /etc/apache2. Run the following command:

cd /etc/apache2
ls –F

Here are some configuration files in your output.
Let’s look at apache2.conf file.

Apache2.conf file setting

This is the main configuration file for the server. Almost all configuration can be done from within this file, although it is recommended to use separate, designated files for simplicity. This file will configure defaults and be the central point of access for the server to read configuration details.

This file is divided into three main parts:

1) Configuration for the global Apache server process.
2) Configuration for the default server.
3) Configuration of Virtual Hosts.

We want to look at the global Apache server process and learn how Apache defines its global settings.

Global configuration part

In this part, there are some options that control how Apache works as a whole.
Let’s see some of them that you may want to modify.

Timeout

The number of seconds before receiving and sending time out. By default, it is set to 300.

KeepAlive

If it is set to “On” it means that allows persistent connections (more than one request per connection).
You can deactivate it by setting it to “off”.

MaxKeepAliveRequests

The maximum number of requests to allow during a persistent connection. If it is set to “0” it means that allows an unlimited amount. We recommend you leave this number high, for maximum performance.

KeepAliveTimeout

The number of seconds to wait for the next request from the same client on the same connection.

MPM Configuration

In this part, you can see the multi-processing module options.
Run the following command:

apache2 –l

In your output, you can see complied modules.

Output:
Compiled in modules:
core.c
mod_so.c
mod_watchdog.c
http_core.c
mod_log_config.c
mod_logio.c
perfork.c
mod_version.c
mod_unixd.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 adjust the configuration of the prefork MPM in the appropriate part.

Explore the Default Virtual Host File

The default virtual host annunciation is in a file named “default” in the sites-available directory.

You can open the file with the following command:

sudo nano /etc/apache2/sites-available/default

As you can see the default virtual host is configured on the standard HTTP port 80.

Virtual host top-level configuration

There are some options that apply to the whole virtual host.

ServerAdmin

It is using a contact email if there are server problems. If you were using this as a template for other virtual host definitions you can add the “ServerName” definition that specifies a domain name or IP address.

You can also make the Virtual Host apply to more than one name by using the “ServerAlias” definition.

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.

Directory definitions

There are some definitions for how the server handles different directories.
The first directory definition applies rules for the “/” or root, directory.

Alias and ScriptAlias statements

Alias is a map of a URL path to a directory path.
ScriptAlias is like the Alias but it defines directories that have executable components in them.

Enable Sites and Modules in Apache

You can enable a site with the following command:

sudo a2ensite virtual_host_file_name

After the changes you should reload the apache with the following command:

sudo service apache2 reload

You can disable your virtual host with the following command:

sudo a2dissite virtual_host_file_name

Or you can use a2enmod for disabling your virtual host.

Then reload the apache again.

Note: Do not forget after any changes you should reload your apache.

Hope you enjoy this article about How to configure Apache on an Ubuntu or Debian VPS.

Related Content you may interested in:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles