What is Apache Web Server?

Apache or Apache HTTP server is a free and open source web server, developed and maintained by the Apache Software Foundation. Its popularity can be judged by the fact that around 46% of the websites worldwide are powered by Apache. Apache allows website developers to serve their content over the web. It serves as a delivery man by delivering files requested by users when they enter a domain name in their browser’s address bar.

This tutorial is about installing and configuring Apache2 on your Ubuntu system. The commands and procedures mentioned in this article have been run on an Ubuntu 18.04 LTS system. Since we are using the Ubuntu command line, the Terminal, in this article; you can open it through the system Dash or the Ctrl+Alt+T shortcut.

Install Apache 2 on Ubuntu Linux

Please follow the following steps in order to install the Apache2 software through Ubuntu official repositories.

Step 1: Update system repositories

You can download the latest version of a software by first updating the local package index of Ubuntu repositories. Open the Terminal and enter the following command in order to do so:

$ sudo apt update

Update Ubuntu repositories

Step 2: Install Apache 2 with the apt command

Next, enter the following command as sudo in order to install Apache2 and its required dependencies:

$ sudo apt install apache2

Install Apache web server with apt

You may be prompted with a y/n option to continue installation. Please enter Y, after which the installation procedure will begin.

Step 3: Verify the Apache installation

When the installation is complete, you can check the version number and thus verify that Apache2 is indeed installed on your system by entering the following command:

$ apache2 -version

Check installed Apache version

Configure the Firewall Settings

In order to configure Apache, we first need to allow outside access to certain web ports of our system and allow Apache on your UFW firewall.

Step 1: List the UFW application profiles

In order to configure the firewall, let us first list the application profiles we will need to enable access to Apache. Use the following command to list such available applications:

$ sudo ufw app list

List App presets in UFW Firewall

In the above output, you can see three Apache profiles all providing different levels of security; Apache being the one that provides maximum restriction with port 80 still open.

Step 2: Allow Apache on UFW and verify its status

Allowing Apache on UFW will open port 80 for network traffic, while providing maximum security to the server. Please configure UFW to allow Apache through the following command:

$ sudo ufw allow 'Apache'

Open Apache ports in UFW

The status of UFW will now display Apache enabled on the firewall.

$ sudo ufw status

Configure the Apache Web server Settings

Step 1: Verify that the Apache service is running

The first step is to verify that the Apache2 service is up and running on your system, through the following command:

$ sudo systemctl status apache2

Check Apache status

The status “active (running) verifies that the apache2 service is running.

Step 2: Verify that Apache is running properly and listens on your IP address

You can also verify if Apache is running by requesting a page from the Apache server. For this purpose, you can use your server’s IP in order to access the Apache landing page.

Use the following command to know about your server’s IP:

$ hostname -I

Get server IP addresses

Then try the IPs, one by one from the output, in your web browser as follows:

http://server_IP

In my case, http://192.168.100.4 and http://192.168.100.5. Doing so will display the following Apache web page for Ubuntu, verifying that the Apache server is working properly.

Apache default page

Set Up Virtual Hosts in Apache

A virtual host is similar to what you have server blocks in Nginx. It is used to manage configurations for more than one domain from one server. We will present an example of how to set up a virtual host through the Apache server. We will set up a website named sampledomain.com by using the server block that is enabled by default in Apache for Ubuntu 18.

Step 1: Set up a domain name

The server block that is enabled by default is capable of serving documents from /var/www/html. However, we will create a directory at /var/www/ leaving the default directory intact.

Create this directory through the following command, replacing sampledomain.com by your respective domain name.

sudo mkdir -p /var/www/sampledomain.com/html

Create the directory for virtual host

Then assign the ownership of the directory through the following commands:

sudo chown -R $USER:$USER /var/www/sampledomain.com/html
sudo chmod -R 755 /var/www/sampledomain.com

Change directory ownership

Let us now create an index page that we can later access to test if Apache is running our domain name. Create an HTML file either through the Nano editor or any of your favorite text editor.

$ nano /var/www/sampledomain.com/html/index.html

Enter the following HTML for the index page:

<html>
<head>
<title>Welcome to the page sampledomain.com!</title>
</head>
<body>
<h1>You got Lucky! Your sampledomain.com server block is up!</h1>
</body>
</html>

Sample index pageWe are using the nano editor to create the HTML file.

You can save a file in nano by using Ctrl+X and then enter Y and hitting Enter.

Apache needs a virtual host file to serve the contents of your server. The default configuration file for this purpose is already created but we will make a new one for our custom configurations.

$ sudo nano /etc/apache2/sites-available/sampledomain.com.conf

Enter the following customized configuration details for our domain name:

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

Apache vhost fileWe are using the nano editor to create this .conf file.

You can save a file in nano by using Ctrl+X and then enter Y and hitting Enter.

Step 2: Enable the domain configuration file

Let us enable the configuration file we created with the a2ensite tool:

$ sudo a2ensite sampledomain.com.conf

Enable config file in apache

The output will suggest activating the new configuration but we can do it all collectively after running the following command that disables the original configuration file:

$ sudo a2dissite 000-default.conf

Disable default website

Now restart the Apache service:

$ sudo systemctl restart apache2

Step 3: Test for errors

Finally, let us test if there are any configuration errors through the following command:

$ sudo apache2ctl configtest

If you do not get any errors, you will get the following output:

Test configuration

However, the following error is common in Ubuntu 18.04

Resolve the error:

Enter the following command in order to resolve the above-mentioned error:

$ echo "ServerName sampledomain.com | sudo tee /etc/apache2/conf-available/servername.conf

Resolve servername error

And then:

$ sudo a2enconf servername

Enable servername config

Now when you check again for errors, you will see this error resolved through the following output:

Step 4: Test if Apache is serving your domain name

Apache server is now configured to serve your domain name. This can be verified by entering your server name as follows in any of the web browsers running on your system:

http://sampledomain.com

The index page should display as follows, indicating that Apache is now ready to serve your server block!

Access your website by domain name

Some Common Apache Management Commands

After setting up the web server, you might have to perform some basic management operations on Apache. Here are the commands that you can enter in your Terminal application for these operations.

CommandPurpose
sudo systemctl start apache2Use this command as sudo in order to start the Apache server.
sudo systemctl stop apache2Use this command as sudo in order to stop the Apache server when it is in start mode.sudo systemctl restart apache2Use this command as sudo in order to stop and then start the Apache service again.sudo systemctl reload apache2Use this command as sudo in order to apply the configuration changes without restarting the connection.sudo systemctl enable apache2Use this command as sudo in order to enable Apache to be started every time you boot your system.sudo systemctl disable apache2Use this command as sudo in order to disable if you have set up Apache to be started every time you boot your system.

Conclusion

Through this article, you have learned to install and configure the Apache web server on your Ubuntu system. This includes making some changes to your UFW firewall and then configuring your web server for your IP address. We also recommend you to set up a virtual host through Apache; this will give you a basis on how to use Apache to host your files on the Internet. The basic Apache management commands will also help you as a web administrator to manage your web server in an optimal manner.

How to Install and Configure Apache 2 Web Server on Ubuntu