How to Install Apache 2 on Ubuntu 18.04

Reading Time: 6 minutes

Apache 2 is an open source web server that is used to host websites and applications. It's known for being reliable, secure, and fast, making it a popular choice for those looking to install a web server quickly. Apache 2 is relatively easy to install and configure and runs on various platforms, including Linux, Windows, and Mac. It also supports multiple programming languages and databases and is used to develop complex applications and websites.

Apache 2 is free to use, making it an ideal web server for anyone looking to get their website or application up and running quickly. It has support for scripting languages, virtual hosting, and SSL encryption, making it secure and reliable. Overall, installing Apache 2 is a great way to ensure full support for your web hosting needs.

How to Install Apache 2 on Ubuntu 18.04

Apache 2 is an open source web server that is used to host websites and applications. It's known for being reliable, secure, and fast, making it a popular choice for those looking to install a web server quickly. Apache 2 is relatively easy to install and configure and runs on various platforms, including Linux, Windows, and Mac. It also supports multiple programming languages and databases and is used to develop complex applications and websites.

Apache 2 is free to use, making it an ideal web server for anyone looking to get their website or application up and running quickly. It has support for scripting languages, virtual hosting, and SSL encryption, making it secure and reliable. Overall, installing Apache 2 is a great way to ensure full support for your web hosting needs.

Installing Apache 2 on Ubuntu 18.04 can seem daunting, but it doesn't have to be. This step-by-step guide walks through how to install apache2 in Ubuntu 18.04, from downloading the software to configuring settings. You'll have everything you need to get your web server up and running in no time.

Prerequisites for Installing Apache 2 on Ubuntu 18.04

  • A system running Ubuntu 18.04 LTS (Bionic Beaver).
  • Root or sudo privileges on your user account.

Installing Apache 2 on Ubuntu 18.04

You'll be delighted to learn that Apache can be installed on your server using Ubuntu's default package management tools. Let’s get started!

Step 1:

Before installing Apache on Ubuntu 18.04, you need to make sure your system has the most up-to-date packages and software running.

sudo apt update && upgrade

This is an important step, as it will ensure you have the most recent and secure versions of Apache2 and all its related packages.

Step 2:

Install Apache and select Y when prompted.

# sudo apt install apache2
Reading package lists... Done
Building dependency tree  	 
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0
  ssl-cert
Suggested packages:
  www-browser apache2-doc apache2-suexec-pristine | apache2-suexec-custom openssl-blacklist
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0
  ssl-cert
0 upgraded, 10 newly installed, 0 to remove and 35 not upgraded.
Need to get 1730 kB of archives.
After this operation, 6997 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

Step 3:

Once the installation is complete, you can verify the installation by typing in your terminal.

# sudo apache2 -v
Server version: Apache/2.4.29 (Ubuntu)
Server built:   2022-06-23T12:51:37

This should display the version of Apache installed on your system. 

Step 4: 

Now that we have installed Apache, we must start the service.

sudo systemctl start apache2

Step 5:

Enabling Apache will automatically start the web server whenever the server is turned on.

# sudo systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2

Step 6: Configure Your Firewall

If your system has the UFW firewall active and running, you must open ports 80 and 443 to permit client queries to the Apache web server through HTTP and HTTPS, respectively, and then use the following instructions to reload the firewall settings.

Use the command below to list the ufw application profiles.

# sudo ufw app list
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH

There are three profiles for Apache:

  • Apache: Only port 80 is open with this profile (normal, unencrypted web traffic).
  • Apache Full: Both port 443 (TLS/SSL encrypted communication) and port 80 (typical, unencrypted web traffic) are to be used.
  • Apache Secure: Only port 443 (for TLS/SSL-encrypted traffic) is opened.

The following command will open the required ports.

# sudo ufw allow 'Apache Full'
Rules updated
Rules updated (v6)

Check the status to confirm this change.

sudo ufw status

HTTP traffic should be allowed in the displayed output.

# sudo ufw status
Status: active

To                     	Action  	From
--                     	------  	----
Apache Full            	ALLOW   	Anywhere             	 
Apache                 	ALLOW   	Anywhere             	 
Apache Full (v6)       	ALLOW   	Anywhere (v6)        	 
Apache (v6)            	ALLOW   	Anywhere (v6)

