Web
Difficulty: Beginner
3 min read

Apache2: Installing a Web Server

Detailed tutorial to install and configure the Apache 2 web server on a Linux distribution.

Back to tutorials
What is Apache?
The Apache HTTP Server, commonly called Apache, is a free and open-source web server software. It played a key role in the early growth of the World Wide Web and remains one of the most popular web servers in the world. It is known for its flexibility, power, and broad compatibility.

Prerequisites

  • A Linux distribution (this guide focuses on Ubuntu/Debian).
  • Root access or sudo privileges.

Step 1: Installing Apache

Apache is available in Ubuntu's default software repositories, which makes installation very simple.

# Update the package list
sudo apt update

# Install Apache2
sudo apt install -y apache2

Step 2: Configuring the firewall

If a firewall is active on your server (such as UFW), you need to allow web traffic.

# List the application profiles for UFW
sudo ufw app list

# Allow traffic on ports 80 (HTTP) and 443 (HTTPS)
sudo ufw allow 'Apache Full'

# Check the status
sudo ufw status

Step 3: Verify that the web server is working

Once the installation is complete, the Apache service should start automatically. You can verify it.

sudo systemctl status apache2

To confirm that it is accessible from the outside, open a web browser and enter your server's IP address. You should see the default Apache2 welcome page for Ubuntu.

Server Up and Running!
If you see the default page, your Apache web server is installed and operational.

Step 4: Set up a site with a Virtual Host

"Virtual Hosts" allow you to host multiple websites on a single server. It is good practice to create a virtual host for each site, even if you only have one.

1. Create the site directory

We will create a directory for our `example.com` site in `/var/www`.

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

2. Set the correct permissions

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

3. Create a sample page

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

Add this simple HTML content:

<html>
    <head>
        <title>Welcome to Example.com!</title>
    </head>
    <body>
        <h1>Success! The example.com virtual host is working!</h1>
    </body>
</html>

4. Create the Virtual Host file

Apache stores site configurations in `/etc/apache2/sites-available`. Let's create one for our site.

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

Paste this basic configuration:



    ServerAdmin webmaster@localhost
    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

5. Enable the new site

Apache uses symbolic links to enable sites. The `a2ensite` tool makes this easy.

# Enable the new site
sudo a2ensite example.com.conf

# Disable the default site
sudo a2dissite 000-default.conf

# Check the configuration for syntax errors
sudo apache2ctl configtest

# Reload Apache so the changes take effect
sudo systemctl reload apache2

If you have configured your DNS locally (via the `hosts` file) or publicly so that `example.com` points to your server's IP, you should now see your new welcome page when accessing it.

Conclusion

You now have a working Apache web server, capable of hosting one or more sites. This is the foundation of many web applications and websites. The next steps usually involve securing your site with HTTPS (using Let's Encrypt and Certbot), installing a programming language such as PHP, or setting up a database such as MySQL.

Written by

Morgann Riu

Cybersecurity and Linux administration expert. I share my knowledge through free tutorials and training to help system administrators and developers secure their infrastructures.

Frequently asked questions

What is an Apache Virtual Host and what is it used for?
A Virtual Host lets you host multiple websites on a single Apache server. Each site has its own configuration (domain name, root directory, logs). Apache determines which site to serve based on the domain name of the incoming HTTP request.
How do I enable an Apache module?
Use the a2enmod command followed by the module name. For example: sudo a2enmod rewrite for the URL rewriting module, or sudo a2enmod ssl for HTTPS support. Then restart Apache with sudo systemctl restart apache2 to apply the changes.
How do I secure an Apache server?
The key steps are: enable HTTPS with Let's Encrypt (certbot), disable directory listing (Options -Indexes), hide the Apache version (ServerTokens Prod), configure security headers (X-Frame-Options, Content-Security-Policy), and restrict access with the Require directives.
How do I check Apache errors?
The error logs are located in /var/log/apache2/error.log by default. Use sudo tail -f /var/log/apache2/error.log to follow errors in real time. Check the configuration syntax with sudo apache2ctl configtest before any restart.

Share this tutorial

Did you enjoy this article?

Comments

Checklist Sécurité Linux

30 points essentiels pour sécuriser un serveur Linux. Recevez aussi les nouveaux tutoriels par email.

Pas de spam. Désabonnement en 1 clic.