Tuesday, September 23, 2008

enable public_html and use .htaccess and .htpasswd for security

so after installing at home the FC9 box i configured the "httpd.conf" to enable access to
"public_html" directory's and support for ".htaccess" and ".htpasswd" files.

this is how i did it... there are many other ways to configure it... this is my solution :-)

NOTE: create a backup for every file BEFORE YOU EDIT OR CHANGE IT.

enable "public_html"

edit httpd.conf
you have to enable "UserDir public_html "so search it and uncomment it.
also you MUST remove the "UserDir disable" line above (search it).

verify that "LoadModule userdir_module modules/mod_userdir.so" is enabled...
uncomment it if you have to...

in order that apache can access your home dir you have to grant it permissions to do so
this is ls -ld on my home dir.

"drwx-----x 48 Yaniv Yaniv 4096 2008-09-22 21:04 Yaniv"

you have to grant others execute permissions... so use
"chmod 701 your_home_dir" to grand others read on the folder.

create the "public_html" folder inside your home dir.
it must be with 755 permissions.
"drwxr-xr-x 2 Yaniv Yaniv 4096 2008-09-22 16:14 public_html/"

restart apache and thats it.
"/etc/init.d/httpd restart"


.htaccess and .htpasswd
ok lets configure user name and password authentication for "public_html" or every other folder inside it.


edit httpd.conf and search for:
# First, we configure the "default" to be a very restrictive set of
# features.

right below it should be some ""
change it to look like this:


Options Indexes FollowSymLinks
AllowOverride All
NOT REQUIRED (i think so) i have enabled it...
Indexes = it will enable the users that is accessing your_url.com/~user_name/ to see the content...
FollowSymLinks = enables you to create links and they are working...

REQUIRED

AllowOverride All = can be changed to be more specific settings... i have used All

save the changes... and restart apache.

now lets cd to your "public_html" folder

create file named ".htaccess" the dot is a MUST
edit the file (i use vi) and place this inside:
AuthUserFile /your_home_directory/public_html/.htpasswd
AuthName "Title for Protected Site"
AuthType Basic
Require valid-user


AuthUserFile = change the path to your home dir path with /.htpasswd that we will create soon :-)
AuthName = place some tilte there... Welcome to my site... bla bla
AuthType Basic = leave it
Require valid-user = leave it

save the changes and close the file.

now use the htpasswd command to create the ".htpasswd" file.

htpasswd -c .htpasswd your_name

this will create the ".htpasswd" file with your name and your password
the password is encrypted inside the .htpasswd file... :-)

to create more users... lets say you would like to give a friend access to this folder with his own credentials
just cd to the folder where the ".htpasswd" is we just created and use:
htpasswd .htpasswd his_name

and thats it.

now change the permissions for both files
".htaccess"
".htpasswd"
to "644"

it should look like this:
-rw-r--r-- 1 Yaniv Yaniv 103 2008-09-22 16:11 .htaccess
-rw-r--r-- 1 Yaniv Yaniv 58 2008-09-22 20:47 .htpasswd


it should work now...

good luck
Yaniv Ferszt






No comments: