DEV Community

Ravi Kyada
Ravi Kyada

Posted on • Originally published at ravijkyada.Medium on

Mastering Apache Web Server: Installation, Multi-Port Configuration, and Best Practices

Mastering Apache Web Server for Ubuntu

The Apache HTTP Server is one of the world's most widely used web servers. What keeps Apache popular today is its mature ecosystem and tooling.

Its Features, such as easy integration with Certbot for SSL certificates, .htaccess-based URL rewriting, and seamless 301 redirects, make it incredibly convenient.

While many modern frameworks ship with their own built-in HTTP servers, there’s still value in having a single entry point—a centralized web server or load balancer with one SSL certificate that routes traffic to various services, not just static content.

But have you ever thought beyond the basic setup? What if you need to run Apache on multiple ports or fine-tune it for custom firewall rules? This guide will walk you through everything — from installation to advanced configuration — in a conversational, step-by-step format.

🧱 Introduction to Apache Web Server

Think of Apache as a highly efficient request handler in your web infrastructure. It listens for incoming HTTP requests, processes them based on configured rules and virtual hosts, retrieves the appropriate resources, such as HTML files or dynamic content via PHP, and delivers the response back to the client’s browser.

Acting as a robust intermediary between the server and the internet, Apache ensures that web traffic is routed, managed, and served reliably and securely.

🌐 Why Apache Still Dominates Web Hosting

Despite newer servers like NGINX, Apache’s modular architecture, ease of configuration, and wide support make it a staple in Linux-based web hosting. It’s flexible enough for a simple blog and powerful enough for a multi-site enterprise application.

⚙️ Installing Apache on Ubuntu

To install Apache on Ubuntu-based systems, run:

sudo apt-get install apache2 -y
Enter fullscreen mode Exit fullscreen mode

This command downloads and installs the latest Apache package with all required dependencies.

🗂️ Default Apache File Structure Explained

Post-installation, here are the key paths you should know:

  • Web root : /var/www/html/index.html
  • Logs : /var/log/apache2/access.log
  • Configuration directory : /etc/apache2

📂 Understanding the Apache Directory Hierarchy

Within /etc/apache2, you’ll find:

  • /sites-available: Where all virtual host configurations live.
  • /sites-enabled: Active configurations linked from sites-available.

To activate a configuration:

sudo a2ensite your-site.conf
Enter fullscreen mode Exit fullscreen mode

✏️ Editing the Index File

Edit your default landing page:

sudo nano /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Replace the default text with something custom, like:

<h1>Welcome to My Apache Server</h1>
Enter fullscreen mode Exit fullscreen mode

📜 Accessing Apache Logs

For troubleshooting and performance monitoring, logs are your best friend:

sudo tail -f /var/log/apache2/access.log
Enter fullscreen mode Exit fullscreen mode

🔧 Introduction to Apache Configuration Files

Every virtual host must be defined in a config file, usually stored in /etc/apache2/sites-available/. Here's a basic example:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

🔗 Enabling Apache Sites with a2ensite

Apache provides a helper command to activate virtual hosts:

sudo a2ensite your-site.conf
Enter fullscreen mode Exit fullscreen mode

This symlinks it to /sites-enabled.

🔁 Reloading Apache Gracefully

After making config changes:

sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

This reloads Apache without dropping current connections.

🧪 Testing Apache Configuration Safely

Before restarting or reloading, test your config:

sudo apachectl configtest
Enter fullscreen mode Exit fullscreen mode

Look for Syntax OK — it’s your green light.

🔀 Configuring Apache to Run on Multiple Ports

Let’s say you want Apache to respond on ports 80 and 81. Start by editing:

sudo nano /etc/apache2/ports.conf
Enter fullscreen mode Exit fullscreen mode

Add:

Listen 81
Enter fullscreen mode Exit fullscreen mode

🏗️ Setting Up Virtual Hosts on Different Ports

Edit your virtual host file:

sudo nano /etc/apache2/sites-available/your-site.conf
Enter fullscreen mode Exit fullscreen mode

Add:

<VirtualHost *:81>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Then enable and reload:

sudo a2ensite your-site.conf
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

🔐 Updating Firewall Rules for Apache Access

If using ufw, run:

sudo ufw allow 81
Enter fullscreen mode Exit fullscreen mode

This opens port 81 to the world. Be cautious and restrict access if needed.

☁️ Running Apache on AWS EC2 — Special Considerations

For EC2 instances, you must also:

  1. Add inbound rule in the EC2 security group.
  2. Allow custom TCP on port 81 with source 0.0.0.0/0.

Access it via:

http://<your-ec2-public-ip>:81
Enter fullscreen mode Exit fullscreen mode

🏁 Conclusion

Apache remains an industry standard because of its deep configurability and reliability. From simple setups to multi-port configurations, mastering Apache gives you full control over your server environment.

❓ FAQ

1. Can I run multiple Apache instances on different ports? Yes, but it’s more efficient to configure virtual hosts on different ports within the same Apache instance.

2. What’s the difference between sites-available and sites-enabled? sites-available contains all configurations; sites-enabled contains symlinks to the active ones.

3. Do I need to open firewall ports for each new Apache port? Yes. For every new port, make sure both your system firewall and cloud security group allow traffic.

4. How do I test if Apache is listening on a port? Run: sudo netstat -tuln | grep :81 or ss -tuln

5. What happens if I don’t reload Apache after config changes? Apache won’t pick up the changes, and your new config won’t be active until you reload or restart it.

Ready to level up your web hosting game? Start by experimenting with multi-port configurations and custom virtual hosts today!

Top comments (0)