How To Install the Apache Web Server on Ubuntu 22.04 | DigitalOcean

Introduction

Apache HTTP Server is the most widely used web server in the world. It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software.

In this guide, you will learn how to install an Apache web server on your Ubuntu 22.04 server.

Before

starting this guide, you will need an Ubuntu 22.04 server configured with a non-root user with sudo privileges and a firewall enabled to block non-essential ports. You can learn how to do this by following our initial server setup guide for Ubuntu 22.04.

Once you are done setting this up, log in as your non-root user and proceed to the first step.

Step 1 – The

Apache Apache installation

is available within Ubuntu’s default software repositories, making it possible to install it using conventional package management tools

.

Start by updating the local package index to reflect the latest upward changes

: sudo apt update Then, install the apache2 package:

  1. sudo

apt install

  1. apache2

After confirming the installation, apt will install Apache and all required dependencies.

Step 2 — Firewall Adjustment

Before testing Apache, you need to modify your firewall settings to allow external access to the default web ports. If you followed the instructions in the prerequisites, you must have a UFW firewall configured to restrict access to the server.

During installation, Apache registers with UFW to provide some application profiles that can be used to enable or disable access to Apache through the firewall.

List the ufw application profiles

by running the following

:

  1. sudo ufw app

list

The result will be a list of application profiles

: OutputAvailable applications: Apache Apache Full Apache Secure OpenSSH

As the output indicates, there are three profiles available for

Apache:

  • Apache: This profile opens only port 80 (normal, unencrypted web traffic)
  • Apache Full: This profile opens both port 80 (normal, unencrypted web
  • traffic) and port 443 (TLS/SSL encrypted traffic)

  • Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you have configured. Since you have not yet configured SSL for your server in this guide, you will only need to allow traffic on port 80

: sudo ufw allow ‘

  1. Apache’

You can verify the change by checking the

status:

  1. sudo ufw status

The output will provide a list of allowed HTTP traffic

: OutputStatus: active For action From – – – OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6) ALLOW Anywhere (v6)

As the output indicates, the profile has been enabled to allow access to the Apache web server.

Step 3 — Checking the web server

At the end of the installation process, Ubuntu 22.04 starts Apache. The web server will now be up and running.

Make sure the service is active by running the command for the

system systemd init:

  1. sudo systemctl status

apache2 Output● apache2.service – The Apache HTTP server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; provider prese> Active: active (running) since Tue 2022-04-26 15:33:21 UTC; 43s Aug Documents: https://httpd.apache.org/docs/2.4/ Main PID: 5089 (apache2) Tasks: 55 (limit: 1119) Memory: 4.8M CPU: 33ms CGroup: /system.slice/apache2.service ├─5089 /usr/sbin/apache2 -k start ├─5091 /usr/sbin/apache2 -k start └─5092 /usr/sbin/apache2 -k start As

this result confirms, the service started successfully. However, the best way to test this is to request an Apache page.

You can access the default Apache landing page to confirm that the software is running properly through your IP address. If you don’t know the IP address of your server, you can get it in different ways from the command line.

Try typing the following at your server’s command prompt:

  1. hostname -I

You will receive some addresses separated by spaces. You can test each one in your web browser to determine if they work.

Another option is to use the free icanhazip.com tool. This is a website that, when accessed, returns your machine’s public IP address as read from another location on the Internet

:

  1. curl -4 icanhazip.com

When you have the IP address of your server, enter it in the address bar of your browser

: http://your_server_ip

You will see the default Ubuntu 22.04 Apache webpage as in the following:

Apache default page

This page indicates that Apache is working correctly. It also includes basic information about important Apache files and directory locations.

Step 4 — Apache Process Management

Now that you have your web server up and running, let’s review some basic administration commands using

systemctl. To stop the web server, run: sudo systemctl stop apache2 To start the

web server when it is stopped, run

: sudo systemctl start apache2

To stop

and restart the service, Run:

  1. sudo systemctl restart apache2

If you are simply making configuration changes, Apache can often reload without losing connections. To do this, use the following command:

  1. sudo systemctl reload apache2

By default, Apache is set to start automatically when the server starts. If this is not what you want, disable this behavior by running

: sudo systemctl disable apache2

To re-enable the service to start at boot, run:

  1. sudo systemctl enable apache2

Apache will now start automatically when the server starts again.

Step 5 — Configuring Virtual Hosts (recommended)

When using Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We’ll set up a domain called your_domain, but you need to replace it with your own domain name.

Apache in Ubuntu 22.04 has a server block enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you host multiple sites. Instead of modifying /var/www/html, create a directory structure within /var/www for a your_domain site, leaving /var/www/html in place as the default directory to be served if a client request does not match any other site.

