How To Install Apache web server on Debian 11

In this article, we want to teach you how to install the Apache web server on Debian 11.

The Apache HTTP Server is free and open-source software that allows users to deploy their websites on the internet.

How To Install Apache web server on Debian 11

You need some requirements to install the Apache web server on Debian 11.

Requirements for Installing Apache on Debian 11

You need to log in as a non-root user with Sudo privileges into your server and a basic setup for a firewall. to do this you can check our article about the Initial server setup with Debian 11.

Install Apache on Debian 11

Apache is available on the Debian repository by default. to install Apache on Debian 11 you need to update and upgrade the packages. run the following command:

sudo apt update
sudo apt upgrade

Now you can install Apache with the following command:

sudo apt install apache2

When your installation of Apache on Debian 11 is completed. you need to adjust the Firewall.

How to adjust the Firewall on Debian 11

You should have a UFW firewall. we assumed that you are done with the requirements that we said at the beginning of the article.

You should modify the firewall settings to allow outside access to the default web ports.

Run the following command to check the available applications through the firewall:

sudo ufw app list
Output
Available applications:
...
WWW
WWW Cache
WWW Full
WWW Secure
...

In this article, you only need to allow traffic on port 80. run the following command:

sudo ufw allow 'WWW'

Verify the change with the following command:

sudo ufw status

In your output, you should see something similar to this:

Output
Status: active
To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
WWW                        ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
WWW (v6)                   ALLOW       Anywhere (v6)

Let’s see how to check your Apache web server on Debian11.

How to Check the Apache webserver

At the end of the installation process, Debian 11 starts Apache.

The Apache web server should already be up and running. To check this run the following command:

sudo systemctl status apache2

In your output, you should see that your service is active and running similar to this:

Output
apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
     Active: active (running) since Thu 2021-09-02 03:30:29 EDT; 2min 51s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3602 (apache2)
      Tasks: 55 (limit: 2340)
     Memory: 11.0M
        CPU: 69ms
     CGroup: /system.slice/apache2.service
             ├─3602 /usr/sbin/apache2 -k start
             ├─3604 /usr/sbin/apache2 -k start
             └─3605 /usr/sbin/apache2 -k start

Now you can access the default Apache web page. To do this you need your server’s IP address. If you don’t have your IP address you can get it with these ways.

Run the following command to get your IP address:

hostname -I

You will get back a few addresses separated by spaces. You can try each in your web browser to see if they work.

Another way is using the curl tool. first, install it with the following command:

sudo apt install curl

Then use the curl command to get back icanhazip.com using IPv4:

curl -4 icanhazip.com

When you get your IP address, you can type it in your browser to see the default Apache web page.

http://your_server_ip

You should see the default Debian 11 Apache web page:

Apache Debian 11 test page

 

This page shows that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.

How to Manage the Apache process

Here you have the Apache webserver up and running. Let’s see some basic management commands for it.

You can stop your web server with the following command:

sudo systemctl stop apache2

You can start it again with:

sudo systemctl start apache2

To stop and start the service again by using the following command:

sudo systemctl restart apache2

If you make configuration changes you need to reload the Apache on Debian 11. you can use the following command:

sudo systemctl reload apache2

Apache is configured to start automatically when the server boots, by default. If you don’t want that to happen run the following command:

sudo systemctl disable apache2

To re-enable the service starts at boot, run the following command:

sudo systemctl enable apache2

After these, it’s recommended to set up Apache virtual hosts on Debian 11. Let’s see how it works.

How to set up Apache Virtual Hosts on Debian 11

The concept of virtual hosts allows more than one Web site on one system or Web server.

The servers are differentiated by their hostname. Visitors to the Web site are routed by hostname or IP address to the correct virtual host. Virtual hosting allows companies to share one server to each have their own domain names.

Here it would be best if you had your domain name and replace it with ours in the commands. our domain name is apache.orcacore.net.

To set up Apache virtual hosts on Debian 11 follow these steps:

First, create a directory for your domain with the following command:

sudo mkdir -p /var/www/apache.orcacore.net

Then, give the ownership of the directory with the $USER environmental variable. run the following command:

sudo chown -R $USER:$USER /var/www/apache.orcacore.net

You need to be sure the permissions of your web roots should be correct. you can use the following command:

sudo chmod -R 755 /var/www/apache.orcacore.net

Now you need to create a sample index.html page with your favorite text editor. here we use the Vi text editor:

vi /var/www/apache.orcacore.net/index.html

Add the following sample HTML to your file:

<html>
<head>
<title>Welcome to apache.orcacore.net!</title>
</head>
<body>
<h1>Success! The apache.orcacore.net virtual host is working!</h1>
</body>
</html>

Save and close the file.

You should create a virtual host file with the correct directives. run the following command:

sudo vi /etc/apache2/sites-available/apache.orcacore.net.conf

Add the following configuration block to your file:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName apache.orcacore.net
ServerAlias www.apache.orcacore.net
DocumentRoot /var/www/apache.orcacore.net
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

When you are finished save and close the file.

Now you need to enable the file. run the following command:

sudo a2ensite apache.orcacore.net.conf

Then, disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

Here, you can test for configuration errors. if you see the syntax ok in your output means it’s ok.

sudo apache2ctl configtest
Output
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

At this point, Restart Apache to put your changes on Debian 11. run the following command:

sudo systemctl restart apache2

Apache should now be serving your domain name. You can test this by typing your domain name in your web browser.

http://apache.orcacore.net

You see something similar to this:

Apache virtual host test page

Conclusion

At this point, you learn how to install the Apache web server on Debian 11. You also learn about the Apache management process and setting up Apache virtual hosts.

Hope you enjoy it.

You might be interested in these articles:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular