Install the Apache Web Server on Ubuntu 18.04, 12.04

Apache is a popular open-source, cross-platform web server that is, by the numbers, the most popular web server in existence. In this quickstart article, we'll see how to install Apache server on Ubuntu, Debain.

Table of Contents

Install Apache

Update your packages:

sudo apt update -y

Now install Apache:

sudo apt install apache2 -y

Then enable and start webserver:

# enable
sudo systemctl enable apache2

# start
sudo systemctl start apache2

Adjust Firewall

First, check firewall is enabled or not:

sudo ufw status

# Status: active/inactive

If status is "active", then allow apache to server traffic:

sudo ufw allow 'Apache'

Check Webserver

Run this command to check website is running or not:

sudo systemctl status apache2

If it is running, you'll see an output like:

● 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 Tue 2022-04-15 12:12:31 UTC; 7min ago
 Main PID: 2583 (apache2)
    Tasks: 55 (limit: 1153)
   CGroup: /system.slice/apache2.service
           ├─2583 /usr/sbin/apache2 -k start
           ├─2585 /usr/sbin/apache2 -k start
           └─2586 /usr/sbin/apache2 -k start

Now you can access default Apache webpage by visiting your server IP:

http://server_ip

Add Virtual Host

In one server, we can host unlimited websites. It is called virtaul host/s. Let's add a domain:

Create the directory for your domain test.com:

sudo mkdir /var/www/test.com

Assign ownership:

sudo chown -R $USER:$USER /var/www/test.com

Set permissions:

sudo chmod -R 755 /var/www/test.com

Create a sample index.html page:

nano /var/www/test.com/index.html

Demo content:

<html>
  <head>
    <title>Welcome to Test.com!</title>
  </head>
  <body>
    <h1>Success! Welcome to Test.com!</h1>
  </body>
</html>

Now, create a new virtual host config file at location /etc/apache2/sites-available for domain test.com:

sudo nano /etc/apache2/sites-available/test.com.conf

And insert the follwing config block:

<VirtualHost *:80>
  ServerAdmin [email protected]
  ServerName test.com
  ServerAlias test.com
  DocumentRoot /var/www/test.com
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the config file. Then enable your virtual host file:

sudo a2ensite test.com.conf

Test for configuration errors:

sudo apache2ctl configtest

# output
# Syntax OK

Disable the default site (optional):

sudo a2dissite 000-default.conf

Finally restart Apache:

sudo systemctl restart apache2

Now visit your domain http://test.com.

That's all. Thanks for reading. 👍


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.