How to Install Apache with Virtual Hosts on Debian 10
Abstract: create a virtual host file for the domain using the command shown below. $ sudo nano /etc/apache2/sites-available/tecmint.com.confwe need to create a
Apache, popularly referred to as the Apache HTTP server, is a free and open-source cross-platform web server maintained by the Apache Foundation. It is the leading web server commanding a 35% market share on the internet with Nginx coming second with 24% .
Apache is highly reliable, flexible, easy to install and ships numerous features which make it popular amongst developers and Linux enthusiasts. Additionally, it is regularly maintained and updated by Apache foundation and this helps in fixing software bugs and improving its overall efficiency. By the time of penning down this article, the latest version of Apache is 2.4.39.
Read Also: Install Nginx with Server Blocks (Virtual Hosts) on Debian 10
In this guide, we will walk you through the steps on how to install Apache web server on Debian 10.
PrerequisitesBefore we get started, ensure the following requirements are met:
- An instance of Debian 10.
- A Fully Qualified Domain Name (FQDN) pointing to the server.
- In this guide, we use the domain
tecmint.com
pointing to a Debian 10 system with an IP address 192.168.0.104. - A good internet connection.
With our pre-flight check done, let’s get started
Step 1: Update the Debian 10 System RepositoryThe first step in installing Apache on Debian 10 is updating the system repositories. To achieve this, log in as a regular user and using sudo privileges run the command.
$ sudo apt update -yUpdate Debian 10 Repository Step 2: Install Apache on Debian 10
Installing Apache is a piece of cake and pretty straightforward. Once you have successfully updated the system repositories, run the command below to install Apache on Debian 10.
$ sudo apt install apache2 -yInstall Apache in Debian 10 Step 3: Checking the Status of Apache Webserver
After successful installation of Apache web server, it’s always recommended to check if the service is running. Most systemd Linux systems will start the service automatically upon installation.
To check the status of Apache webserver execute the command.
$ sudo systemctl status apache2Check Apache Status
If the service is not running, start the service using the command.
$ sudo systemctl start apache2
To enable Apache Web server on boot execute the command.
$ sudo systemctl enable apache2
To restart Apache run.
$ sudo systemctl restart apache2Step 4: Configure the Firewall to Allow HTTP Port
If UFW firewall is already configured, we need to allow the Apache service across the firewall so that external users can have access to the web server.
To achieve this, we need to allow traffic on port 80 on the firewall.
$ sudo ufw allow 80/tcp
To verify that the port has been allowed on the firewall, run.
$ sudo ufw statusOpen Port 80 on UFW Firewall
Additionally, you can use the netstat command to verify the port as shown.
$ sudo netstat -pnltuVerify Apache Listening Port Step 5: Verify Apache HTTP Web Server
With all the settings in place, open your favorite web browser and browse your server’s IP address or FQDN as shown.
http://server-IP-address OR http://server-domain-nameCheck Apache Web Page Step 6: Configuring Apache Web Server
With Apache web server already set up, its time to host a sample website.
The default Apache webpage file index.html
is found at /var/www/html/
which is the webroot directory. You can host a single site or create virtual host files to host multiple sites.
To host a single site, you can modify the index.html
file located on the webroot directory.
But first, make a backup of the file as shown.
$ sudo mv /var/www/html/index.html /var/www/html/index.html.bak
Now let’s create a new index.html
file.
$ sudo nano /var/www/html/index.html
Let’s add some HTML sample content as shown.
<html> <head> <title>Welcome to crazytechgeek</title> </head> <body> <h1>Howdy Geeks! Apache web server is up & running</h1> </body> </html>
Exit the text editor and restart the web server.
$ sudo systemctl restart apache2
Now reload your web browser and notice the changes to your new site.
Check Apache Website Step 7: Creating Virtual Hosts on ApacheIf you want your web server to host multiple sites, the best way to go around this is to create virtual hosts in Apache web server. Virtual hosts come in handy when you want to host multiple domains in a single server
First, we need to create a webroot directory for the domain tecmint.com
.
$ sudo mkdir -p /var/www/html/tecmint.com/
Next, we shall assign the necessary permissions to the directory using the $USER
variable.
$ sudo chown -R $USER:$USER /var/www/html/tecmint.com/
Next, assign the necessary permissions of the webroot directory for the domain.
$ sudo chmod -R 755 /var/www/html/tecmint.com
Now using your favorite text editor, head out and create sample index.html
file.
$ sudo nano /var/www/html/tecmint.com/index.html
Let’s add some HTML sample content as shown.
<html> <head> <title>Welcome to TecMint.com</title> </head> <body> <h1>Howdy Geeks!</h1> </body> </html>
Save and exit the text editor.
Now, create a virtual host file for the domain using the command shown below.
$ sudo nano /etc/apache2/sites-available/tecmint.com.conf
Now copy and paste the content below and replace the domain tecmint.com
with your own domain.
<VirtualHost *:80> ServerAdmin [email protected] ServerName tecmint.com ServerAlias www.tecmint.com DocumentRoot /var/www/html/tecmint.com/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Save and exit.
At this point, enable the virtual host file as shown.
$ sudo a2ensite tecmint.com.conf
Now let’s disable the default site
$ sudo a2dissite 000-default.conf
To effect the changes, reload the apache webserver.
$ sudo systemctl restart apache2
Now reload your web server and notice the changes for your domain.
Verify Virtual Host on ApacheIf you want to enable HTTPS on your website, read this article: How to Setup Free SSL Certificate for Apache on Debian 10.
ConclusionWe have come to the end of the tutorial. In this guide, you learned how to install Apache on Debian 10 and also configure virtual hosts to host other domains. Feel welcome to get back to us with your feedback.