How To Install Apache2 Web Server on Debian Based Linux

Here is an article on how to install Apache 2 (also known as Apache HTTP Server) on a Debian-based system (like Debian, Ubuntu, or other Debian derivatives).

Step 1: Update the System

Before installing Apache, it’s essential to ensure that your package list is up to date. This will make sure you are installing the latest available version of Apache.

Open the terminal.

Run the following command to update the package lists for upgrades and new package installations:
sudo apt update

Optionally, you can run the following command to upgrade any outdated packages:
sudo apt upgrade

Step 2: Install Apache 2

To install Apache 2, use the apt package manager.

Install Apache by running the following command:
sudo apt install apache2

During installation, the system may ask for your confirmation. Press Y and hit Enter to proceed.

Step 3: Verify Apache Installation

Once Apache is installed, you can check if it’s running properly:

Check Apache Status:
Apache starts automatically after installation. To check its status, run:
sudo systemctl status apache2

You should see output indicating that Apache is active and running. It will look something like this:
● apache2.service – Apache2 Web Server

   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)

   Active: active (running) since Thu 2021-03-25 10:37:21 UTC; 1h 20min ago

 If it’s not running, you can start it manually with:
sudo systemctl start apache2

Step 4: Enable Apache to Start on Boot

To ensure Apache starts automatically when the system boots up, use the following command:

sudo systemctl enable apache2

This command enables Apache to start automatically during system startup.

Step 5: Check Apache Installation in a Browser

At this point, Apache should be installed and running. To verify that Apache is working correctly:

  1. Open a web browser.
  2. Type http://localhost/ or http://your_server_ip/ (replace your_server_ip with your server’s actual IP address) in the address bar.
    • If you are running the command on your local machine, http://localhost/ should show the default Apache2 Debian page.
    • If you’re running it on a remote server, http://<your_server_ip>/ should show the same Apache welcome page.
  3. This confirms that Apache is serving web pages correctly.

Step 6: Configure Firewall (Optional)

If you have a firewall enabled on your system (for example, UFW), you will need to allow HTTP traffic through port 80 (and optionally HTTPS through port 443).

To allow HTTP traffic on port 80, run:
sudo ufw allow ‘Apache’

For HTTPS traffic on port 443, you can run:
sudo ufw allow ‘Apache Secure’

To check your firewall settings, use:
sudo ufw status

Step 7: Configure Apache

At this point, you can begin configuring Apache. By default, Apache serves files from the /var/www/html directory.

The main Apache configuration file is located at /etc/apache2/apache2.conf.

To edit this file, run:
sudo nano /etc/apache2/apache2.conf

  1. Apache configuration includes various settings, such as document root, log files, and modules. You can customize it according to your needs.
  2. Apache allows for configuration files to be placed in additional directories:
    • Sites configuration: /etc/apache2/sites-available/
    • Logs: /var/log/apache2/

Step 8: Set Up Virtual Hosts (Optional)

Virtual Hosts allow you to run multiple websites on a single server.

To set up a basic virtual host, you can create a new configuration file in the /etc/apache2/sites-available/ directory:
sudo nano /etc/apache2/sites-available/example.com.conf

Add the following configuration to the file (adjust the DocumentRoot and ServerName as needed):
<VirtualHost *:80>

    ServerAdmin webmaster@localhost

    ServerName example.com

    DocumentRoot /var/www/example.com

    ErrorLog ${APACHE_LOG_DIR}/error.log

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

After creating the virtual host file, create the document root directory:
sudo mkdir /var/www/example.com

Create a simple index.html file to test:
echo “Welcome to example.com” | sudo tee /var/www/example.com/index.html

Enable the site configuration and reload Apache:
sudo a2ensite example.com.conf

sudo systemctl reload apache2

Step 9: Disable Default Apache Site

If you want to remove the default site and use your own configuration, you can disable the default site:

sudo a2dissite 000-default.conf

Then reload Apache:

sudo systemctl reload apache2

Step 10: Install PHP

If you need to run PHP on your Apache server, install PHP by running:

sudo apt install php libapache2-mod-php

After installation, you can test PHP by creating a PHP file in the document root:

Create a info.php file:
sudo nano /var/www/html/info.php

Add the following content to the file:

<?php phpinfo(); ?>

  1. Visit http://localhost/info.php in your browser, and you should see a page displaying detailed PHP information.

Step 11: Restart Apache 

Anytime you make changes to the configuration files, restart Apache to apply them:

sudo systemctl restart apache2

You have successfully installed and configured Apache 2 on your Debian system. You can now host websites, configure virtual hosts, and even integrate Apache with PHP to create dynamic web applications. Don’t forget to secure your server, especially if it’s accessible from the internet. You can configure SSL certificates, enable HTTPS, and regularly update Apache to keep your server secure.

Similar Posts