How to Install Apache on Ubuntu and Debian in Four Commands

Terminal running apt, ufw and systemctl commands next to a browser showing the Apache2 Default Page with It works!

To install Apache on Ubuntu or Debian, you refresh apt's package list, install the apache2 package, allow web traffic through the UFW firewall, and enable the service with systemctl. Those four commands take a bare Linux server to a page you can open in your browser.

This guide walks through each step in order and explains why it matters. It also shows how to confirm Apache is really serving pages and how to fix the three problems behind most failed installs. Because Ubuntu and Debian both use the apt package manager, the steps are the same on either one.

In short
  • Run sudo apt update to refresh the package list before installing.
  • Install Apache with sudo apt install apache2; the package is apache2, not apache.
  • Open port 80 with sudo ufw allow 'Apache', and allow OpenSSH before enabling UFW.
  • Use sudo systemctl enable --now apache2 so Apache runs now and after every reboot.
  • Confirm with systemctl status apache2, then open your server's IP in a browser.
  1. 0:00Intro
  2. 0:27What you'll build and need
  3. 1:13Update your package index
  4. 2:07Install the apache2 package
  5. 3:01Allow web traffic through UFW
  6. 4:00Enable and start the service
  7. 4:51Verify Apache is serving pages
  8. 5:43Fix the three common errors
  9. 6:33Four commands to a live server
  10. 7:05Four commands give you a web server

Check what you need before you start

The goal is simple and easy to test: you type your server's address into a browser and get a page back from Apache. Not a timeout, and not an error. Keeping that finish line in mind helps you tell whether each step has worked, and it gives you a clear signal when something needs fixing.

You only need four things. Installing software changes the system, so you need admin rights through sudo. You also need a terminal, either directly on the machine or over SSH, and a working internet connection so apt can download packages from its repositories.

If you are on Debian rather than Ubuntu, nothing in the main steps changes, because both distributions use apt. The one difference worth knowing in advance is that Debian may not include the UFW firewall by default. The firewall step below covers what to do.

  • A server running Ubuntu or Debian
  • A user account with sudo access
  • Terminal access, local or over SSH
  • An internet connection for apt downloads

Update your package index with apt

apt keeps a local list of every package it can install, and that list goes stale over time. If you install from an old list, you may not get the current version of Apache. So the first command refreshes the catalogue: sudo apt update. It contacts your configured repositories, reads the package lists, and builds the dependency tree.

This command does not install, upgrade, or change any software already on your server. It only updates apt's knowledge of what is available. When you see Done and your prompt comes back, you are ready for the next step.

Read the output rather than skipping past it. Lines starting with Err, or messages saying Failed to fetch, usually mean a network or DNS problem. Fix that before going further, because without a working package index the install in the next step will fail.

  • Output ends with Done and returns to the prompt
  • No lines starting with Err
  • No Failed to fetch messages
sudo apt update

Install the apache2 package

On Ubuntu and Debian the package is named apache2, not apache. That small detail catches a lot of people out. Run sudo apt install apache2 and apt works out everything Apache needs to run. It then lists the new packages it will add: apache2 itself plus apache2-bin, apache2-data and apache2-utils.

Press y to confirm. When you see Setting up apache2, the install is finished. If you are writing a setup script and don't want apt to stop and ask, add -y to the command. apt then answers yes for you, so the install runs without waiting for input.

Two folders are worth remembering from this point on. Apache's configuration lives in /etc/apache2, and the default web page sits in /var/www/html. Those are the places you will return to most often, whether you are changing settings or publishing your own content.

  • The package list includes apache2, apache2-bin, apache2-data and apache2-utils
  • Output shows Setting up apache2
  • Config is in /etc/apache2
  • Web files are in /var/www/html
sudo apt install apache2

Allow web traffic through the UFW firewall

Apache can be installed and running and still be unreachable if a firewall blocks the traffic. Browsers talk to Apache on port 80 by default, so that port must be open. On Ubuntu, the usual tool is UFW, the Uncomplicated Firewall.

Start with sudo ufw app list. Apache registers its own profiles here: Apache, Apache Full and Apache Secure, listed alongside OpenSSH. Then run sudo ufw allow 'Apache', with the name in quotes. When UFW replies Rules updated, the rule is saved. From then on, a browser's HTTP request reaches UFW, UFW allows port 80, and the request gets through to Apache. Without the rule, it stops at the firewall.

