How to Install and Configure Apache Server on Ubuntu: A Beginner’s Guide

Apache HTTP Server, often just called Apache, is one of the most popular web servers in the world. It’s free, open-source, and reliable, making it a great choice for hosting websites on Ubuntu. Whether you’re setting up a personal project, a development environment, or even a production server, Apache is a solid option.

In this guide, we’ll walk you through installing and configuring Apache on an Ubuntu system step-by-step. No prior experience needed!


Installing Apache on Ubuntu

First, update your package index to make sure you’re installing the latest versions:

sudo apt update

Next, install Apache using the package manager:

sudo apt install apache2

This command installs Apache and its dependencies. After the installation finishes, Apache will start automatically.


Verifying the Installation

To check if Apache is running, open your terminal and run:

sudo systemctl status apache2

You should see a status message saying “active (running).” Another quick test is to open a web browser and type your server’s IP address or localhost if you’re on the same machine:

http://localhost

You should see the default Apache welcome page saying “It works!” If you see that, congrats — Apache is installed and running.


Basic Apache Configuration

Apache’s main configuration file is located at:

/etc/apache2/apache2.conf

Most of the time, you won’t need to edit this file for basic setups, but it’s good to know where it is. Apache organizes websites through virtual hosts, which are separate configuration files that tell Apache how to serve each site.

Virtual host files are stored in:

/etc/apache2/sites-available/

You can create or modify files here to configure your websites.


Setting Up a Virtual Host

To serve your own website, create a new configuration file. For example, if your site is called example.com, run:

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

Add the following configuration:

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

This configuration tells Apache to listen on port 80 for example.com and serve files from /var/www/example.com.


Create Your Website Directory

Create the directory where your website files will live:

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

Make sure the directory permissions allow Apache to read files:

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

You can add an index.html file to test:

echo "<h1>Hello, Apache on Ubuntu!</h1>" | sudo tee /var/www/example.com/index.html

Enable the Virtual Host and Reload Apache

Activate your new site with:

sudo a2ensite example.com.conf

Disable the default site if you don’t need it:

sudo a2dissite 000-default.conf

Finally, reload Apache to apply changes:

sudo systemctl reload apache2

Firewall Settings (if applicable)

If you’re using UFW firewall on Ubuntu, allow Apache traffic:

sudo ufw allow 'Apache Full'

Check the firewall status with:

sudo ufw status

Conclusion

Installing and configuring Apache on Ubuntu is straightforward and a great first step into web hosting. Once Apache is up and running, you can expand your setup with SSL certificates, multiple sites, and advanced modules. This powerful server is highly customizable and well-supported, making it perfect whether you’re a beginner or a seasoned developer.

Happy hosting!