How to Install and Configure Apache Web Server on Ubuntu

Apache is responsible for accepting directory (HTTP) requests from Internet users and sending them their desired information in the form of files and Web pages. Much of the Web's software and code is designed to work along with Apache's features.

Update the System.

First, we will need to update,

apt update -y

apt upgrade -y

Install Apache2

Install Apache2 Web Server using below command,

apt install apache2

Output:

root@vps:~# apt install apache2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.2-0 ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser openssl-blacklist
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.2-0 ssl-cert
0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,866 kB of archives.
After this operation, 8,091 kB of additional disk space will be used.

Verify the Apache version,

apache2 -v

Output:

root@vps:~# apache2 -v
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2022-04-26T18:02:11
root@vps:~# 

Firewall Configuration

We'll need to open ports on our system to access Apache.

ufw allow ‘Apache’

Check the status which will show Apache allowed in firewall.

ufw status

It shows inactive, activate it first using below command,

ufw enable 

Output:

root@vps:~# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
root@vps:~#

Now check the status,

ufw status

Output:

root@vps:~# ufw status
Status: active

To                         Action      From
--                         ------      ----
Apache                     ALLOW       Anywhere                  
Apache (v6)                ALLOW       Anywhere (v6)             

root@vps:~# 

Verify if the Apache Service is Running,

systemctl status apache2

Output:

root@vps:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-30 13:55:04 UTC; 16min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 56065 (apache2)
      Tasks: 55 (limit: 3480)
     Memory: 5.6M
     CGroup: /system.slice/apache2.service
             ├─56065 /usr/sbin/apache2 -k start
             ├─56067 /usr/sbin/apache2 -k start
             └─56068 /usr/sbin/apache2 -k start

May 30 13:55:04 vps.server.com systemd[1]: Starting The Apache HTTP Server...
May 30 13:55:04 vps.server.com systemd[1]: Started The Apache HTTP Server.
root@vps:~#

Open the web browser and access apache welcome page as follows,

http://Your_IP_Address

Output:

image

Setting Up Virtual Host in Aapache

Create directory for your domain using below command,

mkdir -p /var/www/dev.domainhere.info/html

Replace dev.domainhere.info with your actual domain

If you've created user then change the directory ownership to current user,

chown -R $USER:$USER /var/www/dev.domainhere.info/html

Give necessary permission as follows,

chmod -R 755 /var/www/dev.domainhere.info

Make a sample page for your website

Let's create a sample page for your website,

nano /var/www/dev.domainhere.info/html/index.html

Add the following lines,

<html>
<head>
<title>Welcome to my Website!</title>
</head>
<body>
<h1>Hello This Website is runnig on Ubuntu 20!</h1>
</body>
</html>

Create a Virtual Host File

Create the new virtual host file with the using the following command,

nano /etc/apache2/sites-available/dev.domainhere.info.conf

Add the below lines by replacing the dev.domainhere.info by your actual_domain_name.

<VirtualHost *:80>
ServerAdmin admin@dev.domainhere.info
ServerName dev.domainhere.info
ServerAlias dev.domainhere.info
DocumentRoot /var/www/dev.domainhere.info/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Activate virtual host configuration file

Activate virtual host configuration file using below command,

a2ensite dev.domainhere.info.conf

Output:

root@vps:~# a2ensite dev.domainhere.info.conf
Enabling site dev.domainhere.info
To activate the new configuration, you need to run:
  systemctl reload apache2

Disable the “000-default.conf” default virtual configuration,

a2dissite 000-default.conf

Output:

root@vps:~# a2dissite 000-default.conf
Site 000-default disabled.
To activate the new configuration, you need to run:
  systemctl reload apache2
root@vps:~# 

Restart Apache to activate the new configuration,

systemctl restart apache2

Test for any configuration errors,

apache2ctl configtest

Output:

root@vps:~# apache2ctl configtest
Syntax OK
root@vps:~# 

Test Virtual Host

Let’s test this by navigating to the following link in the browser,

http://dev.domainhere.info

Replace the dev.domainhere.info with your domain name

image

Done.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS