Installing Apache is a must for users who want to host or manage a website. As such, they have to choose between the available operating systems that support this web server software. And while Linux might have a niche following and not as much of a user count, it is a powerful and efficient OS when it comes to running a web server. Thus, we have dedicated an entire article to cover the various methods a user can install Apache on different Linux distros.
Table of contents
What is Apache?
But the question remains, what is Apache? Sure, we know it is open-source, free cross-platform web server software. But it is as if we have said nothing. Apache is an HTTP server that can host different websites. While there are web services such as WordPress that can create a website, Apache is needed to host said websites.
And since it is an open-source initiative, it has a dedicated community of developers who contribute majorly to the maintenance and enhancement of the web server. Moreover, Apache is a popular software as over 40% of websites use it for hosting. And that too globally.
Apache Software Foundation was the first to develop this software back in 1995, and since then, it has become a staple in web hosting. Not only is it fast, but equally reliable. Furthermore, users can always customize it to their liking, using extensive extensions and modules library.
Prerequisites to install Apache on a Linux distro:
As things stand, it is not always simple to install Apache on a Linux distro. There are quite a few requirements that the user has to meet before, such as:
- APR and APR Utility – Users have to ensure that they have the APR and APR-Util working on their distribution. Otherwise, they are required to download the necessary packages from Apache APR and unpack them to /httpd_source_tree_root/srclib/apr and /httpd_source_tree_root/srclib/apr-util
- Perl-Compatible Regular Expressions Library or PCRE – Next, the users have to install, if not available, the PCRE library which is no longer bundled with the HTTPD command. Thus, the users must download the source code from http://www.pcre.org. If the system is still unable to find the pcre-config script, use the –with-pcre parameter to point to it.
- Memory or Disk space – Users also have to ensure at least 50 MB of free disk space for the Apache installation.
- ANSI-C Compiler and Build System – Another point to note is the ANSI-C compiler which should be installed on the system. Apache devs recommend using the GNU-C compiler or GCC.
- Accurate time format – HTTP protocol is reliant on time and date. Hence, the users should ensure the time sync function of the system is working fine.
Installing Apache on Linux:
Now, we can proceed with the installation. There are two different methods for Apache installation. If the user wants to install the basic version, they can directly install it after updating the respective repository, as shown in method 1. Otherwise, they can build it from scratch using the source code, as covered in method 2.
Method 1. Using the Command Terminal for installation
The following method utilizes the command terminal to install Apache on different Linux distros.
On Ubuntu and Debian
Step 1. First, launch the command terminal using Ctrl + Alt + T.
Step 2. Then, update the APT repository.
sudo apt update
sudo apt-get update
Step 3. Lastly, install Apache.
sudo apt install apache2
Note – Apache is available on most distros by default. Hence, you can use the APT package to install it.
On Red Hat, Fedora, Amalinux, Rocky, CentOS, Oracle, and Amazon
Step 1. Start the command terminal.
Step 2. Now, update the DNF repository.
sudo dnf update
Step 3. Finally, install Apache using HTTPD.
sudo dnf install httpd
Method 2. Compiling and installing from source
In this method, users can compile and build Apache using the source code. The steps are:
Step 1. First, use a web browser and go to the Apache HTTP server download page.
Step 2. Download the Apache HTTP server package from the website.
Step 3. Now launch the command terminal. Make sure you have root access. Otherwise, use the sudo command.
Step 4. Use the following code to extract the source:
sudo gzip -d httpd-NN.tar.gz
Step 5. Then decompress the file using the tar command:
sudo tar xvf httpd-NN.tar
Step 6. A new directory will get created. Navigate to it by using the cd command.
cd httpd-NN
Step 7. Finally, build the parts for Apache.
sudo make
Step 8. And lastly, install the build on your system.
sudo make install
Securing Apache Web Server on Linux
Once Apache is installed, it’s crucial to take measures to secure the web server, as it is the gateway through which users interact with the website. Here are some steps to enhance the security of your Apache installation:
1. Keep Apache Updated
Always ensure that you are using the latest version of Apache, as updates typically contain security patches. You can regularly update Apache using the package manager of your distribution. For instance:
- On Ubuntu/Debian:
sudo apt update && sudo apt upgrade
- On Red Hat-based systems:
sudo dnf update httpd
2. Configure Firewalls
Use a firewall to restrict access to your server. Only allow necessary ports, such as port 80 (HTTP) and 443 (HTTPS). On Ubuntu/Debian, you can use UFW:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
On Red Hat-based systems, you can use firewalld
:
sudo firewall-cmd --zone=public --add-service=http
sudo firewall-cmd --zone=public --add-service=https
sudo firewall-cmd --runtime-to-permanent
3. Disable Unnecessary Apache Modules
Apache comes with many modules, but not all are needed for every use case. Disabling unnecessary modules reduces potential attack surfaces. To disable a module, use the following commands:
- On Ubuntu/Debian:
sudo a2dismod module_name
- On Red Hat-based systems, remove or comment out the module in the
httpd.conf
file.
4. Enable HTTPS with SSL/TLS
Securing your website with HTTPS is a necessity today. Install an SSL certificate and configure Apache to use it. You can use free certificates from Let’s Encrypt or purchase one from a certificate authority.
- Install the
mod_ssl
module:
sudo apt install mod_ssl # (on Debian-based)
sudo dnf install mod_ssl # (on Red Hat-based)
- Obtain and install the SSL certificate, and configure your virtual host file to include the SSL certificate paths.
5. Disable Directory Listing
By default, Apache may display the contents of directories that do not have an index.html
file. You can disable directory listing by editing your Apache configuration:
<Directory /var/www/html>
Options -Indexes
</Directory>
6. Restrict File Permissions
Ensure that your Apache files and directories have the correct permissions:
- Directories:
chmod 755 /var/www/html
- Files:
chmod 644 /var/www/html/index.html
Ownership should typically be assigned to the www-data
user and group:
sudo chown -R www-data:www-data /var/www/html
Performance Optimization of Apache
To ensure that your server performs efficiently, especially under high traffic, consider these optimization techniques:
1. Enable Caching
Caching static content such as images, CSS, and JavaScript can drastically improve load times. Apache’s mod_cache
can be used for this purpose.
- Install the cache module:
sudo apt install libapache2-mod-cache # (Ubuntu/Debian)
sudo dnf install mod_cache # (Red Hat)
- Enable caching in your Apache configuration:
<IfModule mod_cache.c>
CacheQuickHandler off
CacheLock on
CacheRoot /var/cache/apache2/cache
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
2. Enable Gzip Compression
Compressing content before sending it to the client can save bandwidth and improve performance. Enable mod_deflate
to compress text-based content:
sudo a2enmod deflate # (Ubuntu/Debian)
sudo dnf install mod_deflate # (Red Hat)
Add the following lines to your configuration:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
3. Adjust Max Clients and Worker Processes
Fine-tuning the number of concurrent connections your server can handle is essential. Edit the Apache mpm_prefork
or mpm_worker
module settings in the configuration file to adjust these values:
- On Ubuntu/Debian:
/etc/apache2/apache2.conf
- On Red Hat:
/etc/httpd/conf/httpd.conf
Example configuration:
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 3000
</IfModule>
Troubleshooting Common Issues in Apache
1. Apache Not Starting
If Apache fails to start, you can diagnose the issue by checking the error logs:
- Ubuntu/Debian:
/var/log/apache2/error.log
- Red Hat:
/var/log/httpd/error_log
Common causes include incorrect configuration file changes or missing modules.
2. “403 Forbidden” Error
This error typically occurs when the server permissions are misconfigured. Check your directory and file permissions to ensure the web server user (www-data
or apache
) has the necessary access.
3. SSL Certificate Not Working
If Apache is not serving your website over HTTPS, ensure that:
- The SSL module is enabled.
- The virtual host is configured with the correct paths to the SSL certificate and key.
You can also check the Apache logs for SSL-specific errors.
By following these steps, you can ensure that your Apache web server is secure, optimized, and running smoothly on your Linux distribution.