How to Install Apache on Ubuntu 20.04 and Host Website

Apache is an open source and free web server software developed by the Apache Software Foundation. It is officially called Apache HTTP Server. Apache is one of the oldest, cross-platform web servers and it is beginner-friendly.

In this tutorial, we are going to install Apache version 2 (Apache2) on Ubuntu 20.04. Furthermore, we are going to configure virtual hosts so that more than one website can be hosted on a single server.

Install Apache2 on Ubuntu 20.04

First, check for package updates with the command below.

$ sudo apt update

Second, run the following command to install Apache version 2 from the Ubuntu package repository.

$ sudo apt install apache2

When prompted, enter y to proceed with the installation.

After Apache2 is successfully installed, run the next command to confirm the version.

$ apache2 -v
Check Apache server version
Check Apache server version

Check Apache2 Server Status

Next, check the status of the Apache2 server by running the command below.

$ sudo systemctl status apache2
Check Apache server status
Check Apache server status

By default, the Apache2 service should already be active (running). If not, start the service with the next command.

$ sudo systemctl start apache2

Test the Apache2 Web Server

The last step is testing the Apache2 web server. Open your preferred web browser and enter the IP address of your Ubuntu server. Or enter localhost if you are locally connected to the server.

You should see the Apache2 Ubuntu default page as shown in the image below.

Apache2 Ubuntu default page
Apache2 Ubuntu default page

On behalf of the Apache Software Foundation, we welcome you to Apache2!

Configure Virtual Hosts on Apache2

Virtual Hosts allow you to host more than one website on the same Apache2 server. Whereas on Nginx it is named as server blocks.

By default, Apache2 comes with one virtual host and its configuration information is stored in /etc/apache2/sites-enabled/000-default.conf. Also, the default website root is /var/www/html.

Configuring a new virtual host is easy. For example, I would perform the following steps to configure a virtual host for my website at www.cloudindevs.com. You can follow along and simply replace cloudindevs with your own registered domain name.

Note: Your registered domain name should point to the IP address of your Ubuntu server.

Create a virtual host

Create a new virtual host configuration file by copying the default one as follows.

$ cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/cloudindevs.conf

After that, disable the default virtual host with the command below.

$ sudo a2dissite 000-default.conf

Create document root directory

Run the next command to create a document root directory to store website files.

$ sudo mkdir /var/www/cloudindevs

Next, make the Apache2 service user account (www-data) and group (www-data) the owner of the document root directory as follows.

$ sudo chown www-data:www-data /var/www/cloudindevs

Further, grant the Apache2 service user account (www-data) full access to the document root directory with the next command.

$ sudo chmod 755 /var/www/cloudindevs

Create website index file

Now, create an index file in the document root directory with:

$ sudo nano /var/www/cloudindevs/index.html

Copy and paste the sample HTML code below.

<!DOCTYPE html>
<html>
<head>
<title>Welcome to Cloudindevs!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to Cloudindevs!</h1>
<p>If you see this page, the Cloudindevs website is working correctly!</p>
</body>
</html>

Save and close the index.html file.

Edit the virtual host configuration

Next, edit the new virtual host configuration file with the following command.

$ sudo nano /etc/apache2/sites-available/cloudindevs.conf

Now, uncomment the line that starts with ServerName and then replace www.example.com with the actual website URL. Also, replace /var/www/html with the new document root path.

Below is a sample of what is expected after making the changes.

<VirtualHost *:80>
  
  ServerName www.cloudindevs.com

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/cloudindevs

 
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

  
</VirtualHost>

Save and close the virtual host configuration file.

Enable the Apache2 virtual host

To enable the Apache2 virtual host, run the command below.

$ sudo a2ensite cloudindevs.conf

Finally, restart the Apache2 web server with:

$ sudo systemctl restart apache2

Surely, my new website is now accessible through www.cloudindevs.com as shown in the image below.

Welcome to Cloudindevs virtual host
Welcome to Cloudindevs virtual host

Basic Commands to Manage Apache HTTP Server

As we conclude, here are some basic systemctl commands to help you manage your Apache2 web server.

Use restart to stop the Apache2 service and then start it again.

$ sudo systemctl restart apache2

Use the reload option to tell Apache2 to reload its configuration files but without stopping the service.

$ sudo systemctl reload apache2

Run the stop command to stop the Apache2 service.

$ sudo systemctl stop apache2

To enable the Apache2 service to start up when the server boots, run:

$ sudo systemctl enable apache2

Apache2 helpers

a2ensite - enable an apache2 site or virtual host.

$ sudo a2ensite cloudindevs.conf

a2dissite - used to disable an apache2 site or virtual host.

sudo a2dissite cloudindevs.conf

Basic Apache2 Configuration and Log Files

Configuration files

/etc/apache2/apache2.conf -- main configuration file which pulls information from all other configuration files when the web server starts.

/etc/apache2/ports.conf -- defines listening ports for incoming connections.

/etc/apache2/mods-enabled -- contains configuration information for managing modules.

/etc/apache2/conf-enabled -- contains some global configuration information.

/etc/apache2/sites-available -- stores virtual host configurations.

/etc/apache2/sites-enabled -- defines enabled virtual hosts.

Log files

/var/log/apache2/error.log -- contains information about errors encountered by Apache2.

/var/log/apache2/access.log -- contains all requests processed by Apache2.

Conclusion

In this guide, we installed and configured the Apache HTTP Server along with a virtual host on Ubuntu 20.04. In addition, we looked at some useful commands to manage Apache2. Lastly, we gave a brief description of important configuration and log files used by Apache2.

Leave a Comment