Apache web server: what it is and how to use it

What the Apache web server does, how modules, virtual hosts and .htaccess work, where its files live and the commands you use every day.

7–10 minutes
Front panels of rack-mounted servers

The ApacheApache One of the most common programs that sends web pages to visitors. It is the waiter between your website’s files and the people asking for them. More about Apache → web server (officially the Apache HTTP Server) is free, open-source software that runs on a server and sends web pages to visitors’ browsers. This guide is for anyone who has seen the name on a hosting plan, in a cPanel server or in an error message and wants to know what it does, how its configuration fits together and which commands they will actually use.

Apache is a web serverWeb server The program that answers visitors’ browsers and sends them your website. Also used for the computer it runs on. More about Web server →: it listens on portsPort A numbered door on a server. Each service listens behind its own door: web pages on 443, email on others, remote login on 22. More about Port → 80 and 443, receives each request a browser sends and replies with a file or with the output of a program such as PHP. It has been developed by the Apache Software Foundation since the 1990s and is known for **per-folder settings in .htaccess files** and a large set of loadable modules. On Ubuntu and Debian the package is apache2 and its settings live in /etc/apache2/; on AlmaLinux and Rocky Linux it is httpd in /etc/httpd/. After any change, **test with sudo apachectl configtest, then reload**.

What Apache does

When someone visits https://example.com, their browser looks up the domain’s IP addressIP address The number address of a device on the internet, like 203.0.113.10. Computers use it to find each other, the way post uses street addresses. More about IP address → (say 203.0.113.10) and opens a connection to that server. A program on the server has to accept the connection, read the request (“send me /about/”) and send something back. That program is the web server, and on a very large number of servers, including most cPanelcPanel The most popular hosting control panel: a website where you manage your email, files, databases and domains with point-and-click tools instead of commands. More about cPanel → servers, it is Apache.

Apache does three main jobs, often at the same time:

JobWhat it meansExample
Serve filesSends files from a folder straight to the browser/var/www/example.com/public/logo.png
Run or pass to applicationsHands requests for scripts to PHP and returns the resultWordPress pages built by PHP-FPM
Handle HTTPSHolds the SSL certificate and encrypts the connectionA Let’s Encrypt certificate for example.com

It can also act as a reverse proxyReverse proxy A front door that receives every visitor and passes them to the right program behind it, which stays hidden. More about Reverse proxy → (passing requests on to another program, such as a Node.js app on port 3000), redirect old addresses to new ones and rewrite pretty URLs like /blog/my-post/ into something an application understands.

How Apache is built: modules and MPMs

Apache is a small core plus modules, add-ons you switch on for each feature you need. A few you will meet often:

  • mod_rewrite: rewrites and redirects URLs. WordPress permalinks depend on it.
  • mod_ssl: adds HTTPSHTTPS The secure version of the web, shown by the padlock. Everything sent between you and the site is scrambled so nobody in between can read or change it. More about HTTPS → support.
  • mod_headers: adds or changes response headers, such as cachingCache A saved copy of something, kept so it does not have to be made or fetched again. Like keeping a printed copy instead of reprinting it for every person who asks. More about Cache → or security headers.
  • mod_proxy and mod_proxy_fcgi: pass requests to other programs, including PHP-FPMPHP-FPM The part of the server that runs PHP code, the language WordPress and many sites are written in, and hands the finished page to the web server. More about PHP-FPM →.

How Apache handles many visitors at once is decided by its MPM (multi-processing module). Only one is active at a time:

MPMHow it worksWhen you see it
preforkOne separate process per connection, no threadsOlder setups that run PHP inside Apache with mod_php
workerSeveral processes, each with many threadsLess common today
eventLike worker, but idle keep-alive connections are parked cheaplyThe usual default on current Ubuntu, Debian and RHEL-based systems

The practical point: event with PHP-FPM uses far less memory than prefork with mod_php for the same traffic. If your server runs out of memory under load, the MPM is one of the first things to check.

Apache compared with Nginx

Nginx is the other web server you will hear about most. Both are free, mature and fast enough for almost any site. The real differences are in how you configure them.

ApacheNginx
Per-folder settingsYes, with .htaccess filesNo, all settings live in the main configuration
Runs PHPThrough PHP-FPM, or mod_php on older setupsOnly through PHP-FPM
Typical homecPanel servers, shared hosting, many WordPress hostsVPS setups, reverse proxies, high-traffic static sites
Configuration styleXML-like blocks such as VirtualHost and DirectoryCurly-brace blocks such as server and location

Some servers run both: NginxNginx A fast, popular program that sends web pages to visitors and can pass requests on to apps behind it. More about Nginx → in front to handle HTTPS and static files, and Apache behind it to run the application and read .htaccess files. If you are building a new server from scratch, either is a sound choice; if you are on shared hostingShared hosting Hosting where your website shares one server with many others. Cheap and simple; the host looks after the server. More about Shared hosting → or cPanel, you are almost certainly on Apache already.

How Apache configuration works

Where the files are

The layout depends on the Linux family. This is the part that trips most people up when they follow a tutorial written for the other one.

