Skip to content
DBDeependra Bhatta~/notes
Linux#apache · #networking · #linux

Apache Web Server on Ubuntu: Install and Host a Static Site

Install Apache on Ubuntu, learn how /etc/apache2 is organised, and serve a static HTML template from /srv through a dedicated virtual host.

· updated · 6 min read
ON THIS PAGE

Apache HTTP Server is one of the most widely deployed web servers, and on a fresh Linux VM it exercises networking, file permissions and systemctl in a single setup. This guide installs Apache on Ubuntu, explains how its configuration is split across /etc/apache2, and publishes a static HTML template from /srv through a dedicated virtual host.

Prerequisites

  • An Ubuntu server or VM with sudo access.
  • Port 80 reachable from the machine you browse from.

Overview

A web server answers HTTP requests in front of your content or application. It serves static files, terminates TLS (HTTPS), writes access logs, and forwards requests to backend applications.

Apache and Nginx can both act as a reverse proxy: a server between clients and one or more backend servers that forwards requests and returns the responses, so clients never reach the backends directly. A reverse proxy is also the basis for load balancing and zero-downtime deployments.

Apache compared with Nginx

ApacheNginx
How it handles connectionsMulti-Processing Modules (MPMs): processes or threads per connection, or the event MPMEvent-driven: a few worker processes handle many connections each
Per-directory configSupports .htaccess filesNo .htaccess; all config is central
Extending itLarge set of modules, loaded with a2enmod on UbuntuModules, many compiled in
Typical useTraditional hosting, PHP apps, .htaccess-based sitesStatic files, reverse proxy, high connection counts

Both are mature and secure when patched; the choice usually follows the team's existing expertise.

Installation

terminal
$ sudo apt update
$ sudo apt install apache2
$ sudo systemctl enable --now apache2
$ systemctl status apache2
$ curl localhost

enable --now starts Apache and enables it at boot. curl localhost returns the HTML of Ubuntu's default "It works" page. The systemctl commands are explained in Linux Commands.

How /etc/apache2 is organised

On Ubuntu and Debian, Apache's configuration is split into small files:

TXTPlain text
/etc/apache2/
├── apache2.conf          main file, includes everything below
├── ports.conf            ports to listen on (80, and 443 when SSL is enabled)
├── envvars               environment variables: run user, log and PID paths
├── magic                 rules to detect a file's MIME type from its first bytes
├── mods-available/       all modules (*.load loads the code, *.conf configures it)
├── mods-enabled/         symlinks to the modules in use
├── conf-available/       extra global config snippets
├── conf-enabled/         symlinks to the snippets in use
├── sites-available/      one virtual host file per site
└── sites-enabled/        symlinks to the sites being served

The *-available directories hold every installed option; the *-enabled directories hold symbolic links to the active ones. Manage those links with Ubuntu's helper commands rather than by hand:

EnableDisableFor
a2ensitea2dissiteSites (virtual hosts)
a2enmoda2dismodModules, for example a2enmod rewrite
a2enconfa2disconfConfig snippets

After any change, check the syntax and reload:

terminal
$ sudo apache2ctl configtest
$ sudo systemctl reload apache2

Log rotation is not part of /etc/apache2. It is handled by the logrotate tool, using /etc/logrotate.d/apache2, which compresses and deletes old files in /var/log/apache2/ so logs do not fill the disk.

Host a static website

The site in this example is the free Tooplate HTML template 2103_central.

1. Find the download URL

The download button does not expose the zip URL. Open the browser developer tools, switch to the Network tab, click download, and copy the URL of the .zip request:

Browser developer tools Network tab listing the 2103_central.zip request among other page requests

2. Download and copy the files

terminal
$ cd /tmp
$ wget <zip-url-from-devtools>
$ unzip 2103_central.zip -d /tmp/site
$ sudo mkdir -p /srv/mysite
$ sudo cp -r /tmp/site/2103_central/. /srv/mysite/

Confirm the extracted folder name with ls /tmp/site; it matches the template name. /srv is the standard place for data a server serves (see Introduction to Linux).

3. Give Apache access to the files

Apache's worker processes run as the www-data user on Ubuntu, so that user must be able to read the files:

terminal
$ sudo chown -R www-data:www-data /srv/mysite

4. Allow Apache to serve /srv

By default, Apache only serves files under /var/www and /usr/share. Ubuntu's apache2.conf already has a block for /srv, but it is commented out. Remove the # from these lines:

APAapache2.conf
etcapache2apache2.conf
<Directory /srv/>
	Options Indexes FollowSymLinks
	AllowOverride None
	Require all granted
</Directory>

Without this, the site returns 403 Forbidden.

5. Create a virtual host

A virtual host tells Apache which directory to serve for a site. Copy the default virtual host and change DocumentRoot:

terminal
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.conf
$ sudo vim /etc/apache2/sites-available/mysite.conf
APAmysite.conf
etcapache2sites-availablemysite.conf
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot /srv/mysite
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

6. Switch sites and reload

terminal
$ sudo a2ensite mysite.conf
$ sudo a2dissite 000-default.conf
$ sudo apache2ctl configtest
$ sudo systemctl reload apache2
$ curl localhost

curl localhost now returns the template's HTML, and the site loads in a browser at the VM's IP address.

Troubleshooting

SymptomLikely cause
403 ForbiddenThe <Directory /srv/> block is still commented out, or www-data cannot read the files
Still showing the default "It works" page000-default.conf is still enabled, or Apache was not reloaded
Apache will not start after a changeSyntax error; run sudo apache2ctl configtest and check /var/log/apache2/error.log
Works with curl localhost but not from the hostVM network or firewall; check the VM's IP and that port 80 is reachable

Key takeaways

  • On Ubuntu, Apache's config lives in /etc/apache2, split into *-available files and *-enabled symlinks.
  • Use a2ensite, a2enmod and a2enconf (and their a2dis* partners) instead of making links by hand.
  • Serving from outside /var/www needs a <Directory> block that grants access.
  • Files must be readable by www-data.
  • Run apache2ctl configtest before every reload.

For everything else, the Apache HTTP Server 2.4 documentation and its virtual host guide are the official references.