Read in 4 minutes
How to Install Apache on Debian 9
Apache HTTP server is one of the most popular web servers in the world. It is an open-source and cross-platform HTTP server that powers a large percentage of the Internet’s websites. Apache provides many powerful features that can be extended through additional modules.
In this tutorial, we will go through the steps of installing Apache on a Debian 9 server.
Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.
Apache is available in the default Debian repositories and the installation is pretty straightforward.
First update the package index and afterwards install the
apache2 package with the following commands:
sudo apt update sudo apt install apache2
That’s it, Apache is installed and automatically started, you can check the status of the Apache service with:
sudo systemctl status apache2
● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-08-23 20:04:47 UTC; 13s ago Main PID: 11604 (apache2) CGroup: /system.slice/apache2.service ├─11604 /usr/sbin/apache2 -k start ├─11608 /usr/sbin/apache2 -k start └─11609 /usr/sbin/apache2 -k start
Adjust the Firewall
If you use iptables to filter connections to your system, you’ll need to open HTTP (
80) and HTTPS (
Open the necessary ports by issuing the following command:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
Verifying Apache Installation
To verify that everything works correctly, open your browser, type your server IP address or doman name
http://YOUR_IP_OR_DOMAIN/ and you will see the default Apache welcome page as shown below:
The page includes some basic information about Apache configuration files, helper scripts and directory locations.
Manage the Apache service with systemctl
Now that Apache installed on your Debian system, you can take a minute to look over and become familiar with the basic apache service management commands:
To stop the Apache service, run:
sudo systemctl stop apache2
Start it again, by typing:
sudo systemctl start apache2
To restart the Apache service:
sudo systemctl restart apache2
Reload the Apache with a new configuration with:
sudo systemctl reload apache2
If you want to disable the Apache service to start at boot:
sudo systemctl disable apache2
And to re-enable it again:
sudo systemctl enable apache2
Apache Configuration File’s Structure and Best Practices
- In Debian based systems Apache configuration files are located in the
- The main Apache configuration file is
- The ports that Apache will listen to are specified in the
- Apache Virtual Hosts files are located in the
/etc/apache2/sites-availabledirectory. The configuration files found in this directory are not used by Apache unless they are linked to the
- You can activate a virtual host directive by creating a symlink using the
a2ensitecommand from the configuration files found in the
sites-availabledirectory to the
sites-enableddirectory. To deactivate a virtual host use the
- It is highly recommended to follow the standard naming convention, for example if your domain name is
mydomain.comthen the domain configuration file should be named
- Configuration files that are used for loading various Apache modules are located in the
/etc/apache2/mods-availabledirectory. Configurations in the
mods-availabledirectory can be enabled by creating a symlink to the
/etc/apache2/mods-enabledirectory using the
a2enconfcommand and disabled with the
- Files containing global configuration fragments are stored in the
/etc/apache2/conf-availabledirectory. Files in the
conf-availabledirectory can be enabled by creating a symlink to the
a2enconfcommand and disabled with the
- Apache log files (
error.log) are located in the
/var/log/apachedirectory. It is recommended to use a different
errorlog files for each virtual host.
- You can set your domain document root directory to any location you want. The most common locations for webroot include:
You have successfully installed Apache on your Debian 9 server. You can now start deploying your applications and use Apache as a web or proxy server.
If you have any question or feedback, feel free to leave a comment.