How to Install and Start Apache on Linux
Apache HTTP Server is one package install away on every mainstream distro. The parts that trip people up come after that: the package and service have different names per distro, the config layout differs, and the firewall or SELinux silently blocks requests. This post covers install, service management, a first virtual host with HTTPS, and the common reasons Apache refuses to start.
Install
On Debian and Ubuntu the package is apache2:
sudo apt update
sudo apt install apache2
Debian packages start and enable the service right after install.
On AlmaLinux, Rocky Linux, RHEL and Fedora the package is httpd, and the service is not started for you:
sudo dnf install httpd
sudo systemctl enable --now httpd
The rest of the differences, side by side:
| Debian / Ubuntu | RHEL family | |
|---|---|---|
| Package and service | apache2 | httpd |
| Main config | /etc/apache2/apache2.conf | /etc/httpd/conf/httpd.conf |
| Site configs | /etc/apache2/sites-available/, enabled with a2ensite | any *.conf in /etc/httpd/conf.d/ |
| Logs | /var/log/apache2/ | /var/log/httpd/ |
| Worker user | www-data | apache |
| Control tool | apache2ctl | apachectl |
Open the firewall
Ubuntu with ufw (allow SSH first if ufw is not enabled yet, or you lock yourself out):
sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full' # ports 80 and 443
sudo ufw enable
RHEL family with firewalld:
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
On a cloud VM, the security group or network firewall must allow 80 and 443 as well.
Check from the server and from outside:
curl -I http://localhost
curl -I http://203.0.113.10
Ubuntu answers 200 OK with its default page. RHEL answers 403 Forbidden with a test page until you put an index.html in /var/www/html. That is expected.
Start, stop, reload
Use apache2 on Debian/Ubuntu and httpd on the RHEL family:
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2 # full restart, drops open connections
sudo systemctl reload apache2 # graceful: rereads config, keeps connections
sudo systemctl enable apache2 # start at boot
systemctl status apache2
After a config change, test first and reload, not restart:
sudo apache2ctl configtest # apachectl configtest on RHEL
sudo systemctl reload apache2
A syntax error caught by configtest costs nothing. The same error found by a restart takes the site down.
Add a virtual host
Create the document root and a test page:
sudo mkdir -p /var/www/example.com/public
echo 'it works' | sudo tee /var/www/example.com/public/index.html
On Ubuntu, put this into /etc/apache2/sites-available/example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public
<Directory /var/www/example.com/public>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog /var/log/apache2/example.com-error.log
CustomLog /var/log/apache2/example.com-access.log combined
</VirtualHost>
Enable it:
sudo a2ensite example.com
sudo apache2ctl configtest && sudo systemctl reload apache2
On the RHEL family, save the same block as /etc/httpd/conf.d/example.com.conf with log paths under /var/log/httpd/, then sudo apachectl configtest && sudo systemctl reload httpd. Anything under /var/www already has the right SELinux label. For a document root elsewhere, label it:
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
sudo restorecon -R /srv/www
Options -Indexes stops Apache from listing directory contents when there is no index file. AllowOverride None ignores .htaccess files, which is faster and keeps config in one place. Set it to All only if the application relies on .htaccess. sudo apache2ctl -S (or apachectl -S) lists all virtual hosts and which one is the default.
Basic hardening
Hide the version and OS from headers and error pages:
ServerTokens Prod
ServerSignature Off
On Ubuntu these lines live in /etc/apache2/conf-available/security.conf, which is enabled by default; just change the values. On RHEL, create /etc/httpd/conf.d/security.conf with them. List loaded modules with apache2ctl -M and disable what you do not use (sudo a2dismod <module> on Ubuntu).
HTTPS with Let's Encrypt
DNS for the domain must point at the server and port 80 must be reachable, because the certificate authority checks over HTTP.
# Ubuntu / Debian
sudo apt install certbot python3-certbot-apache
# AlmaLinux / Rocky (EPEL provides certbot; on RHEL enable EPEL per its docs)
sudo dnf install epel-release
sudo dnf install certbot python3-certbot-apache mod_ssl
sudo certbot --apache -d example.com -d www.example.com
sudo certbot renew --dry-run
Certbot edits the virtual host, adds the HTTPS one and can set up the HTTP to HTTPS redirect. Confirm that a renewal timer exists with systemctl list-timers | grep -i certbot.
When Apache will not start
Start with the service status and the log:
systemctl status apache2 --no-pager -l
sudo journalctl -u apache2 -n 50
sudo tail -n 50 /var/log/apache2/error.log
The usual causes:
Address already in use: AH00072: make_sock: could not bind to address [::]:80. Another process holds the port, often nginx or a second Apache. Find it withsudo ss -ltnp 'sport = :80'.AH00558: Could not reliably determine the server's fully qualified domain name. Only a warning. Set a globalServerName(for example in a small file underconf-availableenabled witha2enconf) to silence it.- Syntax errors.
configtestprints the file and line. - 403 Forbidden on your own files. The worker user needs execute permission on every parent directory and read on the files.
namei -l /var/www/example.com/public/index.htmlshows where the chain breaks. - SELinux on the RHEL family. Wrong file labels give 403, a non-standard port blocks startup. Check
sudo ausearch -m avc -ts recent. Allow an extra port withsudo semanage port -a -t http_port_t -p tcp 8080.
For stubborn cases, sudo apachectl -X runs a single process in the foreground and prints errors straight to the terminal. Stop it with Ctrl+C and start the service normally afterwards.
Quick reference
sudo apt install apache2 # Debian/Ubuntu
sudo dnf install httpd && sudo systemctl enable --now httpd # RHEL family
sudo ufw allow 'Apache Full' # or firewall-cmd --add-service=http/https
sudo apache2ctl configtest # apachectl on RHEL
sudo systemctl reload apache2 # graceful config reload
sudo a2ensite example.com # Ubuntu; conf.d/*.conf on RHEL
sudo certbot --apache -d example.com # HTTPS
sudo ss -ltnp 'sport = :80' # who holds port 80
