Apache Server Configs

6.0.0 · active · verified Sun Apr 19

The `apache-server-configs` package provides a robust collection of boilerplate configuration snippets for the Apache HTTP server, currently at version 6.0.0. It aims to enhance web server performance, improve security postures, and ensure resources are served with correct content-types, including cross-domain access when necessary. The project maintains an active development cycle, with major version updates occurring roughly annually, addressing evolving web standards for security, caching, and performance. Minor releases typically introduce new MIME types, expand caching directives, and refine existing configurations. A key differentiator is its focus on modern best practices, including strong `Cache-Control` policies, comprehensive security headers like `Content-Security-Policy`, `Permissions-Policy`, and Cross-Origin policies, and optimized asset handling. It explicitly discourages the use of `.htaccess` files due to performance overhead, advocating for direct integration into `httpd.conf`.

Common errors

Warnings

Install

Quickstart

This script demonstrates how to programmatically deploy and activate the Apache server configurations from `node_modules` into an Apache environment, including enabling necessary modules and verifying the setup via Node.js.

import { promises as fs } from 'fs';
import { join } from 'path';
import { exec } from 'child_process';

const sourceDir = join(process.cwd(), 'node_modules', 'apache-server-configs', 'dist'); // Adjust if config files are not in 'dist'
const apacheConfDir = process.env.APACHE_CONF_DIR || '/etc/apache2/conf.d'; // Common Apache config directory
const httpdConfPath = process.env.HTTPD_CONF_PATH || '/etc/apache2/httpd.conf'; // Example main httpd.conf path

async function deployApacheConfigs() {
  try {
    console.log(`Copying Apache config files from ${sourceDir} to ${apacheConfDir}...`);
    await fs.cp(sourceDir, apacheConfDir, { recursive: true, force: true });
    console.log('Apache config files copied successfully.');

    // Append an Include directive to httpd.conf if not already present
    let httpdConfContent = await fs.readFile(httpdConfPath, 'utf8');
    const includeDirective = `IncludeOptional ${apacheConfDir}/*.conf`;
    if (!httpdConfContent.includes(includeDirective)) {
      console.log(`Adding '${includeDirective}' to ${httpdConfPath}...`);
      await fs.appendFile(httpdConfPath, `\n# H5BP Apache Server Configs\n${includeDirective}\n`);
    } else {
      console.log(`'${includeDirective}' already present in ${httpdConfPath}.`);
    }

    // Enable required modules (assuming a Debian/Ubuntu-like system with a2enmod)
    console.log('Enabling required Apache modules...');
    const modules = ['setenvif', 'headers', 'deflate', 'filter', 'expires', 'rewrite', 'include', 'mime', 'autoindex'];
    const a2enmodCommand = `sudo a2enmod ${modules.join(' ')}`;
    console.log(`Executing: ${a2enmodCommand}`);
    await new Promise((resolve, reject) => {
      exec(a2enmodCommand, (error, stdout, stderr) => {
        if (error) {
          console.error(`Error enabling modules: ${stderr}`);
          return reject(error);
        }
        console.log(`Modules enabled: ${stdout}`);
        resolve();
      });
    });

    // Test Apache configuration
    console.log('Testing Apache configuration...');
    await new Promise((resolve, reject) => {
      exec('sudo apache2 -t', (error, stdout, stderr) => {
        if (error) {
          console.error(`Apache config test failed: ${stderr}`);
          return reject(error);
        }
        console.log(`Apache config test successful: ${stdout}`);
        resolve();
      });
    });

    // Reload Apache to apply new config
    console.log('Reloading Apache service...');
    await new Promise((resolve, reject) => {
      exec('sudo apache2ctl reload', (error, stdout, stderr) => {
        if (error) {
          console.error(`Apache reload failed: ${stderr}`);
          return reject(error);
        }
        console.log(`Apache reloaded successfully: ${stdout}`);
        resolve();
      });
    });

    console.log('Apache server configs deployed and Apache reloaded successfully!');
  } catch (error) {
    console.error('Deployment failed:', error);
    process.exit(1);
  }
}

deployApacheConfigs();

view raw JSON →