How to install and configure Apache web server on Debian 10

Apache Server is one of the most popular open source web servers developed and maintained by the Apache Software Foundation. Apache is by far the most commonly used web server application on Linux operating systems, but it can be used on almost all Windows platforms, MAC OS, OS / 2, etc. It allows developers to publish their content over the Internet.

In this article, we will explain how to install and configure Apache web server on Debian 10 OS.

Install Apache 2 on Debian Linux

Follow these steps to install Apache2 on your system using the official Ubuntu repositories.

Step 1. Update system repositories

First, we will need to update the package repositories in our OS. To do this, run the following command in Terminal as sudo:

$ sudo apt update

When prompted for a password, enter your sudo password.

Step 2. Install Apache 2 using the apt command

Then in this step, install the Apache2 web server using the following command:

$ sudo apt install apache2

Install Apache2

You will be given Well no the ability to continue the installation. Hit at continue.

Step 3. Verify Apache Installation

Once the installation is complete, you can view the installed version of Apache by running the following command in Terminal. This way you can also ensure that Apache is successfully installed on your system.

$ apache2 -version

Verify Apache Installation

Configure firewall settings

If your system is running a firewall, you need to allow certain web ports so that external users can access them. To do this, run the following commands in the Terminal:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp

Then run the following command to check if the ports are allowed.

$ sudo ufw status

Configure Apache Web Server Settings

Step 1. Make sure the Apache service is running

After installation, the Apache web service starts automatically. However, to be sure, run the following command in Terminal:

$ sudo systemctl status apache2

Step 2. Check Apache Web Server

You can check if the Apache web server is working normally by requesting a web page from the Apache web server.

Run the below command in Terminal to find the IP address of your server.

$ hostname -I

Once you find the IP address, enter http: // followed by the IP address of your web server:

http://server_IP

By entering the above IP address, you will see the following default Apache page.

Apache default page

Configuring virtual hosts in Apache

Apache virtual hosts allow you to run multiple websites on a single server. We will set up a virtual host on the Apache web server here. To do this, we will first create a website named testdomain.info using the server block that is available in Apache by default.

Step 1. Set up your domain name

First we will create a directory at / var / www for our virtual host testdomain.infoTo do this, use the following command:

(Note: replace testdomain.info with your own domain name.)

$ sudo mkdir -p /var/www/testdomain.info/html

Now change the owner and permissions using the following commands:

$ sudo chown -R $USER:$USER /var/www/testdomain.info /html
$ sudo chmod -R 755 /var/www/testdomain.info

We will now create a sample index page to test our testdomain.info site. To do this, we will create an HTML file using the nano editor as follows:

$ nano /var/www/testdomain.info/html/index.html

Add the following lines for the index page:

<html>
<head>
<title>Welcome to the page testdomain.info!</title>
</head>
<body>
<h1>You got Lucky! Your testdomain.info server block is up!</h1>
</body>
</html>

Sample web page in HTML

Then press Ctrl + O save and then Ctrl + X to exit the file.

We will now create a virtual host file that will serve the contents of your server using the following command:

$ sudo nano /etc/apache2/sites-available/testdomain.info.conf

Now add the following configuration details for your domain name:

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

Apache Virtual Host File

After that press Ctrl + O to save and then Ctrl + X to exit the file.

Step 2. Include the domain config file

Now enable the virtual host file using the following command:

$ sudo a2ensite testdomain.info.conf

enable virtual host

Now, let’s disable the default Apache configurations using the following command:

$ sudo a2dissite 000-default.conf

Disable vhost by default

To apply the changes, restart the Apache2 service using the following command:

$ sudo systemctl restart apache2

Step 3. Check for errors

Now check your configuration for syntax errors:

$ sudo apache2ctl configtest

If there is no error, you will get the following output.

Check apache config file for syntax errors

In some cases, the following error may appear:

Troubleshooting apache configuration errors

To fix this error, edit servername.conf file by running the following command:

$ sudo nano /etc/apache2/conf-available/servername.conf

Add the following line to it:

ServerName testdomain.info

Set the server nameThen press Ctrl + O save and then Ctrl + X to exit the file.

Then run the following command:

$ sudo a2enconf servername

Restart apache to apply the changed configuration

Now restart Apache2:

$ systemctl reload apache2

After that, run the following command again to check the config file:

$ sudo apache2ctl configtest

You will now see that the bug has been removed.

Step 4. Check if Apache is serving your domain name

Now open your browser and go to:

http://testdomain.info

Replace testdomain.info with your own domain name.

The next page of the index shows that you can now access all of your websites.

Domain test

Some common Apache management commands

Some of the more common commands that can be used to manage Apache services are:

Use the following command to start the Apache server:

$ sudo systemctl start apache2

Use the following command to stop the Apache server:

$ sudo systemctl stop apache2

Use the following command to restart the Apache server:

$ sudo systemctl restart apache2

Use the following command to restart your Apache server:

$ sudo systemctl reload apache2

Use the following command to always start the service at boot:

$ sudo systemctl enable apache2

Use the following command to shutdown the Apache server:

$ sudo systemctl disable apache2

In this article, we have learned how to install and configure Apache web server on Debian OS. We’ve done some basic setup, which includes changing the firewall, configuring the virtual host, and how to manage Apache services with some commands. I hope he gave you a basic overview of how to use Apache to properly host websites.

How to install and configure Apache web server on Debian 10

Sidebar