Kunaljain's Weblog

August 8, 2007

Lighttpd Name Based Virtual Hosting

Filed under: Lighttpd — Kunal Jain @ 08:54

Name Based Virtual Hosting

Suppose we have two websites http://www.this.com and http://www.that.com

First create a directory for each domain

# mkdir -p /home/www/vhost/this.com/http
# mkdir -p /home/www/vhost/that.com/http
# chown lighttpd:lighttpd /home/www/vhost/this.com/http
# chown lighttpd:lighttpd /home/www/vhost/that.com/http

Creating log directory for each domain

# mkdir -p /var/log/lighttpd/this.com
# mkdir -p /var/log/lighttpd/that.com
# chown -R lighttpd:lighttpd /var/log/lighttpd/this.com
# chown -R lighttpd:lighttpd /var/log/lighttpd/that.com

Opening lighttpd configuration file
# vi /etc/lighttpd/lighttpd.conf

Add the support for this.com
$HTTP[“host”] =~ “(^|\.)this.com” {
server.document-root = “/home/www/vhost/this.com/http”
server.errorlog = “/var/log/lighttpd/this.com/error.log”
accesslog.filename = “/var/log/lighttpd/this.com/access.log”
server.error-handler-404 = “/e404.php”
}

Add the support for that.com
$HTTP[“host”] =~ “(^|\.)that.com” {
server.document-root = “/home/www/vhost/that.com/http”
server.errorlog = “/var/log/lighttpd/that.com/error.log”
accesslog.filename = “/var/log/lighttpd/that.com/access.log”
server.error-handler-404 = “/e404.php”
}

where,

$HTTP[“host”] =~ “(^|\.)that.com” :- It will match for both http://www.that.com and that.com
server.document-root = “/home/www/vhost/that.com/http” :- Server Document Root
server.errorlog = “/var/log/lighttpd/that.com/error.log” :- Server Error Log File
accesslog.filename = “/var/log/lighttpd/that.com/access.log” :- Server Access Log File
server.error-handler-404 = “/e404.php” :- Web Server Error 404 Handler File

Restarting the lighttpd
# /etc/init.d/./lighttpd restart

**Upload files in the root document and test your configuration

Advertisements

Lighttpd Setup Password Protected Directories(mod_auth)

Filed under: Lighttpd — Kunal Jain @ 08:48

Password Protected Directories

FIRST METHOD

Basic Authentication Method

Sample Setup

Domain Name : this.com

Directory To b Protected with a password : /home/www/vhost/that.com/http/document

UserName : kunal

Lighttpd Password File : /home/lighttpd/.passwd

-> Open /etc/lighttpd/lighttpd.conf

Load module mod_auth

Add the following Three Directives:

auth.backend = “plain”

auth.debug = 2

auth.backend.plain.userfile = “/home/lighttpd./passwd”

Where,

-> auth.backend = “plain” :- you are using plain text backend

-> auth.debug = 2 :- Specify debug level(0 turns off debug message, 1 for authentication ok message

and 2 for detailed/verbose debugging message)

-> auth.backend.plain.userfile = “/home/lighttpd/.passwd” :- Filename of the username:password

Next you have to find the virtual hosting section for domain “that.com” and append the following text

 auth.require = ( "/document/" =>
(
"method" => "basic",
"realm" => "Password protected area",
"require" => "user=kunal"
)
)

Where,

-> auth.require = ( “/document/” => :- Directory Name

-> “method” => “basic”, :- Authentication type

-> “realm” => “Password protected area”, :- Password realm/message shown on the password window

-> “require” => “user=kunal” :- only user kunal can use directory /document/

At the end your configuration should read as follows

$HTTP["host"] =~ "(^|\.)that.com" {
server.document-root = "/home/www/vhost/that.com/http"
server.errorlog = "/var/log/lighttpd/that.com/error.log"
accesslog.filename = "/var/log/lighttpd/that.com/access.log"
server.error-handler-404 = "/e404.php"
auth.require = ( "/document/" =>
(
"method" => "basic",
"realm" => "Password protected area",
"require" => "user=kunal"
)
)
)

Now create a password file

# vi /home/lighttpd/.passwd

Append username and password in .passwd file

kunal:yourpassword

where,

-> kunal :– is the name of the user. Dont use a system user stored in /etc/passwd file

-> yourpassword :- is the password for the user kunal

Save and Close file .passwd

#chown lighttpd:lighttpd /home/lighttpd/.passwd

Restart lighttpd server

# /etc/init.d/./lighttpd restart

Test Your Configuration in a web browser to http://this.com/docs/.

You should be prompted for a username and password.

SECOND METHOD

secure digest authentication(mod_auth)

Setup username and password using htdigest

# htdigest -c /etc/lighttpd/.passwd ‘Authorized users only’ kunal

where,

-> -c :- creates the /etc/lighttpd/.passwd if does not already exist, or deleted and recreated if it does exist

-> /etc/lighttpd/.passwd :- Password file name. It contains username, realm and password.

-> ‘Authorized users only’ :- The realm name to which the user name belongs

-> kunal :- The user name kunal to create or update in /etc/lighttpd/.passwd. if username does not exist in this file

an entry is added. If it does exist, the password is changed

Open /etc/lighttpd/lighttpd.conf

Load module mod_auth

Now, Append following 3 lines:

auth.backend = "htdigest"
auth.backend.htdigest.userfile = "/etc/lighttpd.passwd"
auth.debug = 2

Next you have to find the virtual hosting section for domain “this.com” and append the following text

 auth.require = ( "/docs/" =>
(
"method" => "digest",
"realm" => "Authorized users only",
"require" => "valid-user"
)
)

At the end ur configuration should read as follows

 $HTTP["host"] =~ "(^|\.)that.com" {
server.document-root = "/home/www/vhost/that.com/http"
server.errorlog = "/var/log/lighttpd/that.com/error.log"
accesslog.filename = "/var/log/lighttpd/that.com/access.log"
server.error-handler-404 = "/e404.php"
auth.require = ( "/docs/" =>
(
"method" => "digest",
"realm" => "Authorized users only",
"require" => "valid-user"
)
)
)

Restart lighttpd

# /etc/init.d/./lighttpd restart

Test Your Configuration in a web browser to http://this.com/docs/.

You should be promted for a username and password.

July 31, 2007

Installing and Configuring Lighttpd

Filed under: Lighttpd — Kunal Jain @ 13:46

****Before installing the lighttpd web server you need to install some libraries****

*****For Debian Users*****

apt-get install pcre
apt-get install zlib
apt-get install pcre-devel

*****For CentOS Users*****

yum install pcre
yum install zlib
yum install pcre-devel

****Source code installation****

# cd /home/kunal/downloads/
(kunal is the name of the user, your system has different user name)

***Get latest source code*****

# wget http://lighttpd.net/downlaod/lighttpd-1.4.13.tar.gz

# cd /usr/local/src/

tar -zxvf /home/kunal/downloads/lighttpd-1.4.13.tar.gz

cd lighttpd-1.4.13

***Configuring lighttpd with ./configure

./configure --with-pcre --with-openssl

***Installing lighttpd***

 # make; make install;

***Create lighttpd user and group****

 # groupadd lighttpd

# useradd -g lighttpd -d /var/www/html -s /sbin/nologin lighttpd

****Create a log directory****

 # mkdir /var/log/lighttpd

# chown lighttpd:lighttpd /var/log/lighttpd

****create a configuration directory****

 # mkdir /etc/lighttpd
cp /usr/local/src/lighttpd1.4.13/docs/lighttpd.conf /etc/lighttpd/
chown lighttpd:lighttpd lighttpd.conf
cp /usr/local/src/lighttpd.1.4.13/docs/rc.lighttpd /etc/init.d/ ( redhat user copy rc.lighttpd.redhat )
cd /etc/init.d/
mv rc.lighttpd lighttpd
chown lighttpd:lighttpd lighttpd
chmod +x lighttpd

****configuring lighttpd.conf file****

 cd /etc/lighttpd/

vi lighttpd.conf

****Setup all the directives in lighttpd.conf file****

-> server.document-root = "/home/kunal/www/mysite.com/http" : Specifies default document-root for your server
-> server.port = 80 : Specifies http port for your server (default is 80)
-> server.username = "lighttpd"
-> server.groupname = "lighttpd"
-> server.bind = "server.ip-address" : Specify Server ip address. you can also specifies hostname
-> server.tag = "lighttpd"
-> server.errorlog = "/var/log/lighttpd/error.log" : Specifies the error log file
-> accesslog.filename = "/var/log/lighttpd/access.log" : Specifies the access log file name
-> index-file.names = ( "index.php", "index.html" )

****uncomment****

server.pid-file = “/var/run/lighttpd.pid”

****uncomment the follwing modules****
server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_rewrite”,
“mod_auth”
)

****restarting lighttpd****

cd /etc/init.d/
./lighttpd restart

****verify that lighttpd is running or not.****
$ netstat -ntulp

Open your web browser and test the new set up by trying URL : http://your-site.com/ or http://server-ip/

Create a free website or blog at WordPress.com.