Installing Apache on Ubuntu or Debian provides a fast, standard way to host websites, reverse-proxy applications, or publish internal documentation from a well-supported web server.
On these distributions, the Apache2 package ships a systemd service named apache2 plus a configuration layout under /etc/apache2 that separates global settings, enabled modules, and enabled virtual hosts. Helper commands such as a2enmod, a2dismod, a2ensite, and a2enconf manage symlinks between /etc/apache2/*-available and /etc/apache2/*-enabled, while apache2ctl validates configuration and performs graceful restarts.
Configuration mistakes can prevent apache2 from starting, so syntax checks should run before any reload or restart. Exposing ports 80 and 443 may require firewall changes, and a default install is reachable locally before DNS and TLS are configured.
Steps to install and customize Apache on Ubuntu and Debian:
- Open a terminal with sudo privileges.
- Update the apt package index.
$ sudo apt update [sudo] password for user: Hit:1 http://ports.ubuntu.com/ubuntu-ports lunar InRelease Hit:2 http://ports.ubuntu.com/ubuntu-ports lunar-updates InRelease Hit:3 http://ports.ubuntu.com/ubuntu-ports lunar-backports InRelease Hit:4 http://ports.ubuntu.com/ubuntu-ports lunar-security InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done All packages are up to date.
- Install the Apache2 package and its dependencies.
$ sudo apt install --assume-yes apache2 Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap Suggested packages: apache2-doc apache2-suexec-pristine | apache2-suexec-custom The following NEW packages will be installed: apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap 0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded. Need to get 1,866 kB of archives. After this operation, 13.0 MB of additional disk space will be used. ##### snipped #####
- Confirm the apache2 service is running.
$ sudo systemctl status apache2 ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2025-12-08 10:15:01 UTC; 2min ago Docs: https://httpd.apache.org/docs/2.4/ ##### snipped #####Use journalctl -u apache2 and /var/log/apache2/error.log for recent startup errors.
- Enable the Apache2 service to start automatically on boot.
$ sudo systemctl enable apache2 Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable apache2
- Enable the rewrite module when URL rewriting is required.
$ sudo a2enmod rewrite Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
Common add-ons include headers, ssl, proxy, and http2.
- Verify that the rewrite module is loaded.
$ sudo apache2ctl -M | grep rewrite rewrite_module (shared)
- Display the default virtual host configuration to find DocumentRoot and logging directives.
$ sudo sed -n '1,120p' /etc/apache2/sites-available/000-default.conf <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> - Edit the default virtual host file to customize per-site directives.
$ sudo vi /etc/apache2/sites-available/000-default.conf
Incorrect syntax in /etc/apache2 can prevent apache2 from reloading and cause downtime.
- Modify the global Apache configuration when server-wide settings are needed.
$ sudo vi /etc/apache2/apache2.conf
- Test the configuration for errors before applying changes.
$ sudo apache2ctl configtest AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message Syntax OK
Syntax OK indicates a valid configuration even when AH00558 is shown.
- Restart the Apache2 service to apply the changes.
$ sudo systemctl restart apache2
Use sudo systemctl reload apache2 for a graceful reload when a full restart is unnecessary.
- Allow incoming HTTP traffic through UFW when a firewall is enabled.
$ sudo ufw allow http Rules updated Rules updated (v6)
Debian commonly uses nftables/iptables instead of UFW.
- Allow incoming HTTPS traffic through UFW when a firewall is enabled.
$ sudo ufw allow https Rules updated Rules updated (v6)
- Verify the default site responds locally over HTTP.
$ curl -I http://127.0.0.1/ HTTP/1.1 200 OK Date: Sat, 13 Dec 2025 12:34:56 GMT Server: Apache/2.4.52 (Ubuntu) Last-Modified: Tue, 22 Mar 2022 10:00:00 GMT ETag: "2aa6-5da9a4f1c6c00" Accept-Ranges: bytes Content-Length: 10918 Content-Type: text/html; charset=UTF-8
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.
