How To Install Apache 2 On Ubuntu 18.04 LTS

How To Install Apache 2 On Ubuntu 18.04 LTS

Explains all the steps required to install Apache 2 Web Server on Ubuntu 18.04 LTS.

April 17, 2019

In this tutorial, we will discuss all the steps required to install Apache 2 Web Server on Ubuntu 18.04 LTS. The steps required to install Apache 2 on other Ubuntu versions and Linux systems should be similar to the steps mentioned in this post.

Install Apache2

Execute the below-mentioned commands to install Apache 2 on Ubuntu 18.04 LTS.

# Refresh the indexes
sudo apt-get update

# Install Apache
sudo apt-get install apache2

# Verify Apache
apache2 -version

# Output
Server version: Apache/2.4.29 (Ubuntu)
Server built: 2020-03-13T12:26:16

# Autoclean
sudo apt-get autoclean

# Autoremove
sudo apt-get autoremove
# OR
sudo apt-get --purge autoremove

The above-mentioned commands install the latest version 2.4.29 of Apache 2 while writing this tutorial.

Install Apache 2 Modules

We will also install the security module if not done yet, using the below-mentioned command.

# Install security
sudo apt-get install libapache2-mod-security2

# Enable additional modules
sudo a2enmod rewrite ssl security2 deflate expires headers

# Restart Apache 2
sudo systemctl restart apache2

Check Installation

You can verify the installation by opening the URL - http://localhost using any Browser. It must show the welcome page as shown in Fig 1.

Apache 2 Welcome Page

Fig 1

Basic Commands

This section explains the basic commands available in Ubuntu, and specific to the Apache Web Server installed by us.

# Stop the Server
sudo service apache2 stop
# OR
sudo systemctl stop apache2

# Start the Server
sudo service apache2 start
# OR
sudo systemctl start apache2

# Re-start the Server
sudo service apache2 restart
# OR
sudo systemctl restart apache2

# Server Status
sudo systemctl status apache2

# Reload the Server
sudo service apache2 reload

# Enable Site
sudo a2ensite <config name>

# Disabled Site
sudo a2dissite <Config Name>

# Enable Module
sudo a2enmod <module name>

# Disable Module
sudo a2dismod <module name>

We must always use the reload command to apply the site configurations without disrupting the active sessions.

Basic Configuration

In this section, we will do some basic configuration specific to performance. These are optional and configure only if required.


Configure the keep-alive settings as mentioned below. The actual settings might vary based on your requirements.

# Open apache2 configuration
sudo nano /etc/apache2/apache2.conf

# Check the values of below mentioned options
KeepAlive On
MaxKeepAliveRequests 50
KeepAliveTimeout 5

MPM - Prefork

Configure server process as shown below. Change the values based on requirements.

#sudo nano /etc/apache2/mods-available/mpm_prefork.conf

<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0

# Save the settings

# Disable MPM Event
sudo a2dismod mpm_event

# Enable MPM Prefork
sudo a2enmod mpm_prefork

Production Mode

Update server tokens and signature as shown below.

# Tokens and Signature
sudo nano /etc/apache2/conf-enabled/security.conf

ServerTokens Prod
ServerSignature Off

# Restart Apache 2
sudo systemctl restart apache2

These are the basic steps required to install Apache 2 and the basic configurations that can be applied to it.

Secure Apache

You can further secure the websites and web applications deployed on Apache using the SSL certificate. The most commonly used service for SSL certificates is Let's Encrypt. You can follow Configure Virtual Host On Apache to configure the websites for production usage. You can also follow How To Install Let's Encrypt For Apache On Ubuntu and Redirect HTTP to HTTPS on Apache to secure your websites using Let's Encrypt.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS