Step-by-Step Guide-Install and Configure Apache Web Server 1

Step-by-Step Guide-Install and Configure Apache Web Server

Setting up a reliable and efficient web server is a critical step in establishing a robust online presence. Apache, one of the most widely used web servers, offers a powerful and flexible solution for hosting websites and applications. If you’re new to Apache or seeking a comprehensive step-by-step guide, you’ve come to the right place. In this tutorial, we will walk you through the process of installing and configuring the Apache web server from start to finish. Whether you’re a beginner looking to set up your first web server or an experienced professional seeking a refresher, this step-by-step guide will provide you with the knowledge and practical instructions to successfully install, configure, and optimize Apache for your specific needs.

Apache is a powerful web server that supports more than 50 million websites. It’s developed by the Apache Software Foundation and used by a variety of businesses, from small businesses to large corporations. Apache has been around since 1995 and is still one of the most popular web servers in use today. This guide will walk you through installing and configuring Apache on your remote Linux server.

Step-by-Step Guide-Install and Configure Apache Web Server 2

Step 1 – Prerequisites

To install Apache on a Linux system, the following prerequisites are necessary:

A Linux distribution is installed on your machine, such as Ubuntu, CentOS, or Fedora.

Administrative privileges to perform the installation.

An internet connection to download the Apache packages.

The necessary packages for compiling Apache from sources, such as build-essential, libpcre3-dev, and zlib1g-dev.

A text editor, such as nano or vim, to edit the Apache configuration files.

Before installing Apache, it’s a good idea to check if there’s an Apache package available in the default package repository of your Linux distribution. If so, it’s recommended to use that package as it will simplify the installation process.

 

For Debian-based systems (e.g. Ubuntu, Mint, etc.) use apt:

apt search apache2

CentOS, etc

yum list httpd

yum info httpd

The result should indicate if the repository is available or if it is already installed. Ran on  a target, centos distribution below is the output

 

[root@ip--------]# yum list httpd

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
244 packages excluded due to repository priority protections
Installed Packages
httpd.x86_64                                  2.4.54-1.amzn2                                   @amzn2-core
[root@ip--------]# yum info httpd

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd

244 packages excluded due to repository priority protections

Installed Packages
Name        : httpd
Arch        : x86_64
Version     : 2.4.54
Release     : 1.amzn2
Size        : 4.1 M
Repo        : installed
From repo   : amzn2-core
Summary     : Apache HTTP Server
  • It’s very important that you already have a domain name and DNS properly configured to the IP address of the target Linux machine.
  • You will also need SSH access to the target server.
  • The memory requirement for Apache is usually a minimum 512 MB although it can vary depending on the number of modules you are using in your configuration file.
  • For Ubuntu and Debian distributions Apache is called “apache2” and for centos-based systems, it’s called “httpd”.

 

Apache Installation

Here are the steps to install Apache on the three mentioned Linux systems:

Ubuntu/Debian:

sudo apt update
sudo apt install apache2

Centos

sudo yum update
sudo yum install httpd

Once the installation is complete, you can start the Apache service using the following command:

sudo systemctl start apache2 # on Ubuntu/Debian
sudo systemctl start httpd # on CentOS

sudo systemctl enable apache2 # on Ubuntu/Debian
sudo systemctl enable httpd # on CentOS

Location for Apache Config files

For Linux distributions, Apache configuration files are usually stored in the following locations for different distributions:

  • Debian/Ubuntu: The main Apache configuration file is typically located at “/etc/apache2/apache2.conf”.
  • The virtual host configurations are stored in the “/etc/apache2/sites-available” directory.
  • CentOS: The main Apache configuration file is typically located at “/etc/httpd/conf/httpd.conf”.
  • The virtual host configurations are stored in the “/etc/httpd/conf.d” directory.
See also  Installing MySQL Like a Pro on an EC2 Instance

Note that these paths may vary depending on the version of the Linux distribution and the method used to install Apache.

Below is the SSH output of the centos-based distribution :

[root@ip-----httpd]# ls -lt
drwxr-xr-x 2 root root 138 Oct 31 00:26 conf.d
drwxr-xr-x 2 root root 264 Oct 31 00:10 conf.modules.d
drwxr-xr-x 2 root root  59 Oct 27 12:48 conf
lrwxrwxrwx 1 root root  10 Oct 26 14:15 run -> /run/httpd
lrwxrwxrwx 1 root root  19 Oct 26 14:15 state -> ../../var/lib/httpd
lrwxrwxrwx 1 root root  19 Oct 26 14:15 logs -> ../../var/log/httpd
lrwxrwxrwx 1 root root  29 Oct 26 14:15 modules -> ../../usr/lib64/httpd/modules

