RHEL7: Configure Apache access restrictions on directories.

Share this link

Note: This is an RHCE 7 exam objective. It has been renamed in June 2016 from “Configure private directories” to “Configure access restrictions on directories” without any particular change.

Prerequisites

First, follow the instructions to install an Apache web server.

Then, create a private directory (called here private):

# cd /var/www/html 
# mkdir private
# echo "This is a test." > private/index.html
# restorecon -R .

There are several ways to restrict access to this directory:

1) host-based private directories

To only allow the test.example.com host (add the name/IP address in the /etc/hosts file if necessary) to access a specific directory (here private), edit the /etc/httpd/conf/httpd.conf file and paste the following lines at the end:

<Directory "/var/www/html/private">
AllowOverride None
Options None
Require host test.example.com
</Directory>

Check the configuration file:

# apachectl configtest
Syntax OK

2) user-based private directories

To only allow me to access a specific directory (here private), edit the /etc/httpd/conf/httpd.conf file and paste the following lines at the end:

<Directory "/var/www/html/private">
AuthType Basic
AuthName "Password protected area"
AuthUserFile /etc/httpd/conf/passwd
Require user me
</Directory>

Check the configuration file:

# apachectl configtest
Syntax OK

Create the passwd file and store me‘s password:

# htpasswd -c /etc/httpd/conf/passwd me
New password: your password
Re-type new password: your password
Adding password for user me
# chmod 600 /etc/httpd/conf/passwd
# chown apache:apache /etc/httpd/conf/passwd

Note: The .htpasswd file can be used locally instead of the httpd.conf file in 1) and 2) for the same purpose.

Whatever the option chosen, restart the httpd service:

# systemctl restart httpd

Configuration Check

Check the httpd service:

# yum install -y curl
# curl -u user:password http://localhost

or

# yum install -y elinks
# elinks http://localhost/private

Useful Tip

If you forget the syntax of some Apache directives, install the httpd-manual package and browse the documentation in the /usr/share/httpd/manual/howto directory:

# yum install -y httpd-manual
# elinks /usr/share/httpd/manual/howto/auth.html

Thanks to Jeromeza for this tip.

(5 votes, average: 4.20 out of 5)
Loading...
33 comments on “RHEL7: Configure Apache access restrictions on directories.
  1. abu3lia says:

    Hi,
    I’ve tried the command [# htpasswd -c passwd user01] but this didn’t create the [passwd] file until I entered the full path for the destination location.

    Regards.

  2. nariman1064 says:

    If during the exam or real life, you have to create the webserver directory under any location other than /var/www/,
    It will certainly take more time to reconfigure everything:
    If you look at “/etc/httpd/conf/httpd.conf” you will notice there are several lines in that file that point to “/var/www/” to set or call different things.
    If in an actual work case, you have to point to a different directory, then there are no issues with taking your time and changing all the paths in httpd.conf to the new location. However, during the exam you have little time to play with these things, the best solution in this case would be to use symlinks.
    Let’s say if you are asked to put your webserver files under /newlocation/, you can create everything there and then create a symlink under /var/www/html for it and in the “/etc/httpd/conf.d/vhosts.conf” file, refer to the symlink for the location :
    Another thing you need to remember is that you have also more work to do other than just restorecon -R :
    Example : let’s say you have to create your webserver under /life/world/ directory instead of /var/www/ ;
    In this case, Selinux will prevent the access and you need to perform below commands to fix the issue:
    (we assume semanage is already installed)
    # semanage fcontext -a -t httpd_sys_script_exec_t “/life/world(/.*)?”
    # semanage fcontext -a -t httpd_sys_content_t “/life/world(/.*)?”
    # restorecon -R -v /life/world/

    • CertDepot says:

      I perfectly agree. Thanks.

      • jeromeza says:

        Could you guys perhaps explain why this option no longer works?

        Symbolic link not allowed or link target not accessible: /var/www/html/dummy-host.example.com/index.html

        # cat /var/www/html/dummy-host.example.com/index.html
        selinux test

        # ls -latrh /var/www/html/dummy-host.example.com/
        index.html > /website/index.html

        • jeromeza says:

          I found the solution myself:

          ====================================
          HTTP – USING AN ALTERNATIVE WEB DIR
          ====================================

          #### INSTALL REQUIRED PACKAGES ####
          yum groupinstall -y “Web server”
          yum -y install setroubleshoot-server selinux-policy

          #### START AND ENABLE SERVICE ####
          systemctl enable httpd
          systemctl start httpd

          #### ADD FIREWALL RULES ####
          firewall-cmd –permanent –add-service=https
          firewall-cmd –reload

          #### MAKE NEW WEB DIR ####
          mkdir /web
          echo testing > /web/index.html

          #### SET SELINUX CONTECT ####

          NOTE: CHECK MAN SEMANAGE-FCONTEXT
          semanage fcontext -a -t httpd_sys_content_t “/web(/.*)?”
          restorecon -R /web

          #### CREATE SYMLINK TO /VAR/WWW/HTML ####

          NOTE: WE DO THIS SO WE DON’T NEED TO EDIT ANY HTTPD CONF FILES FOR DIFFERENT LOCATIONS OR SET DIRECTORY RULES

          ln -s /web/* /var/www/html/mysite/

          NOTE: MAKE SURE /var/www/html/mysite/ IS LISTED AS THE DOCUMENTROOT IN YOUR VHOST CONFIG

          #### RESTART APACHE ####
          systemctl restart httpd

          #### CHECK SITE ####
          curl http://localhost

          • Lisenet says:

            This probably works as long as you have all symlinks in place and none of them are broken. However, what is your plan for making directories available? Say you have a private file located under /web/private/secret.html. How do you think to publish it?

  3. jeromeza says:

    I’ve found that elinks doesn’t seem to handle the auth properly and I don’t get through to the private content.

    Curl seems to a) actually work for me b) be quicker:

    curl -u me:password http://localhost

  4. jeromeza says:

    yum install -y httpd-manual
    less /usr/share/httpd/manual/howto/auth.html

    This helps greatly if you forget the syntax for the auth based directives.

  5. sumon1142 says:

    Hi, how to create the Access Restricted Directory with virtual host configuration? I wrote the Directory stanza in /etc/httpd/conf.d/private.conf and create passwd file. below is my private.conf configuration:

    AuthType Basic
    AuthName “Secret Files”
    AuthUserFile “/etc/httpd/passwd”
    Require user sam

    DocumentRoot /var/www/html/private

    when I am trying to connect using http://localhost/private, it returns ‘not found’ error.

    Can you please explain, What am i missing?

  6. asifshabir says:

    How can we restrict a whole domain.
    e.g
    *.example.local.

    I am trying this without dns but it does not seem to work.

    Options None
    AllowOverride None

    Require all granted
    Require not host *.example.local

  7. Honest Abe says:

    Hi CertDepot,

    Thanks for the tutorials. They provide a very solid base to experiment on.

    I am facing a weird behavior in Curl while practicing access restrictions.

    I have set up Access Restrictions as follows –
    Directory Paths – /var/www/html/{host,user}private/index.html

    where http://webserver/hostprivate should allow/deny specific hosts to view the page
    & http://webserver/userprivate should ask for user’s authentication.

    Configuration :
    A. /etc/httpd/conf.d/01_hostprivate.conf
    <Directory /var/www/html/hostprivate>
    AllowOverride None
    #Options None
    Require host CentOS-Client1.example.com
    #Require ip 10.10.100.2
    </Directory>

    B./etc/httpd/conf.d/02_userprivate.conf
    <Directory /var/www/html/userprivate>
    #AllowOverride None
    AuthType Basic
    AuthName “Restricted Files”
    # (Following line optional)
    AuthBasicProvider file
    AuthUserFile /etc/httpd/conf.d/hpasswd
    Require user mike
    </Directory>

    Firewall is allowed, SELinux is enforced, contexts are correct, permissions are good as well.

    For some weird reason, while curling from (allowed) client, I get the following:

    [root@CentOS-Client1 ~]# curl -k http://10.10.100.1/hostprivate
    <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>
    <html><head>
    <title>301 Moved Permanently</title>
    </head><body>
    <h1>Moved Permanently</h1>
    <p>The document has moved <a href=”http://10.10.100.1/hostprivate/” rel=”nofollow”>here</a>.</p>
    </body></html>

    [root@CentOS-Client1 ~]# curl -u mike:redhat http://10.10.100.1/userprivate
    <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>
    <html><head>
    <title>301 Moved Permanently</title>
    </head><body>
    <h1>Moved Permanently</h1>
    <p>The document has moved <a href=”http://10.10.100.1/userprivate/” rel=”nofollow”>here</a>.</p>
    </body></html>

    However, lynx and firefox are working as expected, since they are prompting for username and password. Any ideas why curl is vomiting?

  8. muhammad.shakeeb says:

    Hi,
    How can I restrict specific ip on apache 2.4.6?
    My configuration is not restricting.

    DocumentRoot “/var/test”

    #
    #
    Directory “/var/test/sysdbagroup”
    AllowOverride AuthConfig
    Require ip 192.168.0.101
    /Directory

    Seems its not working on 2.4 version.

    • Lisenet says:

      The following works for me when put in the Directory section:

         AllowOverride None
         Options None
         Require ip 10.8.8.1/32
      • muhammad.shakeeb says:

        so how would .htaccess works if AllowOverride None. My Scenario is if user1 belongs to dba group and coming from 192.168.0.101, web should be accessible if not then should be denied.

        • Lisenet says:

          Then you can use the following for the group:

          AuthType Basic
          AuthName "Group"
          AuthGroupFile "/path/to/file"
          AuthUserFile "/another/path/to/file"
          Require group dba

          If you want to restrict to specific IPs, then add:

          Require ip 10.8.8.1/32

          To require all conditions to be met, put both into RequireAll.

      • muhammad.shakeeb says:

        what if I need to call .htaccess. For e.g if user1 belongs to dba group and coming from 192.168.0.101 should be allowed else denied.

Leave a Reply

Upcoming Events (Local Time)

There are no events.

Follow me on Twitter

Archives

vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |