It doesn’t have to be this difficult to host a website on a Linux server. No way, Apache. On Linux, follow these steps to install and configure an Apache server.
Apache is the most powerful, versatile, and extensively used open-source software for serving online content across the internet. When a client sends a request with the website domain, the server acts as a delivery man, serving material available as HTML files.
Most importantly, web servers, like Apache, support a wide range of operating systems, including Linux, Windows, Solaris, macOS, and others. As a result, you may quickly convert a computer into a server that hosts many websites.
The Apache HTTP server supports software and application integration by allowing the loading of modules. This article describes how to install and configure an Apache HTTP server under Linux.
Step 1: Install Apache Server on Linux
Before you begin installing Apache, you must update the Linux package database from the official repositories. It is a crucial procedure that helps to avoid security flaws and incorporates the most recent features into the packages.
Using the following commands, you can update the system repository list and install Apache:
On Ubuntu and Debian:
sudo apt-get update
sudo apt-get install apache -y
On CentOS:
sudo yum update
sudo yum install httpd -y
Regarding Fedora:
sudo dnf update
sudo dnf install httpd -y
To install Apache on Arch Linux, run:
sudo pacman -Syu
sudo pacman -S apache
Step 2: Verify Apache Service Status
The Apache service automatically starts on Debian-based distributions. To visit the server’s landing page, open a browser and input your local IP address. If you’re not sure what your server’s address is, use the hostname -i command to find out.
hostname -i
Output:
192.168.43.130
The page confirms that the installation was successful.
http://<local_server_IPadd>
To validate the installation, use the following command:
apache2 -version
Output:
The service will not start automatically if you are using CentOS. You can manually start the service by running the following command:
sudo systemctl start httpd
Check the following service status:
sudo systemctl status httpd
Step 3: Configure Firewall to Allow Apache Server Access
Another required step in Apache configuration is to enable the UFW firewall in Linux to accept or allow traffic to the server through the default port 80. The service registers with the firewall with some application profiles during installation. The list of application profiles assists you in enabling and disabling Apache access.
To view all Apache application profiles, run the following command:
sudo ufw app list
Output:
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
The available profiles are:
- Only opens port 80 to allow unencrypted internet connection – Apache
- Allows unencrypted and secure communication on ports 80 and 443 – Apache Full
- Allows HTTPS server access by accepting traffic on port 443 – Apache Secure
Because the server does not have SSL/TLS configured, we will only allow UFW access on port 80, as seen below:
sudo ufw allow 'Apache'
Now, use the following command to check the status of the firewall:
sudo ufw status
Step 4: Understand Apache Directories and Files
After successfully installing and configuring the server, every newbie should understand how the server manages its websites and their content. All of the websites you intend to host on your server are managed in the /var/www/html directory.
The directory contains the web page you saw previously by default. To host several websites, Apache allows you to establish distinct subdirectories in this folder.
The main Apache server configuration directory in Ubuntu and Debian-based distributions is /etc/apache2, while in CentOS it is /etc/httpd. As a result, all of the server’s configuration files contain within these directories. Among the most well-known files and directories are:
- /var/log/apache2/error.log: Logs all the errors encountered
- /var/log/apache2/access.log: All server access requests are logged.
- /etc/apache2/sites-available: A directory containing virtual hosts
- /etc/apache2/sites-enabled: Stores websites that are ready to serve per virtual host. It will not work until the configuration file links inside the sites available directory with the a2ensite command.
An Example to Set Up a Virtual Host
In all Linux distributions, Apache server installation produces a default directory of /var/www/html. This directory includes all of your website’s files, but it cannot use if you want to host numerous websites on the same server.
You can use virtual hosts and build a domain directory inside the /var/www folder to serve several domains, as seen below:
sudo mkdir /var/www/host_example
Using chown, you can change the directory’s ownership and file permissions.
sudo chown -R $current_user:$current_user /var/www/host_example
sudo chmod -R 755 /var/www/host_example
Now open the /var/www/host_example/html/content.html file in your favorite editor and copy/paste the following HTML:
<html>
<head>
<title>Welcome to host_example!</title>
</head>
<body>
<h1>You are running host_example on Ubuntu 18.04!</h1>
</body>
</html>
Apache produces a configuration folder that serves as a repository for virtual host information. /etc/apache2/sites-available/000-default.conf is the default configuration file. You can, however, create a new file with your domain name and copy/paste the configuration block from the default file into it.
Using your preferred text editor, update the file with your domain name and the new directory as follows:
<VirtualHost *:80>
ServerAdmin admin@host_example
ServerName host_example
ServerAlias www.host_example
DocumentRoot /var/www/host_example
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Activate Your Domain Configuration File
The activation of the domain configuration file host example.conf necessitates the use of a2ensite.
sudo a2ensite host_example.conf
The criteria for disabling the default configuration file (000-default.conf) are shown in the output above:
sudo a2dissite 000-default.conf
To load the changes, restart the apache service.
sudo systemctl restart apache2
To see if your website is being served, open your browser and go to the domain name:
http://host_example
Test for Configuration Errors
You can use the apache2ctl program to check for Apache server configuration issues. Then to test the successful no-error setting, run the following command and look for the Syntax OK output:
sudo apache2ctl configtest
Output:
Syntax OK
Hosting Multiple Websites on Linux Servers Using Apache
The tutorial above demonstrates the modularity and ease with which an Apache server can be installed and configured. The server’s flexibility allows you to customize the setup and host websites according to your needs. The virtual host setup use case demonstrates how the configuration files function and interact.
You may have also noted that depending on your Linux distribution and Apache version, the specifics/folders may vary and finally, the Apache management commands are used to efficiently manage, start, and reload server services. Other Linux servers can also be used to host your websites.