Install Apache in Ubuntu 18

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

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

    sudo apt update
    

Install Apache HTTP Server in Ubuntu 18

  1. 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.

  2. 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.

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

    Apache package install prompt

  4. 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.

    Apache service status

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

    Apache default page in Firefox

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.

  1. 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
    
  2. This is where Apache’s main configuration file apache2.conf can be found.

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

    /etc/apache2 directory contents

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.

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

    cd /var/www/html
    ls -l
    
  2. By default, there will be a single file named index.html. This is the Apache2 Ubuntu Default Page we saw in the browser earlier.

    /var/www/html directory contents

Verification

  1. 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).

    apache2 -v command

  2. 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.

    sudo gedit test.html
    

    Paste the below html into the file and save:

    <html>
        <head><title>Apache test</title></head>
        <body>
            <p1>Apache 2 web server - it works</p1>
        </body>
    </html>
    

    In your browser navigate to localhost/test.html (or 127.0.0.1/test.html) and you should see the words Apache 2 web server - it works.

    test.html in Firefox

Wrapping up

That’s it for this quick article. In the next part we’ll install MySQL in Ubuntu.