[root@ip--------- conf]# ls -lt
-rw-r--r-- 1 root root 15691 Oct 28 13:38 httpd.conf                                                   //Apache config
-rw-r--r-- 1 root root 13064 Jun 30  2022 magic
-rw-r--r-- 1 root root 11910 Jun 30  2022 httpd.conf.old

[root@ip--------- conf.d]# ls -lt
-rw-r--r-- 1 root root 9732 Oct 28 14:58 ssl.conf                                                          //SSL configuration
-rw-r--r-- 1 root root 2893 Jun 30  2022 autoindex.conf
-rw-r--r-- 1 root root  366 Jun 30  2022 README
-rw-r--r-- 1 root root 1252 Jun 30  2022 userdir.conf
-rw-r--r-- 1 root root  516 Jun 30  2022 welcome.conf
-rw-r--r-- 1 root root 1618 Jun 23  2022 php.conf
-rw-r--r-- 1 root root 3475 Jun 16  2015 mod_evasive.conf                                       //Security module

The Virtual host directory contains additional modules that can be used to extend the functionality of your Apache server.   This directory contains configuration files for your virtual hosts. When a new site is configured, Apache will read the file from this directory and include its contents into your main server configuration file.

Step-by-Step Guide-Install and Configure Apache Web Server 3

Step 3 – Configuring Virtual Hosts for Apache

Virtual hosts are a way to host single/multiple websites on a single web server. Each virtual host has a unique domain name and can serve its own content. To configure multiple Apache virtual hosts, you need to create separate virtual host configurations for each website in the Apache configuration file (usually named “httpd.conf”).

Each virtual host configuration should

  • specify the domain name,
  • Document Root (the directory where website files are stored),
  • and any other necessary options such as the port number and Server Name.

Here is an example of a basic virtual host configuration in Apache. This can be done in the same conf file or added to new files and pointed to by main conf file.

<VirtualHost *:80>
    ServerName domain1.com
    DocumentRoot /var/www/domain1.com
</VirtualHost>

<VirtualHost *:80>
    ServerName domain2.com
    DocumentRoot /var/www/domain2.com
</VirtualHost>

After editing the Apache configuration file, you need to restart the Apache service for the changes to take effect.

Hosting Multiple Websites on Apache Server

You may configure multiple web hosts in the same config file or create separate config files, both methods can be used to configure multiple websites on an Apache server. However, using multiple virtual host files is considered the best practice as it provides better organization and ease of maintenance. A virtual host file is a separate file for each website, allowing you to configure specific settings for each website independently, and it also makes it easier to manage and update each website without affecting others.

See also  Best Guide to Modules and Configuration - Apache Web Server

Below are two virtual host files for two separate websites,

example1.com and example2.com, on an Apache server:

/etc/httpd/conf.d/example1.com.conf:
<VirtualHost *:80>
    ServerName example1.com
    ServerAdmin admin@example1.com
    DocumentRoot /var/www/example1.com/public_html
    ErrorLog /var/www/example1.com/error.log
    </VirtualHost>

/etc/httpd/conf.d/example2.com.conf:
<VirtualHost *:80>
    ServerName example2.com
    ServerAdmin admin@example2.com
    DocumentRoot /var/www/example2.com/public_html
    ErrorLog /var/www/example2.com/error.log
</VirtualHost>

Setup Document Root and Permissions for Apache

The DocumentRoot is the directory on your server that contains the files to be served for a website. It’s specified in the virtual host file and Apache serves web files from that directory in response to incoming requests.

Correct permissions for the DocumentRoot depend on your specific needs, but a common and secure setup is to allow the web server process (usually Apache or nginx) to read the files, and a separate user (such as “www-data” normally used by Apache and Nginx servers) to own the files and have write access to the directory.

Below is a quick demo of how to set correct permissions using the chown and chmod commands:

sudo chown -R www-data:www-data /var/www/example.com/public_html
sudo chmod -R 755 /var/www/example.com/public_html

  • This sets the owner and group of the directory and all files inside to www-data
  • Sets the permissions to 755, which means that the owner has read, write, and execute permissions.
  • while everyone else has only read and execute permissions.

Step-by-Step Guide-Install and Configure Apache Web Server 4

