How To Install Apache On Linux

How To Install Apache On Linux

Reading time1 min
#Linux#WebServer#OpenSource#Apache#LinuxAdmin#SysAdmin

Mastering Apache Installation on Linux: From Basics to Optimization

Most tutorials stop at “install and run.” This guide dives deeper—showing you how to install Apache on Linux the right way by integrating optimization and security best practices from day one, turning a basic setup into a robust web server foundation.


Why Apache on Linux?

Apache HTTP Server remains one of the most popular web server platforms worldwide due to its flexibility, extensive modules, and rock-solid reliability. Whether you're hosting personal sites, developing cloud services, or building scalable applications, mastering Apache installation on Linux is an invaluable skill.

But beyond simply getting it running, focusing on optimization and security from the start ensures your server performs efficiently and is protected against common threats.

Let's get practical.


Step 1: Preparing Your System

Update your package list first to ensure you’re fetching the latest available software versions:

sudo apt update
sudo apt upgrade -y

Why? Running the latest packages reduces bugs and vulnerabilities.


Step 2: Installing Apache

On Debian-based distributions like Ubuntu:

sudo apt install apache2 -y

On Red Hat-based distros like CentOS or Fedora:

sudo dnf install httpd -y

Verify installation:

apache2 -v       # Debian/Ubuntu
httpd -v         # CentOS/Fedora

Step 3: Starting and Enabling Apache Service

Enable Apache to start at boot:

sudo systemctl enable apache2    # Debian/Ubuntu
sudo systemctl enable httpd      # CentOS/Fedora

Start (or restart) the Apache server:

sudo systemctl start apache2     # Debian/Ubuntu
sudo systemctl start httpd       # CentOS/Fedora

Check server status with:

sudo systemctl status apache2    # Debian/Ubuntu
sudo systemctl status httpd      # CentOS/Fedora

You can now visit your server’s IP address in a browser; you should see the default Apache welcome page.


Step 4: Apache Configuration Basics

Apache’s main config file locations:

  • Debian/Ubuntu: /etc/apache2/apache2.conf
  • Red Hat/CentOS: /etc/httpd/conf/httpd.conf

By default, virtual hosts (sites) are stored as separate config files for modularity:

  • /etc/apache2/sites-available/ (Debian)
  • /etc/httpd/conf.d/ (CentOS)

Creating Your First Virtual Host

Assuming a domain example.com points to your server IP, create a custom virtual host file:

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

Insert basic configuration:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/public_html

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Create document root directory and grant permissions:

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

Enable the new site and reload Apache:

sudo a2ensite example.com.conf    # Debian-based only; for CentOS just ensure conf file exists.
sudo systemctl reload apache2     # or sudo systemctl restart apache2 after config changes.

Place an index.html inside /var/www/example.com/public_html to verify.


Step 5: Optimizing Apache for Performance

1. Enable KeepAlive

Keep connections open between client-server to improve loading time.

Edit /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf and set:

KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

2. Enable Compression (mod_deflate)

Compression reduces data size transferring over the network.

Enable module (Debian):

sudo a2enmod deflate 
sudo systemctl restart apache2 

Add this to your config or .htaccess:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/json application/javascript application/x-javascript text/javascript application/xml application/rss+xml application/xhtml+xml application/font-woff application/font-ttf font/opentype image/svg+xml image/x-icon 
</IfModule>

3. Enable Caching with mod_expires

Set expiry headers so browsers cache content efficiently.

Enable module (Debian):

sudo a2enmod expires 
sudo systemctl restart apache2 

Example configuration snippet to add in your virtual host or .htaccess file:

<IfModule mod_expires.c>
    ExpiresActive On

    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    
    ExpiresByType text/css "access plus 1 week"
    
    ExpiresByType application/javascript "access plus 1 week"
    
    ExpiresByType text/html "access plus 600 seconds"
</IfModule>

4. Adjust MPM Settings (Multi-Processing Modules)

Apache uses MPMs like prefork, worker, or event. On modern Linux servers running PHP-FPM or similar, event is usually best for performance.

On Debian/Ubuntu, switch MPM as needed:

sudo a2dismod mpm_prefork 
sudo a2enmod mpm_event 
sudo systemctl restart apache2 

Tune MPM parameters in /etc/apache2/mods-enabled/mpm_event.conf.

Example tweaking:

<IfModule mpm_event_module>
    StartServers             4        
    MinSpareThreads         25       
    MaxSpareThreads         75       
    ThreadLimit             64       
    ThreadsPerChild         25       
    MaxRequestWorkers      150       
    MaxConnectionsPerChild   0       
</IfModule>

Step 6: Basic Security Hardening

Disable Directory Listing

We already added Options -Indexes in our virtual host <Directory> block. Test by trying to access a folder without an index file — it should give a forbidden error instead of listing files.

Hide Apache Version & OS Details

Edit main config (apache2.conf or httpd.conf) and set:

ServerTokens Prod  
ServerSignature Off  
TraceEnable Off  
Header always unset X-Powered-By  

Reload Apache afterwards.

Enable Firewall Rules for Web Traffic Only

Use UFW on Ubuntu/Debian as follows (assumes UFW installed):

sudo ufw allow 'Apache Full'
sudo ufw enable         # If not already enabled.

This opens port 80 for HTTP and port 443 for HTTPS if SSL is configured later.


Bonus Tip: Enable HTTPS with Let's Encrypt (Optional But Highly Recommended)

Install Certbot for automated SSL certificates assessment—validating your domain ownership and deploying HTTPS easily.

On Ubuntu/Debian:

sudo apt install certbot python3-certbot-apache -y 
sudo certbot --apache -d example.com -d www.example.com  

Follow prompts; Certbot will configure SSL certs & renewals automatically.


Wrapping Up

Installing Apache on Linux is straightforward—but mastering it demands careful configuration beyond basics. By following this step-by-step guide, you’ll not only have a working web server but one optimized for performance and hardened against common security risks right from day one.

From updating packages daily to leveraging modules that compress content and cache resources effectively, these practices will ensure your web apps scale smoothly even under heavy demand.

Now, go build great things with your fast, secure, reliable Apache web server!


Got questions or want me to cover specific advanced topics like load balancing or PHP integration? Drop me a comment below!