Server Management

Installing and Configuring Apache on Ubuntu

Installing and Configuring Apache on Ubuntu: A Step-by-Step Guide

If you’re hosting your own website or application, installing and configuring a web server is a crucial step. One of the most popular and powerful web servers available today is Apache HTTP Server — often just called Apache.

In this guide, we’ll walk you through how to install and configure Apache on Ubuntu, from setting it up to hosting your first site, securing it, and fine-tuning performance.

Whether you’re using a VPS from Vicservers or a local Ubuntu server, this tutorial will give you a production-ready setup.


 What Is Apache?

Apache is an open-source web server developed by the Apache Software Foundation. It powers over 30% of all websites globally and supports a wide range of functionalities, from simple HTML serving to complex PHP and database-backed apps.

Key Benefits:

  • Cross-platform and highly configurable
  • Modules for SSL, security, logging, caching, and more
  • Excellent community support
  • Stable, secure, and scalable

 Prerequisites

Before starting, make sure you have:

✅ An Ubuntu-based server (20.04, 22.04, or later)
✅ Root or sudo access
✅ A terminal or SSH access to your server

If you’re using Vicservers, your VPS is ready with SSH access and Ubuntu pre-installed.


1.  Connect to Your Server

Open your terminal and connect via SSH:

ssh username@your_server_ip

Replace username with your server’s user (e.g., root or ubuntu) and your_server_ip with your VPS IP.


2.  Update the System

It’s always a good idea to update your package list and system before installing new software:

sudo apt update
sudo apt upgrade -y

3.  Install Apache

Now, install Apache using Ubuntu’s built-in package manager:

sudo apt install apache2 -y

Once installed, Apache will start automatically. To check its status:

sudo systemctl status apache2

You should see something like:

active (running)

4.  Test Apache

Open a web browser and visit your server’s IP:

http://your_server_ip

You should see Apache’s default “It works!” page. That confirms Apache is successfully installed and serving content.


5. Configure the UFW Firewall

If you’re using UFW (Uncomplicated Firewall), you need to allow Apache traffic:

sudo ufw allow 'Apache Full'

Then enable UFW (if not already done):

sudo ufw enable
sudo ufw status

You should see ports 80 (HTTP) and 443 (HTTPS) open.


6.  Understand Apache Directory Structure

Apache’s configuration on Ubuntu is located in /etc/apache2. Key directories:

  • /etc/apache2/apache2.conf: Main config file
  • /etc/apache2/sites-available/: Virtual host files
  • /etc/apache2/sites-enabled/: Active sites
  • /var/www/html/: Default document root
  • /etc/apache2/mods-enabled/: Active modules

Apache uses modules and virtual hosts to provide flexible configurations.


7.  Host a Website Using Apache

Let’s host a simple HTML site under a custom domain like example.com.

Step 1: Create a directory for your site

sudo mkdir -p /var/www/example.com/html

Give it proper permissions:

sudo chown -R $USER:$USER /var/www/example.com/html

Step 2: Add a sample webpage

nano /var/www/example.com/html/index.html

Paste in:

<html>
  <head><title>Welcome to Example.com!</title></head>
  <body><h1>Success! Apache is working on your domain.</h1></body>
</html>

Save and exit (Ctrl + O, then Ctrl + X).

Step 3: Create a Virtual Host file

sudo nano /etc/apache2/sites-available/example.com.conf

Paste the following:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Enable the site and reload Apache

sudo a2ensite example.com.conf
sudo systemctl reload apache2

Now, if your domain is pointed to your server’s IP, visiting http://example.com should show your custom page.


8.  Enable Rewrite Module (For Pretty URLs)

Many apps like WordPress require Apache’s mod_rewrite for SEO-friendly URLs.

Enable it with:

sudo a2enmod rewrite
sudo systemctl restart apache2

To allow rewrites in .htaccess files, modify your site config:

sudo nano /etc/apache2/sites-available/example.com.conf

Inside the <VirtualHost> block, add:

<Directory /var/www/example.com/html>
    AllowOverride All
</Directory>

Then reload Apache again:

sudo systemctl reload apache2

9.  Secure Your Site with HTTPS (SSL)

Use Let’s Encrypt and Certbot to add a free SSL certificate.

Step 1: Install Certbot

sudo apt install certbot python3-certbot-apache -y

Step 2: Run Certbot

sudo certbot --apache

Follow the prompts to:

  • Choose your domain
  • Automatically redirect HTTP to HTTPS

Certbot will install the certificate, update Apache configs, and reload the server.

To test renewal:

sudo certbot renew --dry-run

10.  Monitor Apache Logs

Logs help you debug problems:

  • Access log: /var/log/apache2/access.log
  • Error log: /var/log/apache2/error.log

To view them:

tail -f /var/log/apache2/access.log

Or:

tail -f /var/log/apache2/error.log

11.  Performance Tuning (Optional)

Some useful Apache modules and tweaks:

  • mod_deflate: Enables gzip compression
    Enable with:

    sudo a2enmod deflate && sudo systemctl restart apache2
    
  • mod_headers: For caching and security headers
    sudo a2enmod headers
    
  • Adjust MaxRequestWorkers and KeepAlive in /etc/apache2/apache2.conf for high traffic optimization.

12.  Uninstall Apache (If Needed)

To remove Apache:

sudo apt remove apache2
sudo apt autoremove

This will uninstall Apache but not delete your site data or logs.


🏁 Conclusion: Apache + Ubuntu = Power, Flexibility, Control

Setting up Apache on Ubuntu is a foundational step in taking full control of your website or application. Whether you’re building a static site, deploying a PHP app, or running WordPress, Apache offers the stability and configurability needed for serious web hosting.


 Why Use Vicservers for Your Apache Hosting?

Vicservers offers optimized Linux-based VPS and cloud hosting with:

✅ 1-click Ubuntu installation
✅ Full root access
✅ Pre-configured Apache templates (optional)
✅ Free SSL certificates
✅ 24/7 support from real humans
✅ Affordable pricing with guaranteed uptime

Apache performs best on reliable infrastructure. Vicservers gives you the speed, support, and security you need to launch with confidence.


 Ready to Deploy?

Start hosting your site on a rock-solid Apache server today.

Get started at Vicservers.com

We’ll handle the infrastructure — you focus on building.

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button