Shubham S Nimje logo
Shubham Nimje
Apache

Password Protection with Apache on Linux

Password Protection with Apache on Linux
2 min read
#Apache

Password Protection with Apache on Linux

Enhance the security of your website by leveraging basic authentication with Apache on your Linux server. This guide walks you through creating login credentials, configuring password protection for a specific directory, and restarting Apache to apply the changes.

Prerequisites

  • An Apache web server running on a Linux server/VPS with root or sudo privileges.
  • Basic understanding of command line editing.
  • Access to your server via SSH.

Security Considerations

  • Password Strength: Choose strong and unique passwords for enhanced security. Consider using a password manager to generate and store complex passwords.
  • Alternative Methods: Basic authentication transmits credentials in Base64 encoding, which is not ideal for highly sensitive areas. Explore alternative methods like HTTPS with client-side certificates for more robust security when handling sensitive data.
  • Regular Updates: Keep your Apache web server and operating system updated with the latest security patches to address vulnerabilities.

Instructions

1. Create Login Credentials

  1. Generate a Password File:

    • Command: htpasswd -c /etc/apache2/.htpasswd user_name
    • Note: The -c option creates a new file. Omit this option if you want to add users to an existing file.
  2. Verify the Credentials:

    • Command: cat /etc/apache2/.htpasswd

2. Configure Password Protection

  1. Navigate to Apache Configuration Directory:

    • Command: cd /etc/apache2/sites-available
  2. Open the Required Virtual Host File:

    • Command: nano 000-default.conf
  3. Add the Following Content to the Virtual Host File:

    <Directory /var/www/html/admin>
        AuthType Basic
        AuthName "admin area"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </Directory>
    • Note: Replace /var/www/html/admin with the directory you want to protect.

3. Restart Apache

  1. Restart Apache to Apply Changes:
    • Command: service apache2 restart