Skip to content

Instantly share code, notes, and snippets.

@AbdullahGhani1
Last active March 27, 2024 09:35
Show Gist options
  • Save AbdullahGhani1/da9d4392be1305550bfe9a2e43821005 to your computer and use it in GitHub Desktop.
Save AbdullahGhani1/da9d4392be1305550bfe9a2e43821005 to your computer and use it in GitHub Desktop.
PAM Authentication For Apache - KodeKloud

PAM Authentication For Apache

The document root /var/www/html of all web apps is on NFS share /data on storage server in Stratos Datacenter. We have a requirement where we want to password protect a directory in the Apache web server document root.
We want to password protect http://:<apache_port>/protected
URL as per the following requirements (you can use any website-url for it like localhost since there are no such specific requirements as of now):
a. We want to use basic authentication.
b. We do not want to use htpasswd file base authentication. Instead, we want to use PAM authentication, i.e Basic Auth + PAM so that we can authenticate with a Linux user.
c. We already have a user jim with password Rc5C9EyvbU which you need to provide access to.
d. You can access the website on LBR link. To do so click on the + button on top of your terminal, select Select port to view on Host 1, and after adding port 80 click on Display Port.

Click on ✔ and Do Task Again

Solution:

Do these task in all app servers(stapp01, stapp02, stapp03)
At first install pwauth
sudo yum --enablerepo=epel -y install mod_authnz_external pwauth 
### Then Edit the config
sudo vi /etc/httpd/conf.d/authnz_external.conf

Add these lines

<Directory /var/www/html/protected>
AuthType Basic
AuthName "PAM Authentication"
AuthBasicProvider external
AuthExternal pwauth
require valid-user
</Directory>
Then create a protected directory
sudo mkdir -p /var/www/html/protected
# Open the index.html file.
# you will found protected directory message
sudo vi /var/www/html/protected/index.html
# Restart and check status of httpd
sudo systemctl restart httpd 
sudo systemctl status httpd
# Finally check from 
# curl command with mentioned username and password
curl -u jim:Rc5C9EyvbU http://localhost:8080/protected/
@amirensit
Copy link

I get error when starting the httpd service: invalid command AuthName

Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com systemd[1]: Starting The Apache HTTP Server...
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com httpd[1212]: AH00526: Syntax error on line...:
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com httpd[1212]: Invalid command ' AuthName ',...n
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com systemd[1]: httpd.service: main process ex...E
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com kill[1213]: kill: cannot find process ""
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com systemd[1]: httpd.service: control process...1
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com systemd[1]: Failed to start The Apache HTT....
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com systemd[1]: Unit httpd.service entered fai....
Apr 05 07:16:03 stapp03.stratos.xfusioncorp.com systemd[1]: httpd.service failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment