Apache is often used in com­bin­a­tion with Linux systems. In par­tic­u­lar, Ubuntu is ideal to install the web server due to its strong community and doc­u­ment­a­tion. In this guide we will explain step by step how you can install and set up Apache on Ubuntu.

System re­quire­ments for Apache on Ubuntu

Apache is one of the oldest and most stable web servers. Its pop­ular­ity is down to its scalab­il­ity and simple con­fig­ur­a­tion. To install Apache on Ubuntu 22.04 there are no specific re­quire­ments for the processor since most modern pro­cessors should be able to run Apache on Ubuntu. However, you will need suf­fi­cient RAM and hard drive memory.

Apache uses few system resources and can be con­figured for different types of systems including desktop computers, laptops, servers and virtual machines. If you want to host a powerful website, you may find that you need more resources to ensure suf­fi­cient per­form­ance. You need to remember that using modules can increase your system re­quire­ments. For example, if you want to integrate a module to improve the Apache web server’s per­form­ance, your system will need ad­di­tion­al memory for caching and other op­tim­isa­tions.

To install the Apache web server you should have the following minimum system re­quire­ments:

  • Memory (RAM): 4 Gigabytes
  • Operating system: Ubuntu, users with sudo access rights
  • Hard-disk drive: 5 Gigabytes
  • Firewall: for http data traffic and to block ports that aren’t necessary
  • Internet con­nec­tion: to download packages
Tip

Linux Hosting from IONOS supports a wide range of Apache modules that allow you to quickly and ef­fect­ively configure your own Apache reverse proxy. Benefit from flexibly scalable per­form­ance, DDOS pro­tec­tion and top PHP features.

A step-by-step guide to in­stalling Apache on Ubuntu

Ubuntu 22.04 uses the package man­age­ment tool APT to install Apache. You will first need to update your package index on your Ubuntu system to ensure that all necessary de­pend­en­cies are up to date.

If you’re not in­stalling the program locally, you will need to log in to your Ubuntu server using SSH.

Step 1: Update the package list

Open the terminal and run an update.

$ sudo apt update
bash

Step 2: Install the Apache package

Next you need to install the Apache package with all the necessary de­pend­en­cies by using the APT command ‘install’.

$ sudo apt install apache2
bash

Step 3: Change your firewall settings

To configure Apache you need to activate un­com­plic­ated firewalls (UFW) on Ubuntu. Once you’ve installed Apache on Ubuntu, Apache will set up ap­plic­a­tion profiles in UFW with which data traffic can be managed to the web ports.

You can use the following command to display the list of ap­plic­a­tion profiles:

$ sudo ufw app list
bash

This will show three Apache profiles:

Image: Linux terminal: Apache application profile list
Terminal: Apache ap­plic­a­tion profile list
  • Apache: opens TCP port 80 for HTTP (un­en­cryp­ted con­nec­tion)
  • Apache Full: opens TCP port 80 (HTTP, un­en­cryp­ted) and 443 (HTTPS, with TLS/SSL encrypted)
  • Apache Secure: opens only HTTPS port 443 for an encrypted con­nec­tion

Since we still haven’t set up SSL, we can only open port 80.

$ sudo ufw allow 'Apache'
bash

Using the command ‘status’ you can check whether the correct settings are in place.

$ sudo ufw status
bash

Step 4: Test Apache status

Use the system manager ‘systemd’ to check whether the Apache service is active.

$ sudo systemctl status apache2
bash

Step 5: Open the standard Apache default page

Enter your IP address into your browser’s address bar to go to the standard Apache default page. If you don’t know your IP address you can display it using ‘hostname’.

$ hostname -I
bash

You can also use the icanhazip tool.

$ curl -4 icanhazip.com
bash

You can now open the standard Apache page in your browser and enter your IP address instead of ‘server_ip’.

http://server_ip

Here is an excerpt from the default page about Ubuntu:

Image: Web browser: Apache default page on Ubuntu
Web browser: Apache default page on Ubuntu

Step 6: Manage the Apache daemon

You can manage the Apache web server daemon or service with ‘systemctl’.

Starting the Apache web server:

$ sudo systemctl start apache2
bash

Stopping the Apache web server:

$ sudo systemctl stop apache2
bash

Stopping and re­start­ing the Apache web server:

$ sudo systemctl restart apache2
bash

Re­start­ing Apache and reloading the con­fig­ur­a­tion:

$ sudo systemctl reload apache2
bash

If you install Apache on Ubuntu, the web server will auto­mat­ic­ally start once set up when booting. You can also de­ac­tiv­ate this feature using the following command:

$ sudo systemctl disable apache2
bash

To re­act­iv­ate the automatic Apache start when booting, you can enter the following command:

$ sudo systemctl enable apache2
bash

Step 7: Use virtual hosts

Apache usually hosts documents under /var/www/html. If you want to use more domains on a server, you can use virtual hosts. In this example, we will set up a directory structure for your own domain under /var/www/.

$ sudo mkdir /var/www/your_domain
bash

Replace ‘your_domain’ with your own domain.

You can assign ownership of the directory with the en­vir­on­ment­al variable $USER:

$ sudo chown -R $USER:$USER /var/www/your_domain
bash

You can also spe­cific­ally assign reading, writing and execution rights in the Oktal mode:

$ sudo chmod -R 755 /var/www/your_domain
bash

Step 8: Create a test page

Set an index.html as the default page for your domain. To do this you can use the text editor nano, for example.

$ sudo nano /var/www/your_domain/index.html
bash

Select a for­mu­la­tion and add it to the HTML file:

<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
        <h1>Here you can see that your_domain virtual host is successfully working!</h1>
    </body>
</html>
html

Step 9: Set the con­fig­ur­a­tion file for virtual hosts

To display the example page you need to correctly configure Apache for your domain. Create your own con­fig­ur­a­tion file for your domain and leave the standard con­fig­ur­a­tion file for Apache as it is.

$ /etc/apache2/sites-available/your_domain.conf
bash

Add the following block and replace ‘your_domain’ with the name of your domain. You can also enter an ad­min­is­trat­or’s email address next to ‘ServerAd­min’:

<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 combined
</VirtualHost>

Activate the con­fig­ur­a­tion file with the command ‘a2ensite’:

$ sudo a2ensite your_domain.conf
bash

De­ac­tiv­ate the old default page:

$ sudo a2dissite 000-default.conf
bash

Test your con­fig­ur­a­tion for errors:

$ sudo apache2ctl configtest
bash

If everything is correct you can restart Apache:

$ sudo systemctl restart apache2
bash

Go to your default page:

http://your_domain

You should now be able to see your example page:

Image: Web browser: Example page for a virtual host
Web browser: Example page for a virtual host

Step 10: Important Apache files and dir­ect­or­ies

To ef­fi­ciently operate the Apache web server, it’s helpful to know some commonly used files and dir­ect­or­ies:

  • /var/www/html: This is the directory in which Apache usually stores documents. This can be changed in the con­fig­ur­a­tion file.
  • /etc/apache2: This is where all Apache con­fig­ur­a­tion files are stored.
  • /etc/apache2/apache2.conf: This is the primary con­fig­ur­a­tion file with which you can change the global con­fig­ur­a­tion.
  • /etc/apache2/ports.conf: This file contains the open ports. These are normally port 80 and/or port 443.
  • /etc/apache2/sites-available/: This folder contains the virtual hosts you are using. Config files stored here must be linked to the directory with ‘site-enabled’ to work.
  • /etc/apache2/conf-available/, /etc/apache2/conf-enabled/: These dir­ect­or­ies save other conf files which don’t belong to virtual hosts. You can activate them with ‘a2enconf’ and de­ac­tiv­ate them using ‘a2disconf’.
  • /etc/apache2/mods-available/, /etc/apache2/mods-enabled/: Available and activated modules are stored in these dir­ect­or­ies. You can activate a module using ‘a2enmod’ and de­ac­tiv­ate it with ‘a2dismod’.
  • /var/log/apache2/access.log: This log file records all requests to the web server.
  • /var/log/apache2/error.log: This file records all error messages. LogLevel in­form­a­tion describes the severity.
Go to Main Menu