How to Install Apache on Debian 9

Updated on

4 min read

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.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges .

Installing Apache

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 updatesudo 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 (443) ports.

Open the necessary ports by issuing the following command:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPTsudo 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 domain name http://YOUR_IP_OR_DOMAIN/ and you will see the default Apache welcome page as shown below:

Apache welcome page

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 /etc/apache2 directory.
  • The main Apache configuration file is /etc/apache2/apache2.conf.
  • The ports that Apache will listen to are specified in the /etc/apache2/ports.conf file.
  • Apache Virtual Hosts files are located in the /etc/apache2/sites-available directory. The configuration files found in this directory are not used by Apache unless they are linked to the /etc/apache2/sites-enabled directory.
  • You can activate a virtual host directive by creating a symlink using the a2ensite command from the configuration files found in the sites-available directory to the sites-enabled directory. To deactivate a virtual host use the a2dissite command.
  • It is highly recommended to follow the standard naming convention, for example if your domain name is mydomain.com then the domain configuration file should be named /etc/apache2/sites-available/mydomain.com.conf
  • Configuration files that are used for loading various Apache modules are located in the /etc/apache2/mods-available directory. Configurations in the mods-available directory can be enabled by creating a symlink to the /etc/apache2/mods-enable directory using the a2enconf command and disabled with the a2disconf command.
  • Files containing global configuration fragments are stored in the /etc/apache2/conf-available directory. Files in the conf-available directory can be enabled by creating a symlink to the /etc/apache2/conf-enabled using the a2enconf command and disabled with the a2disconf command.
  • Apache log files (access.log and error.log) are located in the /var/log/apache directory. It is recommended to use different access and error log 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:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>

Conclusion

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 questions or feedback, feel free to leave a comment.

This post is a part of the How to Install LAMP Stack on Debian 9 series.
Other posts in this series: