Tag: lighttpd

  • How to install lighttpd from source code

    How to install lighttpd from source code

    Lighttpd is a secure, fast, compliant, and very flexible web server that has been optimized for high-performance environments. In this post, we will be installing Lighttpd by compiling the source code.

    To install lighttpd from the source, first, let’s install the requirements

    For RHEL, CentOS, AlmaLinux, etc..

    dnf -y install wget
    dnf -y group install 'Development Tools'
    

    Use yum instead of dnf if you are using an older version, for example, CentOS 7

    For Ubuntu/Debian

    apt update
    apt -y install build-essential wget
    

    Next, we will download and install lighttpd, you can get the latest version download link from

    https://www.lighttpd.net/

    cd /usr/local/src
    wget http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.64.tar.gz
    tar -xvf lighttpd-1.4.64.tar.gz
    cd lighttpd-1.4.64
    make clean && make distclean
    ./configure --prefix=/usr --with-fam
    make
    make install
    

    If you get error

    configure: error: pcre2-config not found, install the pcre2-devel package or build with --without-pcre2
    

    install pcre2 devel package

    RHEL

    dnf install -y pcre2-devel
    

    For Ubuntu/Debian

    apt install -y libpcre2-dev
    

    If you get error related to zlib “configure: error: zlib headers not found, install them or build without –with-zlib”, install

    For Ubuntu/Debian

    apt install -y zlib1g-dev
    

    Create User

    Let’s create a user for running lighttpd

    groupadd lighttpd
    adduser -m -g lighttpd -d /var/www -s /sbin/nologin lighttpd
    

    For Ubuntu/Debian

    useradd -m  -d /var/www -s /sbin/nologin lighttpd
    

    Instead of creating a new user, you can also use user nobody on RHEL based distros, www-data on Ubuntu.

    Install configuration files

    To setup default lighttpd.conf file, run

    install -Dp ./doc/config/lighttpd.conf /etc/lighttpd/lighttpd.conf
    cp -R doc/config/conf.d/ /etc/lighttpd/
    cp doc/config/conf.d/mod.template /etc/lighttpd/modules.conf
    

    Create a directory for log files

    mkdir /var/log/lighttpd
    chown -R lighttpd:lighttpd /var/log/lighttpd
    

    Copy service files

    cp doc/systemd/lighttpd.service /usr/lib/systemd/system/
    cp doc/systemd/lighttpd.socket /usr/lib/systemd/system/
    

    Update configuration files

    Edit

    vi /etc/lighttpd/modules.conf
    

    Find

    server.modules += ( "mod_Foo" )
    

    Comment the line.

    Edit /etc/lighttpd/lighttpd.conf

    vi /etc/lighttpd/lighttpd.conf
    

    Find

    server.use-ipv6 = "enable"
    

    Replace with

    server.use-ipv6 = "disable"
    

    Update server document root as needed

    var.server_root = "/var/www"
    server.document-root = server_root + "/html"
    

    Starting lighttpd

    Verify configuration file has now errors

    /usr/sbin/lighttpd -tt -f /etc/lighttpd/lighttpd.conf
    

    Run lighttpd manually

    /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
    

    You can enable/start lighttpd with

    systemctl enable lighttpd
    systemctl start lighttpd
    systemctl status lighttpd
    

    back to lighttpd

  • lighttpd too many open files

    lighttpd server crashes with fllowing error in error_log file.

    2019-11-05 09:39:02: (network_linux_sendfile.c.143) open failed:  Too many open files
    2019-11-05 09:39:02: (connections.c.603) connection closed: write failed on fd 1228
    2019-11-05 09:39:02: (response.c.537) file not found ... or so:  Too many open files /4032/1_451.jpg ->
    

    As lighttpd is a single-threaded server, its main resource limit is the number of file descriptors, which is set to 1024 by default (on most systems).

    If you are running a high-traffic site you might want to increase this limit by setting server.max-fds.

    server.max-fds = 8192
    
    [root@server22 lighttpd]# cat /proc/sys/fs/file-nr
    4544    0       95873
    [root@server22 lighttpd]#
    

    Related Posts

    lighttpd

    maximum number of open files and file descriptors in linux

  • lighttpd

    lighttpd is a lightweight web server used mainly to serve static files. Youtube used lighttpd before it was purchased by Google. I was using lighttpd with some file hosting/image hosting sites with mod_secdownload that allows the link to expire after a predefined time.

    To install Lighttpd on CentOS, run

    yum install lighttpd
    

    To see lighttpd version, run

    lighttpd -v
    
  • lighttpd mod alias

    mod_alias lighttpd module allow you to map urls to file system. For example, you need /mrtg show /var/www/mrtg

    You need to first enable mod_alias in lighttpd.conf

    vi /etc/lighttpd/lighttpd.conf
    

    Find

    server.modules              = (
    #                               "mod_rewrite",
    #                               "mod_redirect",
    #                               "mod_alias",
    

    and remove the comment # on “mod_alias” line. It should look like

    server.modules              = (
    #                               "mod_rewrite",
    #                               "mod_redirect",
                                    "mod_alias",
    

    To add alias, add following in lighttpd.conf

    alias.url = ( "/mrtg/" => "/var/www/mrtg/" )
    

    If you need to add more, just use

    alias.url += ( "/cgi-bin/" => "/var/www/cgi-bin/" )
    

    Alias will allow you to access files/programs in the folder with url http://yourdomain-or-ip/mrtg/

    See Lighttpd

  • bind lighttpd to one ip address

    I want to bind lighttpd on IP address 76.76.18.20, so i can use other IP’s for Apache, Apache have “Listen” option to bind apache to specific IP address.

    vi /etc/lighttpd/lighttpd.conf
    

    Find

    #server.bind                = "127.0.0.1"
    

    Replace with

    server.bind                = "76.76.18.20"
    

    See lighttpd

  • Auto restart lighttpd on crash

    To restart lighttpd if it went down or crashed, create a file check_lighttpd.sh

    mkdir /usr/serverok
    vi /usr/serverok/check_lighttpd.sh
    

    With the following content

    #!/bin/bash
    
    /usr/bin/wget --tries=1 --timeout=30 -O /dev/null http://localhost:80/up.html
    
    if [ $? -ne 0 ]; then
    (
    killall -9 lighttpd;
    killall -9 gam_server;
    killall -9 php-cgi;
    /etc/init.d/lighttpd stop;
    mv /var/log/lighttpd/access.log /var/log/lighttpd/access.$(date +%Y%m%d_%H).log
    mv /var/log/lighttpd/error.log /var/log/lighttpd/error.$(date +%Y%m%d_%H).log
    /etc/init.d/lighttpd start;
    echo 'restarting lighttpd' `ps aux` | mail -s "`hostname` restarting lighttpd `who | awk '{print $6}'`" [email protected]
    datetime=`date "+%Y%m%d %H:%M:%S"`
    echo $datetime "failure">>/var/log/lighttpd/check.log
    )
    fi
    
    

    Make it executable

    chmod 755 /usr/serverok/check_lighttpd.sh
    

    Create a file up.html on web root of your web site

    echo "up" > /path/to/docroot/up.html
    

    Now set a cronjob to run every 5 minutes

    crontab -e
    

    Add following cronjob

    */5 * * * *  /usr/serverok/check_lighttpd.sh  1> /dev/null 2> /dev/null
    

    See lighttpd