Step 4: Starting and Checking Apache

To start Apache, you can use the following command:

sudo systemctl start apache2         //Ubuntu and Debian
sudo systemctl start httpd        //Centos

To check the Apache server status:

Ubuntu/Debian:
Check Apache status: sudo systemctl status apache2

CentOS:
Check Apache status: sudo systemctl status httpd

Both commands should return output indicating whether Apache is running and functioning properly. The exact output may vary depending on the version of the operating system and Apache. If Apache is running correctly, the output should indicate that the Apache service is active (running).

Below is the output of centos based distribution:

[root@ip-------- conf.d]# sudo systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since Fri 2023-01-06 01:50:31 UTC; 3 weeks 6 days ago
     Docs: man:httpd.service(8)
  Process: 1848 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 2110 (httpd)

if the status is “Dead” then check the error log file. most likely is because of Apache not finding files where they are supposed to be.

Configuring SSL for Your Website on Apache

SSL (Secure Sockets Layer) is a security protocol that provides encryption and authentication for communication over the internet. It is now commonly known as TLS (Transport Layer Security). To configure SSL for Apache, you need to obtain a SSL certificate from a trusted certificate authority (CA) and install it on your Apache server. The process of obtaining a certificate and installing it may vary depending on the CA you choose.

Once you have obtained the certificate, you can configure SSL for Apache by adding the following lines to your virtual host configuration in Apache’s configuration file. Normally these config is added to ssl.conf as shown in the above section.

<VirtualHost *:443>
    ServerName domain1.com
    DocumentRoot /var/www/domain1.com
    SSLEngine on
    SSLCertificateFile /path/to/certificate.crt
    SSLCertificateKeyFile /path/to/private.key
</VirtualHost>

The certificate and private key files have to be issued by CA service( or you can self-sign the certs ). Whatever the process is, we are not going to go into details of it for now. We assume you have got access to these required cert files. Note that you also need to ensure that the Apache mod_ssl module is enabled and that port 443 is open in your firewall. After making the changes, restart the Apache service for the changes to take effect.

See also  Mastering Apache Configuration-Tips for Optimal Performance

To check if the mod_ssl module is enabled in Apache, you can run the following command:

sudo apachectl -M | grep ssl

If the module is enabled, you will see the line ssl_module (static) in the output.

If the module is not enabled, you can enable it on Ubuntu/Debian by running the following command:
sudo a2enmod ssl

And on CentOS, run the following command:
sudo yum install mod_ssl

After enabling the mod_ssl module, you need to restart the Apache service for the changes to take effect:

sudo systemctl restart apache2 # on Ubuntu/Debian

sudo systemctl restart httpd # on CentOS

Redirect HTTP to HTTPS for Apache

You may want to redirect all HTTP (port 80 ) traffic to HTTPS ( port 443 )  to direct all your users to secure socket layers communication which would normally be the preferred way of communication.

Edit the Apache configuration file for editing and add the following lines in the virtual Host section.

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Save the changes and don’t forget to restart your Apache server.

If for any reason above code doesn’t work try the below code:

Redirect permanent / https://domain.com/

Configure SSL Certs using Lets Encrypt Project

Let’s Encrypt is a non-profit organization that provides free, automated, and open Certificate Authority (CA) services, allowing anyone to secure their website with SSL/TLS certificates. These certificates are used to establish a secure and encrypted connection between a website and its visitors’ web browsers. By making it easy and free for website owners to obtain and install certificates, Let’s Encrypt aims to improve internet security and privacy for everyone. Below we are giving out a very basic demo of how to install lets encrypt issued SSL certs on your system. It is not intended to be a detailed tutorial.

Ubuntu/Debian:
Install certbot: sudo apt install certbot
Obtain SSL certificate: sudo certbot --apache
Follow the on-screen instructions to complete the SSL certificate issuance process
Verify SSL certificate installation: sudo certbot certificates

CentOS:
Install certbot: sudo yum install certbot
Obtain SSL certificate: sudo certbot --apache
Follow the on-screen instructions to complete the SSL certificate issuance process
Verify SSL certificate installation: sudo certbot certificates

 

The above instructions are for obtaining an SSL certificate using the certbot tool and assuming the Apache web server is already installed and running.

Step-by-Step Guide-Install and Configure Apache Web Server 5

Apache- Check if it all works:

If all has gone well, if you access the domain name in Brower you should be redirected to the Apache default welcome screen