DEV Community

loading...
Cover image for Install the Apache Web Server on Ubuntu

Install the Apache Web Server on Ubuntu

rich1n profile image Richard Rodrigues ・5 min read

Introduction

The Apache HTTP server is the most used web server. It provides dynamically loadable modules, robust media support, and extensive integration with other popular software.

In this guide, we’ll explain how to install an Apache web server on your Ubuntu 18.04 server.
Prerequisites

Before begin you need a root user with sudo privileges configured on your server. Additionally, you will need to enable a basic firewall to block non-essential ports.

1. Installing Apache


sudo apt update
sudo apt install apache2

2. Adjusting the Firewall

List the ufw application profiles by typing:

sudo ufw app list

You will see a list of profiles:

Output
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH
Enter fullscreen mode Exit fullscreen mode
  • Apache: Opens only port 80 (normal, unencrypted web traffic)
  • Apache Full: Opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Apache Secure: Opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile (allowing the traffic configured). Since we haven’t configured SSL for our server yet in this guide, we will only need to allow traffic on port 80:

sudo ufw allow 'Apache'
sudo ufw status

You should see HTTP traffic allowed in the output:

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Apache                     ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Apache (v6)                ALLOW       Anywhere (v6)
Enter fullscreen mode Exit fullscreen mode

This has been activated to allow access to the web server.

3. Checking your Web Server

Check if the Apache server is running by typing:

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 Tue 2018-04-24 20:14:39 UTC; 9min ago
 Main PID: 2583 (apache2)
    Tasks: 55 (limit: 1153)
   CGroup: /system.slice/apache2.service
           ├─2583 /usr/sbin/apache2 -k start
           ├─2585 /usr/sbin/apache2 -k start
           └─2586 /usr/sbin/apache2 -k start
Enter fullscreen mode Exit fullscreen mode

To check your IP address type:

hostname -I

To check your Public IP address:

curl -4 icanhazip.com

Enter in your browser your Public IP address

http://your_server_ip

You should see the default Apache web page indicating that Apache is working correctly

Alt Text

4. Setting Up Virtual Hosts (Recommended)

You can encapsulate configuration details and host more than one domain from a single server. We will set up a domain called exampleSite (you need to replace it with your own domain name)

Apache has one server block enabled by default that serve documents from the /var/www/html directory. If you want hosting multiple sites, instead of modifying /var/www/html, let’s create a directory structure within /var/www for exampleSite, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Create the directory for example as follows:

sudo mkdir /var/www/exampleSite

assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/exampleSite

The permissions of your web roots should be correct if you haven’t modified your unmask value, but you can make sure by typing:

sudo chmod -R 755 /var/www/exampleSite

Next, create a sample index.html page using nano or your favorite editor:

nano /var/www/exampleSite/index.html

add the following sample HTML:

<html>
    <head>
        <title>Welcome to Site!</title>
    </head>
    <body>
        <h1>Congrats!  The Site virtual host is Ready!</h1>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Save and close the file after that.

In order for Apache to serve this content, you need to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf directly, let’s make a new one at /etc/apache2/sites-available/exampleSite.conf:

sudo nano /etc/apache2/sites-available/exampleSite.conf

Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName exampleSite
    ServerAlias www.exampleSite
    DocumentRoot /var/www/exampleSite
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Save and close.

Enable this file with the a2ensite tool:

sudo a2ensite exampleSite.conf

Disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

Next, let’s test for configuration errors; you should see "Syntax OK" output:

sudo apache2ctl configtest

Restart Apache to implement your changes:

sudo systemctl restart apache2

Test your new site configuration navigating to http://exampleSite, where you should see your html recently pasted.

/var/www/html:

The actual web content, which by default only consists of the default Apache page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering Apache configuration files.

/etc/apache2:

The Apache configuration directory. All of the Apache configuration files reside here.

/etc/apache2/apache2.conf:

The main Apache configuration file. This can be modified to make changes to the Apache global configuration. This file is responsible for loading many of the other files in the configuration directory.

/etc/apache2/ports.conf:

This file specifies the ports that Apache will listen on. By default, Apache listens on port 80 and additionally listens on port 443 when a module providing SSL capabilities is enabled.

/etc/apache2/sites-available/:

The directory where per-site virtual hosts can be stored. Apache will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory with the a2ensite command.

/etc/apache2/sites-enabled/:

The directory where enabled per-site virtual hosts are stored. Typically, these are created by linking to configuration files found in the sites-available directory with the a2ensite. Apache reads the configuration files and links found in this directory when it starts or reloads to compile a complete configuration.

/etc/apache2/conf-available/, /etc/apache2/conf-enabled/:

These directories have the same relationship as the sites-available and sites-enabled directories, but are used to store configuration fragments that do not belong in a virtual host. Files in the conf-available directory can be enabled with the a2enconf command and disabled with the a2disconf command.

/etc/apache2/mods-available/, /etc/apache2/mods-enabled/:

These directories contain the available and enabled modules, respectively. Files in ending in .load contain fragments to load specific modules, while files ending in .conf contain the configuration for those modules. Modules can be enabled and disabled using the a2enmod and a2dismod command.

/var/log/apache2/access.log:

By default, every request to your web server is recorded in this log file unless Apache is configured to do otherwise.

/var/log/apache2/error.log:

By default, all errors are recorded in this file. The LogLevel directive in the Apache configuration specifies how much detail the error logs will contain.

Discussion (0)

pic
Editor guide