Install Apache in Ubuntu 18

01 Mar 2019, 14:32
by CodeOoze

This article is the first in a mini-series of quick reference guides for installing Apache, PHP, and MySQL in Ubuntu 18. These steps are possibly the minumum required for getting a web development environment up and running, and can be used for local testing of your web development. This first article will cover the installation of the Apache HTTP Server.

My environment for these steps is Ubuntu 18.04 LTS running on VirtualBox in Windows 10 x64.

Update Ubuntu package list

First, make sure your Ubuntu package list is up to date.

sudo apt update

Install Apache HTTP Server in Ubuntu 18

Next, ensure that Apache httpd is not already installed. There are a few different ways to do this. The first is to try print the apache2 version details.

Open a new terminal and type

apache2 -v

If Apache is not installed you will see an error message like this:

Command 'apache2' not found, but can be installed with: sudo apt install apache2-bin

Another method is to use dpkg packet management software (credit to this StackOverflow answer for providing this option.

dpkg --get-selections | grep apache

This command will not return any output if Apache is not installed.

Now we can install the Apache web server

sudo apt install apache2

Sidenote: you may have noticed the error message we got from apache2 -v indicated we should install apache2-bin, but instead we installed apache2 (without the -bin). Why is that?

I previously wondered the same thing and found this SuperUser question: Difference between apache2 and apache2-bin. Basically, apache2-bin is just part of the overall Apache pacakge, so by installing apache2 we not only get apache2-bin, but all the other neccessary items.

At the prompt hit y and then Enter to download the Apache packages and install them.

When the installation is complete, the Apache service will be automatically started. Apache will also run automatically everytime Ubuntu starts.

You can check the Apache service status using systemctl:

sudo systemctl status apache2

Hit Ctrl + C to return to the command prompt.

Open a web browser and navigate to localhost (or 127.0.0.1) and you should see the Apache2 Ubuntu Default Page

Apache configuration file apache2.conf

Apache configuration is specified in the file apache2.conf which is read by Apache during startup. Changes to this file require the Apache service to be restarted.

By default, Apache is installed to the /etc/apache2 directory. We can cd into this folder and list the files:

cd /etc/apache2
ls -l

This is where Apache’s main configuration file apache2.conf can be found.

The full path to this file is: /etc/apache2/apache2.conf

Apache document root

The document root is where Apache stores documents and by default is located at /var/www/html. In general, files placed in this location are accessible via the browser.

Change into the document root /var/www/html and list the files:

cd /var/www/html
ls -l

By default, there will be a single file named index.html. This is the Apache2 Ubuntu Default Page we saw in the browser earlier.

Verification

The basic install of Apache is now complete. You can verify it by trying to print the version details again:

apache2 -v

This command will show the server and build versions. My installed version is Apache/2.4.29 (Ubuntu).

This next step is not neccesary, but can be educational if you are new to Apache. Create a new file within /var/www/html named test.html.