Verify Apache Web server installation

Verify Apache by visiting the following page in your browser. In the example below, change server_ip with your actual server IP address. You will see Apache’s default page.

http://server_ip

Optional: Install and Enable PHP

Depending on your needs, say a website with PHP code, you may also want to install and enable PHP.

apt install php libapache2-mod-php php-mysql

Ubuntu 18.04’s default PHP version is 7.2. You’ll see this value reflected when using the following command to confirm.

php --version

Here is the output.

# php --version
PHP 7.2.24-0ubuntu0.18.04.16 (cli) (built: Jan 10 2023 15:47:23) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
	with Zend OPcache v7.2.24-0ubuntu0.18.04.16, Copyright (c) 1999-2018, by Zend Technologies

Where Are Apache's Configuration Files?

All Apache configuration files reside in the /etc/apache2 directory. Below are some of the files to be familiar with.

/etc/apache2/apache2.conf

This is the server's main configuration file. Almost all configurations are done within this file, but it is recommended that separate, designated files be used for simplicity. This file will configure defaults and serve as the server's primary interface for reading configuration details.

/etc/apache2/ports.conf

This file specifies the ports on which virtual hosts should listen.

/etc/apache2/sites-available/ and sites-enabled/

Virtual host file configurations are stored in the sites-available directory. This folder's configurations will determine which content is served for which requests. This is accomplished by connecting to the sites-enabled directory, which contains activated virtual host configuration files. When Apache starts or reloads, it reads the configuration files and links from the sites-enabled directory and compiles a full configuration.

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

These directories are linked in the same way as the sites-available and sites-enabled directories, but they are used to store configuration fragments that do not belong in a virtual host. The a2enconf and a2disconf commands enable and disable files in the conf-available directory.

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

These directories contain the available and enabled modules. Files with the extension .load contain fragments for loading specific modules, whereas files with the extension.conf contain the configuration for those modules. The a2enmod and a2dismod commands enable and disable modules.

/var/www/html

Apache’s default document root. The actual web content is stored here.

Apache Server Logs

Apache Server Logs are an invaluable resource for web admins. They provide information on visitors to a website, such as their IP address, the time they spent on the website, the pages they visited, and more. The logs also monitor security breaches, detect suspicious activity, and optimize website performance. Once you have installed Apache 2 on Ubuntu, it's important to ensure that you have the necessary log files enabled - this can be done by editing the apache configuration files to include the relevant log locations.

Once configured, these logs provide plenty of information to help you understand and manage your website traffic. Apache Server Logs are an important part of server management. They allow you to track user activity and help you identify any potential issues with your setup.

/var/log/apache2/access.log

Every request to your web server is logged in this file by default unless Apache is configured differently.

/var/log/apache2/error.log

All failures that occurred while processing requests are recorded in the Apache error log. The error log's ability to provide details on what went wrong during the Apache server's starting and, most likely, pointers on how to resolve the problem is what matters most.

/var/log/apache2/domlogs

The domain access logs are shown here. They can be beneficial when analyzing different problems, such as attacks, determining where traffic originates, figuring out how worms enter your system, etc. They are also utilized by webstat apps like Webalizer, AWstats, and Analog (AFAIK).

Conclusion

Installing Apache 2 on Ubuntu 18.04 is straightforward and doesn’t require any special knowledge or skills. You can follow this guide to successfully install Apache 2 on Ubuntu 18.04 and get your web server going. Additionally, the configuration settings help you get the most out of your new web server.

The VPS Hosting, Cloud Dedicated Servers, and Dedicated Servers at Liquid Web come with Apache as part of the standard LAMP stack. You can hit the ground running with Apache preloaded for your next project. Contact our sales to configure your server today.

Avatar for Ronald Caldwell

About the Author: Ronald Caldwell

Ron is a Technical Writer at Liquid Web working with the Marketing team. He has 9+ years of experience in Technology. He obtained an Associate of Science in Computer Science from Prairie State College in 2015. He is happily married to his high school sweetheart and lives in Michigan with her and their children.

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article