← Back to Apache Course | Chapter 1: Introduction & Setup | Lesson 3 of 4

Apache on Ubuntu/Linux

Ubuntu and other Debian-based distributions install Apache under the package name apache2, manage it with systemctl, and store its configuration under /etc/apache2/. This lesson walks through the exact commands to install, start, and verify Apache on Ubuntu.

Installing with apt

On Ubuntu/Debian, update the package index and install the apache2 package:

`` sudo apt update sudo apt install apache2 ``

This installs the server itself along with a default configuration and a sample "It works!" page under /var/www/html/.

Managing the Service with systemctl

Ubuntu uses systemd to manage services. After installing, Apache usually starts automatically, but you control it explicitly with:

`` sudo systemctl start apache2 sudo systemctl stop apache2 sudo systemctl restart apache2 sudo systemctl reload apache2 sudo systemctl enable apache2 ``

restart stops and starts the whole process (briefly dropping connections); reload re-reads the configuration without dropping active connections, which is safer after a small config change. enable makes Apache start automatically on every boot.

Note: Use sudo systemctl status apache2 any time something seems wrong -- it shows whether the service is running, and often prints the exact error line from a bad config.

The Ubuntu Config Layout

Ubuntu splits Apache's configuration across several directories under /etc/apache2/: apache2.conf is the main file, sites-available/ holds virtual host definitions you've created, sites-enabled/ holds symlinks to the ones actually active, and mods-available//mods-enabled/ do the same for modules. This is a Debian-specific convention -- RHEL-based systems use a single flatter conf.d/ directory instead.

Enabling and Disabling Sites and Modules

Rather than manually editing symlinks, Ubuntu provides helper commands: a2ensite/a2dissite enable or disable a virtual host from sites-available/, and a2enmod/a2dismod do the same for modules. For example, sudo a2enmod rewrite enables mod_rewrite, and you then reload Apache for it to take effect.

Warning: a2enmod/a2ensite only create the symlink -- Apache doesn't pick up the change until you run sudo systemctl reload apache2 afterward.

Verifying the Install

Once the service is running, confirm it two ways: run curl -I localhost on the server itself and look for HTTP/1.1 200 OK, then visit the server's IP address in a browser from another machine and confirm you see the default Apache Ubuntu welcome page.

Example: Installing and enabling Apache on Ubuntu

apacheconf
# Install Apache
sudo apt update
sudo apt install apache2

# Start it now, and on every future boot
sudo systemctl start apache2
sudo systemctl enable apache2

# Check it's running
sudo systemctl status apache2
curl -I localhost
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Editing a file directly inside sites-enabled/ -- it's a symlink to sites-available/, so changes are better made on the real file there, and re-enabled with a2ensite after renaming.
  2. Forgetting sudo systemctl reload apache2 after a2enmod/a2ensite, then wondering why the module or site doesn't seem to be active.
  3. Using apt install httpd (the RHEL package name) on Ubuntu, which doesn't exist -- Ubuntu's package is apache2.
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.