Web Server — Apache সম্পূর্ণ গাইড

লেসন ১৮ / ৩৮

🪶 Apache কেন এখনো?

  • WordPress, cPanel, legacy app default
  • .htaccess per-directory config
  • 1000+ module ecosystem
  • প্রতিটি client-এ probably দেখবেন

📦 Install

sudo apt install apache2 -y
sudo systemctl enable --now apache2

# Files
/etc/apache2/                  # config root
/etc/apache2/sites-available/  # vhosts
/etc/apache2/mods-available/   # modules
/var/www/html/                 # default root

🌐 VirtualHost

# /etc/apache2/sites-available/example.com.conf

    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    
    
        Options Indexes FollowSymLinks
        AllowOverride All       # .htaccess enabled
        Require all granted
    

    ErrorLog ${APACHE_LOG_DIR}/example.error.log
    CustomLog ${APACHE_LOG_DIR}/example.access.log combined


# Enable
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest    # test!
sudo systemctl reload apache2

🔧 Module Management

sudo a2enmod rewrite ssl headers
sudo a2dismod status
sudo systemctl restart apache2

apachectl -M    # all loaded modules

📜 .htaccess Power

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# www → non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Pretty URLs (Laravel)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

# Block bad bot
SetEnvIfNoCase User-Agent "BadBot" bad_bot
Deny from env=bad_bot

# Password protect
AuthType Basic
AuthName "Admin"
AuthUserFile /home/user/.htpasswd
Require valid-user

# Generate password
htpasswd -c /home/user/.htpasswd admin

# Cache headers

    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"


# Block file types

    Require all denied

🆚 Apache vs Nginx

Apache:
+ .htaccess (per-dir flexibility)
+ Mature module ecosystem
+ Easier shared hosting
- Slower static files
- More memory per connection

Nginx:
+ Faster static
+ Better for concurrent
+ Lower memory
+ Reverse proxy excellent
- No .htaccess
- Module compile time

Hybrid: Nginx (frontend) → Apache (backend) — best of both

🛡️ Security

# Hide version (/etc/apache2/conf-enabled/security.conf)
ServerTokens Prod
ServerSignature Off

# Disable directory listing

    Options -Indexes


# Disable trace
TraceEnable off

🎓 মূল কথা ⭐

Apache এখনো 25% Internet। .htaccess শক্তিশালী, কিন্তু performance cost। নতুন প্রজেক্টে Nginx বাছুন। Legacy / cPanel-এ Apache অপরিহার্য।