How to install and configure apache web server on Ubuntu Server 20.04

Apache is a modularized web server that can be extended by dynamically loading extra modules as and when required. This provides the flexibility to run a bare minimum web server or a fully featured box with modules to support compression, SSL, redirects, language modules, and more. Apache provides multiple connection processing algorithms called multi-processing modules (MPM). It provides an option to create a separate single threaded process for each new request (mpm_prefork), a multi-threaded process that can handle multiple concurrent requests (mpm_worker), or the latest development of mpm_event, which separates the active and idle connections.

Installing the Apache web server

Follow these steps to install and configure the Apache web server:

Step 1 : Install Apache2 from the Ubuntu package repository:

sudo apt-get update
    sudo apt-get install apache2

Step 2 : Check if Apache2 has installed successfully. The command wget should download the index.html file:

wget 127.0.0.1

Step 3 : You can also open a browser on a local machine and point it to the server IP address. You should see a default It works! page customized for Ubuntu:

Creating the first virtual host

Step 1 : Now, let’s proceed with creating our first virtual host. First create a directory structure. Change the directory to /var/www/ and create a new directory for the contents of our site:

cd /var/www
    sudo mkdir example.com

Step 2 : Change the ownership and group of the directory example.com:

sudo chown $USER:www-data example.com

Step 3 : Set the file permissions to secure web contents:

sudo chmod 750 example.com

Step 4 : Create the required directories under the example.com directory:

cd example.com
    mkdir public_html

Step 5: Create a index.html file under the public_html directory:

echo '<b>Hello World ...</b>' > public_html/index.html

Step 6 : Next, we need to set up a new virtual host under the Apache configuration.

Step 7 : Copy the default Virtual Host file under /etc/apache2/sites-available and use it as a starting point for our configuration:

cd /etc/apache2/sites-available
    sudo cp 000-default.conf example.com.conf

Step 8 : Edit example.com.conf to match it with the following example. Change the parameters as per your requirements:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/example.com/public_html

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

Step 9 : Save the changes and exit example.com.conf.

Step 10 : If you are using the same port as the default VirtualHost, do not forget to disable the default one:

sudo a2dissite 000-default.conf

Step 11 : Finally, enable our new VirtualHost with a2ensite and reload Apache

sudo a2ensite example.com.conf
    sudo service apache2 reload

Step 12: Start your browser and point it to the domain or IP address of your server:

Enabling HTTP/2 in Apache

If you are looking for HTTP2 support, Apache does provide a separate module for that. Apache version 2.4.17 ships with a module, mod_http2, that implements the latest HTTP version, HTTP2.

Step 1 : you can enable mod_http2 as follows:

sudo a2enmod http2

Step 2 : Next, edit the specific virtual host file to enable the HTTP2 protocol for a specific site. Note that you need to configure your site to use an SSL/TLS connection:

<VirtualHost *:443>
    Protocols h2 http/1.1
    ...
</VirtualHost>

Step 3 : Finally, restart your Apache server:

sudo service apache2 restart

Add Comment