Apache Installation

Run following commands to do Apache Installation.

sudo apt update
sudo apt install apache2

Verify Apache Installation

sudo systemctl status apache2

Expected Output...

sudo systemctl status  apache2
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Mon 2020-08-24 21:32:59 UTC; 57s ago
  Process: 12207 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)

Open HTTP Port 80 and HTTPs Port 443

Run following ufw command to open Apache both 80 and 443 ports.

sudo ufw allow 'Apache Full'

Let check the status now...

sudo ufw status | egrep 'Apache'

Expected output...

sudo ufw status | egrep 'Apache'
Apache Full                ALLOW       Anywhere
Apache Full (v6)           ALLOW       Anywhere (v6)

Setting Up Apache Virtual Host

Apache can host more than one website. Each website has a Apache config file. For a single website hosting, Apache comes with default config file which resides in /etc/apache2/sites-enabled/ and the website content is in /var/www/html directory.

Create Multiple Apache Virtual Hosts

  1. Let us create a new virtual host for a site mysite.com.
  1. Create a directory first in /var/www directory
sudo mkdir -p /var/www/mysite.com
  1. Create an index.html in /var/www/mysite.com directory as shown below.
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to mysite.com</title>
  </head>
  <body>
    <h1>Success! mysite.com home page!</h1>
  </body>
</html>
  1. To avoid permission issues, change the ownership of the domain document root directory to the apache user (www-data)
sudo chown -R www-data: /var/www/mysite.com
  1. Create virtual host config file
<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.mysite.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/mysite.com/public_html

    <Directory /var/www/mysite.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/mysite.com-error.log
    CustomLog ${APACHE_LOG_DIR}/mysite.com-access.log combined
</VirtualHost>
  1. Activate virtual host configuration
sudo a2ensite mysite.com
  1. Test the Apache configuration
sudo apachectl configtest
  1. Restart the Apache
sudo systemctl restart apache2

Common Errors While Installing Apache

You might run in to following error...

Check who is listening on port 80 by running lsof command...

lsof -i :80

Find the PID and kill using following command...

kill -9 <pid>
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80