Ubuntu and DebianAlmaLinux and Rocky Linux
Package and service nameapache2httpd
Main configuration file/etc/apache2/apache2.conf/etc/httpd/conf/httpd.conf
Site configurations/etc/apache2/sites-available/ (switched on in sites-enabled/)/etc/httpd/conf.d/ (any file ending in .conf)
Default web folder/var/www/html/var/www/html
Logs/var/log/apache2/access.log and error.log/var/log/httpd/access_log and error_log
User Apache runs aswww-dataapache

Virtual hosts: one server, many sites

A virtual host is a block that tells Apache which folder to serve for which domain. One server can host dozens of sites this way, all on the same IP address. Apache picks the right one by matching the domain the browser asked for against each ServerName and ServerAlias.

A typical virtual host for example.com, saved as /etc/apache2/sites-available/example.com.conf on Ubuntu or /etc/httpd/conf.d/example.com.conf on AlmaLinux and Rocky:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/apache2/example.com-error.log
    CustomLog /var/log/apache2/example.com-access.log combined
</VirtualHost>

On AlmaLinux and Rocky, change the two log paths to /var/log/httpd/. DocumentRoot is the folder visitors see, Require all granted lets everyone read it, and AllowOverride All allows .htaccess files in that folder to change settings.

.htaccess files

An .htaccess file is a small configuration file placed inside a website folder. Apache reads it on every request and applies its rules to that folder and everything below it. This is how WordPress, and shared hosting customers in general, set redirects and rewrites without access to the main configuration.

Two things to know:

  • .htaccess only works where AllowOverride permits it. On Ubuntu and Debian the default for /var/www/ is AllowOverride None, so .htaccess is ignored until you allow it in the virtual host, as in the example above.
  • Because Apache checks for these files on every request, the official advice is to put rules in the main configuration when you control the server. Use .htaccess when you cannot, or when an application like WordPress expects to manage it.

Install Apache and serve your first site

On Ubuntu or Debian

sudo apt update
sudo apt install apache2
sudo systemctl enable --now apache2
sudo ufw allow "Apache Full"

"Apache Full" is a firewall profile that opens ports 80 and 443. Visit http://203.0.113.10 (your server’s IP) and you should see the “Apache2 Default Page”.

To switch on the example.com site and the rewrite module:

sudo mkdir -p /var/www/example.com/public
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo a2enmod rewrite
sudo apachectl configtest
sudo systemctl restart apache2

a2ensite and a2enmod simply create links in sites-enabled/ and mods-enabled/; a2dissite and a2dismod remove them. After switching a module on or off, a2enmod asks for a restart, so this first time use restart; for later changes to site files, reload is enough.

On AlmaLinux or Rocky Linux

sudo dnf install httpd
sudo systemctl enable --now httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Here there is no a2ensite: any .conf file you place in /etc/httpd/conf.d/ is active after a reload. Install mod_ssl (sudo dnf install mod_ssl) before you add HTTPS.

Add HTTPS

Once the domain’s DNSDNS The internet’s phone book. It turns a name people can remember, like example.com, into the number address computers use to find the server. More about DNS → points at the server, Certbot can fetch a free certificate and edit the virtual host for you. On Ubuntu and Debian:

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

The full process, including renewals, is in How to get a free SSL certificate with Let’s Encrypt.

The commands you will use every day

TaskUbuntu and DebianAlmaLinux and Rocky
Test the configurationsudo apachectl configtestsudo apachectl configtest
Apply changes without dropping visitorssudo systemctl reload apache2sudo systemctl reload httpd
Full restartsudo systemctl restart apache2sudo systemctl restart httpd
Is it running?systemctl status apache2systemctl status httpd
List sites and which file defines eachsudo apachectl -Ssudo apachectl -S
List loaded modulessudo apachectl -Msudo apachectl -M

Make it a habit: test, then reload. configtest should print Syntax OK. If it prints an error with a file name and line number, fix that line before reloading, because a restart with broken configuration leaves the site down.

When something goes wrong, the error log usually names the cause. Watch it live while you load the page:

sudo tail -f /var/log/apache2/error.log

On AlmaLinux and Rocky, use /var/log/httpd/error_log.

Common problems

SymptomCauseFix
AH00558: Could not reliably determine the server’s fully qualified domain nameNo global ServerName is set. It is only a warningAdd a line such as ServerName example.com to the main configuration file, then reload
Apache will not start: Address already in use, could not bind to port 80Another program, often Nginx, already uses the portFind it with sudo ss -tlnp, then stop it or move one of them to another port
You see the Apache default page instead of your siteYour virtual host is not enabled, or the domain does not match ServerNameRun sudo apachectl -S, enable the site, check ServerName and ServerAlias
WordPress home page works but every other page gives 404mod_rewrite is off or AllowOverride is NoneEnable rewrite and set AllowOverride All for the site folder, then reload
403 ForbiddenFolder permissions, a missing index file or a Require ruleSee How to fix 403 Forbidden
500 Internal Server Error after editing .htaccessA typo or a directive for a module that is not loadedCheck the error log, undo the last change, see How to fix 500 Internal Server Error

If the page does not load at all and the browser says the connection was refused, Apache is probably stopped or a firewallFirewall A gatekeeper that decides which connections are allowed into a server and blocks the rest. More about Firewall → is blocking the port. ERR_CONNECTION_REFUSED walks through those checks.

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.