Setting up an apache server on Ubuntu

Setting up an apache server on Ubuntu

A summarized process of installing an apache server.

SeekPng.com_indiana-jones-png_8964755.png

Installation

  1. Update the apt package index:
    sudo apt-get update
    
  2. Install the apache server:
    sudo apt install -y apache2
    
    Apache runs on port 80 by default. You might get an error such as failed to start the apache server: port 80 is occupied.

If you get such errors, you will need to free the port by killing the process using the port.

  • Verify the process id(pid) using port 80:
    sudo lsof -t -i:80
    
    This returns the pid.
  • Kill the process:
    sudo kill pid
    
    pid is the process id returned.

If you didn't notice the error during the installation, it doesn't guarantee that the apache service started successfully. You need to verify its status using the method below.

  • Using systemctl:
    sudo systemctl status apache2
    
    You should get a similar output as shown.

active-status.PNG

The active status of the apache is shown on line 3 (active: inactive). If you get an active status, then apache is ready to go. If you get a failed status, you need to restart the apache service using the command below.

sudo systemctl restart apache2

The above command will restart the apache service. You should get an active status now as shown below using:

sudo systemctl status apache2

runingactivestatus.PNG

Adjusting the firewall

Before testing Apache, it’s necessary to modify the firewall settings to allow outside access to the default web ports. During installation, Apache registers itself with UFW to provide a few application profiles that can be used to enable or disable access to Apache through the firewall.

UFW(Uncomplicated Firewall) is a program for managing a netfilter firewall designed to be easy to use. It uses a command-line interface consisting of a small number of simple commands, and uses iptables for configuration.

It is okay if you don't understand UFW at first. We only want to allow outside access to apache default web ports, and ufw happens to be a tool we'll use to accomplish that.

  1. List the ufw application profiles by typing:
    sudo ufw app list
    
    You will receive a list of the application profiles as shown below:

ufwapplications.PNG

As indicated by the output, there are three profiles available for Apache:

  • Apache: This profile opens only port 80 (normal, unencrypted web traffic)
  • Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)

Again, do not worry about the above list. We simply want to allow traffic on port 80.

  1. Enable port 80 using:
    sudo ufw allow 'Apache'
    
  2. Check the status of ufw:
    sudo ufw status
    
    On your list of applications, Apache and Apache(v6)'s status should be similar to the output below.

ufwstatus.PNG

  1. verify your local IP address:
    hostname -I
    
    This should show a list of space-separated IP addresses. You can try each on the browser. Your output should be similar to the one shown below.

apachebrowser.PNG

Apache processes

start

Start the apache service:

sudo systemctl start apache2

stop

Stop the apache service:

sudo systemctl stop apache2

reload

Reload the apache service:

sudo systemctl reload apache2

enable

Stop the apache service. This automatically starts the apache service after system reboot:

sudo systemctl enable apache2

disable

Stop the apache service.

The apache service needs to be enabled after system reboot:

sudo systemctl enable apache2

Thanks for reading. Drop your comment below. Also read How to setup an apache virtual host