Create the directory for your_domain

as follows

: sudo

  1. mkdir

/var/www/your_domain

Next, assign ownership of the directory to the user you are currently logged in, because with the environment variable

$USER:

  1. sudo chown -R $USER:$USER /var/www/your_domain

The permissions on your web root must be correct if you have not modified its umask value, which sets the default file permissions. To make sure your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can enter the following command

: sudo

  1. chmod -R 755 /var/www/your_domain

Next, create a sample index.html page using nano or your favorite editor:

  1. sudo nano /var/www/your_domain/index.html

Inside, add the following HTML example:

<html> <head> <title>Welcome to Your_domain!</title> </head> <body> <h1>Success! The virtual host is your_domain working!</h1> </body> </html>

Save and close the file when you are finished. If you are using nano, you can do so by pressing CTRL+X, then Y and ENTER.

For Apache to serve this content, you need to create a virtual host file with the correct directives. Instead of modifying

the default configuration file located in /etc/apache2/sites-available/000-default.conf directly, make a new one in /etc/apache2/sites-available/your_domain.conf:

  1. sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following configuration block, which is similar to the default, but updated for your new directory and domain name:

<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName your_domain ServerAlias www.your_domain DocumentRoot /var/www/your_domain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log merged </VirtualHost>

Note that we have updated DocumentRoot to our new directory and ServerAdmin to an email that your_domain The site administrator can access it. We’ve also added two directives: ServerName, which sets the base domain that will match this virtual host definition, and ServerAlias, which sets other names that will match as if they were the base name.

Save and close the file when you are finished.

Now enable the file with the a2ensite tool: sudo a2ensite your_domain.conf Disable the default

site defined in

000-default.conf: sudo

  1. a2dissite 000-default.conf

Next, test the configuration errors

:

  1. sudo apache2ctl configtest

You should receive the following output:

Output

.

  1. .

. OK Syntax

Restart Apache to implement your changes:

  1. sudo systemctl restart apache2

Apache will now be serving your domain name. You can test this by navigating to http://your_domain, where you’ll see something like the following:

Step 6 – Familiarize

yourself with important Apache files and directories

Now that you know how to manage the Apache service itself, you should take a few minutes to familiarize yourself with some important directories and files.

Contents

  • /var/www/html: The actual web content, which by default only consists of the default Apache page you previously viewed, is served from the /var/www/html directory. This can be changed by altering the Apache configuration files.

Server Configuration

/

  • etc/apache2: The Apache configuration directory. All Apache configuration files reside here.
  • /etc/apache2

  • /apache2.conf: The main Apache configuration file. This can be modified to make changes to the Apache global configuration. This file is responsible for loading many of the other files into the configuration directory.
  • /

  • etc/apache2/ports.conf: This file specifies the ports on which Apache will listen. By default, Apache listens on port 80 and also listens on port 443 when a module that provides SSL capabilities is enabled.
  • /

  • etc/apache2/sites-available/: The directory where virtual hosts can be stored per site. Apache will not use the configuration files found in this directory unless they are linked to the site-enabled directory. Typically, all server block configuration is done in this directory and then enabled by linking to the other directory with the a2ensite command.
  • /

  • etc/apache2/sites-enabled/: The directory where the site-enabled virtual hosts are stored. Typically, these are created by linking to configuration files that are located in the sites-available directory with a2ensite. Apache reads the configuration files and bindings found in this directory when it is started or reloaded to build a complete configuration.
  • /

  • etc/apache2/conf-available/, /etc/apache2/conf-enabled/: These directories have the same relationship as the sites-available and sites-enabled directories, but are used to store configuration fragments that do not belong to a virtual host. Files in the conf-available directory can be enabled with the a2enconf command and disabled with the a2disconf command.
  • /

  • etc/apache2/mods-available/, /etc/apache2/mods-enabled/: These directories contain the available and enabled modules, respectively. Files ending in .load contain fragments for loading specific modules, while files ending in .conf contain the settings for those modules. Modules can be enabled and disabled using the a2enmod and a2dismod commands.

Server

logs

/var/log/apache2/access.log: By default, every request to your web server is logged in this log file

  • unless Apache is configured to do otherwise. /var/log/apache2/error.log: By default,
  • all errors are logged in this file. The LogLevel directive in the Apache configuration specifies how many details the error logs will contain.

Conclusion

Now that you have

your web server installed, you have many options for the type of content you can serve and the technologies you can use to

create a richer experience.

If you want to create a more complete application stack, you can read this article on how to set up a LAMP stack in Ubuntu 22.04