Read in 4 minutes

last updated 

How To Install Apache on Ubuntu 18.04

Apache HTTP server is the most popular web server in the world. It is a free, open-source and cross-platform HTTP server providing powerful features which can be extended by a wide variety of modules.

This tutorial explains how to install and manage the Apache web server on Ubuntu 18.04.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

Install Apache

Apache is available in the default Ubuntu repositories so we can easily install it using the apt package management tool. On Ubuntu and Debian systems the Apache package and the service is called apache2.

First update the package index and afterwards install the apache2 package with the following commands:

sudo apt update
sudo apt install apache2

That’s it, Apache is installed and automatically started. You can check the Apache service status by issuing:

sudo systemctl status apache2
● 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 Sun 2018-06-24 02:17:57 PDT; 2min 41s ago
 Main PID: 3143 (apache2)
    Tasks: 55 (limit: 2321)
   CGroup: /system.slice/apache2.service
           |-3143 /usr/sbin/apache2 -k start
           |-3144 /usr/sbin/apache2 -k start
           `-3145 /usr/sbin/apache2 -k start

Adjust the Firewall

If your Ubuntu server is protected by a firewall you’ll need to open HTTP (80) and HTTPS (443) ports.

Assuming you are using UFW to manage your firewall, you can open the necessary ports with the following command:

sudo ufw allow 'Apache Full'

You can verify the change with:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Verifying the Apache Installation

To verify that everything works correctly, open your browser, type your server IP address http://YOUR_IP_OR_DOMAIN/ and you will see the default Ubuntu 18.04 Apache welcome page as shown below:

The page includes some basic information about Apache configuration files, helper scripts and directory locations.

Manage the Apache service with systemctl

Now that we have Apache installed on our Ubuntu system we can go over and become familiar with the basic service management commands:

To stop the Apache service, run:

sudo systemctl stop apache2

To start it again, type:

sudo systemctl start apache2

To restart the Apache service:

sudo systemctl restart apache2

To reload the Apache service after you made some configuration changes:

sudo systemctl reload apache2

If you want to disable the Apache service to start at boot:

sudo systemctl disable apache2

And to re-enable it again:

sudo systemctl enable apache2

Apache Configuration File’s Structure and Best Practices

  • All Apache configuration files are located in the /etc/apache2 directory.
  • The main Apache configuration file is /etc/apache2/apache2.conf.
  • The ports that Apache will listen to are specified in the /etc/apache2/ports.conf.
  • Apache Virtual Hosts files are stored in /etc/apache2/sites-available directory. The configuration files found in this directory are not used by Apache unless they are linked to the /etc/apache2/sites-enabled directory.
  • To activate a virtual host you need to create a symlink by using the a2ensite command from the configuration files found in the sites-available directory to the sites-enabled directory. To deactivate a virtual host use the a2dissite command.
  • It is a good idea to follow a standard naming convention, for example if your domain name is mydomain.com then the virtual host configuration file should be named /etc/apache2/sites-available/mydomain.com.conf
  • Configuration files which are responsible for loading various Apache modules are located in the /etc/apache2/mods-available/ directory. Configurations in the mods-available directory can be enabled by creating a symlink to the /etc/apache2/mods-enable/ directory with the a2enconf command and disabled with the a2disconf command.
  • Files containing global configuration fragments are stored in the /etc/apache2/conf-available/ directory. Files in the conf-available directory can be enabled by creating a symlink to the /etc/apache2/conf-enabled/ with the a2enconf command and disabled with the a2disconf command.
  • Apache log files (access.log and error.log) are located in the /var/log/apache/ directory. It is recommended to have a different access and error log files for each vhost.
  • You can set your domain document root directory to any location you want. The most common locations for webroot include:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>

Conclusion

You have successfully installed Apache on your Ubuntu 18.04 server. You’re now ready to start deploying your applications and use Apache as a web or proxy server.

If you have any question or feedback feel free to leave a comment.

This post is a part of the how-to-install-lamp-stack-on-ubuntu-18-04 series.
Other posts in this series:

How To Install Apache on Ubuntu 18.04