Be careful at this step. On Debian, UFW may not be installed, so add it with apt first. And if you switch UFW on, allow OpenSSH before you do. Otherwise you can lock yourself out of your own server over SSH.

  • ufw app list shows the Apache profiles
  • ufw allow 'Apache' returns Rules updated
  • OpenSSH is allowed before UFW is enabled
  • UFW is installed if you are on Debian
sudo ufw app list
sudo ufw allow 'Apache'

Enable and start the Apache service

Next you want Apache running now and running again after every reboot. A single command does both: sudo systemctl enable --now apache2. systemd syncs the service state and runs its install helper. To check the result, run systemctl is-enabled apache2. It should reply enabled.

The two parts of the command do different things. enable on its own only tells the system to start Apache at boot. Adding --now also starts it immediately, so you don't need a separate start command.

Ubuntu and Debian usually start Apache as soon as it is installed, so this step may seem unnecessary. Running it anyway costs nothing and makes sure the service is enabled for boot. That matters the first time your server restarts and you expect your site to come back without any help.

  • enable --now runs without errors
  • systemctl is-enabled apache2 replies enabled
sudo systemctl enable --now apache2
systemctl is-enabled apache2

Verify Apache is serving pages

Check in two places. The service status tells you Apache is running on the server, and a browser tells you people can actually reach it. Both can pass or fail independently, so check both. Run systemctl status apache2 and look for Loaded with enabled, and Active: active (running). If the status output holds the terminal, press q to get your prompt back.

Next, find your server's address with hostname -I, which prints its IP address. Open that IP in a browser. You should see the Apache2 Default Page with the words It works! That page is the file index.html in /var/www/html.

Once you see that page, the install has done its job. To publish your own content, edit /var/www/html/index.html and refresh the browser. Your changes are live straight away, which was the goal from the beginning.

  • Status shows Loaded with enabled
  • Status shows Active: active (running)
  • hostname -I prints the server's IP
  • The browser shows the Apache2 Default Page with It works!
systemctl status apache2
hostname -I

Fix the three most common Apache errors

If the check didn't go to plan, the cause is almost always one of three things: a busy port, a closed firewall, or a typo in the configuration. Match what you see to one of these and apply that fix.

If Apache reports that port 80 is already in use, another web server such as nginx owns it. Find the program with sudo ss -ltnp and stop it. If the browser times out, traffic is being blocked, so rerun sudo ufw allow 'Apache' and check your cloud provider's firewall as well. If Apache fails to start or reload, run sudo apache2ctl configtest. It points to the broken line, and Syntax OK means the configuration is clean.

When none of those match, read the logs. journalctl -u apache2 shows why the service stopped, and /var/log/apache2/error.log usually names the exact cause.

  • Address already in use: find the process with ss -ltnp and stop it
  • Browser timeout: recheck the UFW rule and any cloud firewall
  • Fails to start or reload: run apache2ctl configtest
  • Still unclear: check journalctl -u apache2 and the error log

Key takeaways

  • Order matters: update, install, allow, enable, then verify.
  • The package is apache2, and its files live in /etc/apache2 and /var/www/html.
  • The UFW rule opens port 80. Allow OpenSSH before enabling the firewall.
  • systemctl enable --now starts Apache and keeps it running after reboots.
  • Most failures are a busy port, a blocked firewall, or a config typo.
  • A good next step is adding free HTTPS with Let's Encrypt.

Frequently asked questions

Is the Apache package called apache or apache2 on Ubuntu?

On Ubuntu and Debian the package is apache2. Running sudo apt install apache2 installs Apache along with apache2-bin, apache2-data and apache2-utils.

Does sudo apt update upgrade my installed software?

No. It only downloads fresh package lists from your repositories so apt knows what is currently available. It does not upgrade or change anything already installed.

Are the steps different on Debian?

The main steps are the same because Debian also uses apt. The difference is that UFW may not be installed on Debian, so you may need to add it with apt before running the firewall commands.

Why does my browser time out when I open my server's IP?

A timeout usually means a firewall is blocking port 80. Rerun sudo ufw allow 'Apache' and check any firewall your cloud provider applies to the server.

Where do I put my own website files?

The default page is /var/www/html/index.html. Edit that file, or replace it with your own, and refresh the browser to see your content.

How do I find out why Apache won't start?

Run sudo apache2ctl configtest to check for configuration typos. If that reports Syntax OK, look at journalctl -u apache2 and /var/log/apache2/error.log for the exact cause.

Watch the full video on YouTube →

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
Ad