How To Install Apache2

How To Install Apache2

Reading time1 min
#WebHosting#Security#Ubuntu#Apache2#Linux#Certbot

Step-by-Step Guide to Securely Installing Apache2 on Ubuntu for Reliable Web Hosting

When it comes to setting up your web server, Apache2 remains one of the most trusted and widely-used HTTP servers in the world. However, simply installing Apache2 is not enough to guarantee a secure and reliable server. Missteps during installation or configuration can open doors to vulnerabilities, performance bottlenecks, and downtime — issues no developer or sysadmin wants to face.

Forget generic install commands—this guide goes beyond basics to embed security best practices and performance tweaks right at the installation stage, saving headaches later. Follow along as I walk you through a practical, secure Apache2 installation on Ubuntu that will form a rock-solid foundation for your web hosting needs.


Prerequisites

  • A clean Ubuntu server instance (Ubuntu 20.04 LTS or above recommended).
  • Root or sudo privileges.
  • Basic familiarity with Linux terminal commands.

Step 1: Update Your System Packages

Before installing any software, it's essential to update your package lists and upgrade existing packages. This ensures you’re downloading the latest, patched versions.

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache2

The simplest command might be sudo apt install apache2, but we’ll take a secure approach by installing with recommended packages:

sudo apt install apache2 apache2-utils -y
  • apache2-utils provides handy utilities like htpasswd for authentication management.

Confirm the installation:

apache2 -v

You should see output like:

Server version: Apache/2.4.xx (Ubuntu)

Step 3: Start Apache and Enable it on Boot

Ensure the Apache service starts immediately and automatically on reboot:

sudo systemctl start apache2
sudo systemctl enable apache2

Check status:

sudo systemctl status apache2

Step 4: Harden Your Firewall Settings to Allow Only Needed Traffic

Ubuntu usually comes with UFW (Uncomplicated Firewall). Allow web traffic through ports 80 (HTTP) and 443 (HTTPS):

sudo ufw allow 'Apache Full'
sudo ufw enable
sudo ufw status

This opens only necessary ports, limiting exposure.


Step 5: Disable Unnecessary Default Modules

Apache loads modules which can impact security and performance. Check loaded modules:

apache2ctl -M

Disable modules that aren’t needed for your use-case. For example, disabling autoindex prevents directory listings which can leak sensitive information:

sudo a2dismod autoindex
sudo systemctl restart apache2

Step 6: Configure ServerTokens and ServerSignature for Privacy

By default, Apache may reveal detailed information about your server version in responses—a useful clue for attackers. Let’s minimize that info.

Open the security config file:

sudo nano /etc/apache2/conf-available/security.conf

Find and set:

ServerTokens Prod
ServerSignature Off

Save (Ctrl+O), exit (Ctrl+X), then reload Apache:

sudo systemctl reload apache2

Now error pages won’t show your Apache version or OS details.


Step 7: Set Up Basic HTTP Authentication (Optional but Recommended)

If you want basic password protection on certain directories, use htpasswd. For example, protecting /var/www/html/private directory:

  1. Create a password file and add a user:
sudo htpasswd -c /etc/apache2/.htpasswd adminuser

(You’ll be prompted to enter a password)

  1. Create the directory if it doesn’t exist:
sudo mkdir /var/www/html/private
  1. Edit or create an .htaccess inside /var/www/html/private with:
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
  1. Enable .htaccess override in the site config (/etc/apache2/sites-available/000-default.conf) by ensuring AllowOverride All is set for that directory block.

  2. Restart Apache to apply changes:

sudo systemctl restart apache2 

Now anyone accessing http://your-server-ip/private/ will need credentials.


Step 8: Enable HTTPS with Let’s Encrypt (Highly Recommended)

Secure communication is non-negotiable in today’s world. Integrate free SSL certificates via Certbot right after install.

  1. Install Certbot and its Apache plugin:
sudo apt install certbot python3-certbot-apache -y 
  1. Request a certificate (replace yourdomain.com):
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com 

Follow prompts to automatically configure HTTPS redirection.

  1. After success, verify HTTPS access through browser or curl:
curl -I https://yourdomain.com 

You should see HTTP/1.1 200 OK with proper certificates.

  1. Certbot sets up automatic renewal cron jobs; you can test renewal process by running:
sudo certbot renew --dry-run 

Step 9: Verify Your Installation

Visit your server IP or domain name in a browser; you should see the default Apache welcome page confirming success!

Run security scan for common issues using tools like Mozilla Observatory or SSL Labs.


Final Thoughts

Installing Apache2 on Ubuntu is straightforward but doing so securely requires attention to these key steps — updating packages first, locking down unnecessary modules, hiding version info, enabling HTTPS early, and optionally adding authentication mechanisms.

By baking these best practices into your install routine, you create a more robust baseline free from common vulnerabilities — saving headache and downtime down the road.

Ready to build further? Next steps could include configuring virtual hosts for multiple websites, fine-tuning performance settings like caching, or implementing monitoring solutions—all starting from this strong foundation.

Feel free to share your experiences or questions in the comments below!