1

In DirectAdmin panel I create protect folder for Admin folder, But i need create protect for folder in Ubuntu server 16.04 without use DirectAdmin panel.

For Example in DirectAdmin panel:

Path: /admin

file: .htaccess

AuthGroupFile /dev/null
AuthType Basic
AuthUserFile /home/user/domains/domain.com/.htpasswd/public_html/administrator/.htpasswd
AuthName "admin"
require valid-user

Path: /home/user/domains/domain.com/.htpasswd/public_html/administrator/.htpasswd:

admin:$apr1$Zbp5WV.h$wmBNMkeSuUd./eL9OFwxX.

Picture:

enter image description here

......

Now how to create protect for folder in Ubuntu server 16.04?

6
  • Please be a bit more clear! Feb 3, 2017 at 12:31
  • Hi @George , Edit my question.
    – mySun
    Feb 3, 2017 at 12:36
  • your referring to apache server? Feb 3, 2017 at 12:39
  • No! add picture in my question.
    – mySun
    Feb 3, 2017 at 12:49
  • 1
    Ok I get you now one one moment will post an answer. Feb 3, 2017 at 13:41

1 Answer 1

2

There are two options to do this:

  1. Using the htpasswd commmand, and

  2. Using the .htaccess file

Option 1:

  1. When using the htpasswd command for the first time we use it like this:

    sudo htpasswd -c /etc/apache2/.htpasswd sammy
    
    # you wil be asked for password for user sammy
    # content of .htpasswd would be
    # sammy:$apr1$lzxsIfXG$tmCvCfb49vpPFwKGVsuYz.
    # another_user:$apr1$p1E9MeAf$kiAhneUwr.MhAE2kKGYHK.
    
  2. for additional users leave out the -c argument

    sudo htpasswd /etc/apache2/.htpasswd another_user
    
  3. Configure apache to check this file (.htpasswd):

    a. Open virtual host file for the domain in question:

    sudo nano /etc/apache2/sites-enabled/000-default.conf
    
    
    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
            <Directory "/var/www/html/MyDomain/protect_folder">
                    AuthType Basic
                    AuthName "Restricted Content"
                    AuthUserFile /etc/apache2/.htpasswd
                    Require valid-user
            </Directory>
    </VirtualHost>
    

    b. Restart apache:

      sudo service apache2 restart
    

Option 2: Using .htaccess file

  1. Find the block for the /var/www directory that holds the document root. Turn on .htaccess processing by changing the AllowOverride directive within that block from "None" to "All"

    sudo nano /etc/apache2/apache2.conf
    
    <Directory /var/www/>
          Options Indexes FollowSymLinks
          AllowOverride All
          Require all granted
    </Directory>
    
      # save and close
    
  2. Add .htaccess file to protect folder:

    sudo nano /var/www/html/.htaccess
    
    # Add the following
    AuthType Basic
    AuthName "Restricted Content"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
    
  3. Restart apache:

    sudo service apache2 restart
    

Note:

  1. Advantage of .htpasswd file:

    Apache will not re-read these files on every request that involves the directory, which helps performance.

  2. Advantage of .htaccess file:

    If you can not modify the Virtual host file this is the right choice

  3. You don't have to use /etc/apache2/sites-enabled/000-default.conf use any virtual host you may have created

  4. This is for a VPS you control, for shared hosting setting it up will be host dependent and is usually automated seek advice from customer care at your shared host.

Source:

https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-apache-on-ubuntu-14-04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .