Apache on Ubuntu/Linux
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.In this page:
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.
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.
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
# 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). #}
- Editing a file directly inside
sites-enabled/-- it's a symlink tosites-available/, so changes are better made on the real file there, and re-enabled witha2ensiteafter renaming. - Forgetting
sudo systemctl reload apache2aftera2enmod/a2ensite, then wondering why the module or site doesn't seem to be active. - Using
apt install httpd(the RHEL package name) on Ubuntu, which doesn't exist -- Ubuntu's package isapache2.
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: