Install lighttpd on FreeBSD

Lighttpd web server

Install, configure, and maintain the secure, fast, high-performance Lighttpd HTTP web server on FreeBSD. A step-by-step article shows how to configure Lighttpd and various Lighttpd web server modules.

Topics covered:
  • 1.1Install Lighttpd
    • 1.1.1Locate lighttpd
    • 1.1.2Auto-start lighttpd
  • 1.2Lighttpd configuration
    • 1.2.1Basic configurations
    • 1.2.2System modules
    • 1.2.3System mime
    • 1.2.4Start lighttpd
  • 1.3Lighttpd vhosts
    • 1.3.1Lighttpd website directory
    • 1.3.2Website vhosts config
  • 1.4Troubleshoot lighttpd
    • 1.4.1Check lighttpd config
    • 1.4.2Lighttpd process
    • 1.4.3Lighttpd port
    • 1.4.4Lighttpd unresponsive

Buy this service

The service includes the installation and setup of a Lighttpd web server on a single FreeBSD server. The customer must provide remote SSH access to the FreeBSD VPS or cloud server.

Product name:
Install Lighttpd on FreeBSD
Product ID:
GWINC-SE-D2C7I6
Price*:
US $69.95 — Buy Now

*GST extra, please add 18% GST to the above price. GST is not applicable for orders outside India. International payments are accepted only through PayPal.

Affiliate links

Setting up a custom Lighttpd web server requires a VPS with root access. Use our affiliate links to purchase a VPS or cloud server from third-party vendors. The affiliate commissions we earn facilitate, Free website access for everyone.

The affiliate links are listed in alphabetical order without any favor. Users are encouraged to refer to the Global Webdynamics LLP Terms of Service governing the Third-party vendors.

1.1Install Lighttpd

Install Lighttpd using the FreeBSD package manager. For installation with custom configurations, install Lighttpd web server from the source code. The article covers the lighttpd installation from the FreeBSD package manager only.

Terminal ~ 1.1-1
$ sudo pkg install lighttpd

1.1.1Locate lighttpd

Find where lighttpd binaries, configurations, and documentation files are installed system-wide.

Terminal ~ 1.1-2
$ which lighttpd
/usr/local/sbin/lighttpd

$ sudo find /usr/local -name "lighttpd"
/usr/local/sbin/lighttpd
/usr/local/share/doc/lighttpd
/usr/local/etc/rc.d/lighttpd
/usr/local/etc/lighttpd
/usr/local/lib/lighttpd

1.1.2Auto-start lighttpd

To auto-start lighttpd on system boot, add the following line to the end of the system run-commands configuration file /etc/rc.conf.

Terminal ~ 1.1-3
$ sudo nano -w -c /etc/rc.conf
...
lighttpd_enable="YES"

1.2Lighttpd configuration

At the top of the Lighttpd configuration file ( lighttpd.conf ), define the commonly used global configuration variables. Also, create a Lighttpd web server cache and log directories. Set 755 as directory permission and make these directories owned by the username and group www.

Terminal ~ 1.2-1
$ sudo nano -w -c /usr/local/etc/lighttpd/lighttpd.conf
...
var.server_root = "/usr/local/www/data"
var.server_port = 8001
var.socket_root = "/tmp"
var.cache_root = "/usr/local/var/lighttpd/cache"
var.log_root = "/usr/local/var/lighttpd/log"
...

# Web server root dir
$ sudo mkdir -p /usr/local/www/data
$ chmod -R 755 /usr/local/www
$ chown -R www:www /usr/local/www

# Web server cache and log dir
$ sudo mkdir -p /usr/local/var/lighttpd/{cache,log}
$ chmod -R 755 /usr/local/var/lighttpd
$ chown -R www:www /usr/local/var/lighttpd

1.2.1Basic configurations

Following are the basic Lighttpd web server configurations. Please consult Lighttpd documentation for other configuration settings. Open the Lighttpd configuration file and update the config settings as per requirement.

Terminal ~ 1.2-2
$ sudo nano -w -c /usr/local/etc/lighttpd/lighttpd.conf
...
##  Basic Configuration
## ---------------------
server.port = server_port
...
server.username = "www"
server.groupname = "www"
...
server.document-root = server_root
...
## module includes
include "conf.d/deflate.conf"
include "conf.d/fastcgi.conf"
include "conf.d/expire.conf"
...
##  Logging Options
## ------------------
...
server.errorlog = log_root + "/error.log"
...
## Access log config
...
include "conf.d/access_log.conf"
...
##  Tuning/Performance
## --------------------
...
server.max-keep-alive-idle = 5
server.max-keep-alive-requests = 16
server.max-read-idle = 60
server.max-write-idle = 360
...
##  Filename/File handling
## ------------------------
...
## mimetype mapping
...
include "conf.d/mime.conf"
...
## Should lighttpd follow symlinks?
...
server.follow-symlink = "enable"
...
## custom includes like vhosts.
...
include "conf.d/gwinc_expire.conf"
include "vhosts.d/*.conf"
...

1.2.2System modules

The Lightttpd provides a modules.conf configuration file to load web server modules. The required modules are added to server.modules configuration list. The order of the modules in server.modules is significant. Please refer to Lighttpd documentation for more information on the order of the modules.

Terminal ~ 1.2-3
$ sudo nano -w -c /usr/local/etc/lighttpd/modules.conf
...
server.modules = (
  "mod_access",
  ...
  "mod_auth",
  ...
  "mod_setenv",
  "mod_rewrite"
  ...
)
...

1.2.3System mime

The Lighttpd web server mime file type mappings are configured in the mime.conf file. Almost all types of mime file type mappings are available by default. But, if any additional mime type is required, add it to the mimetype.assign list.

The web fonts .woff and .woff2 and favicon icon mime type .ico are added to the web server mimetype.assign configuration list. This will help the Lightttpd web server to set the correct Content-Type in the HTTP response.

Terminal ~ 1.2-4
$ sudo nano -w -c /usr/local/etc/lighttpd/conf.d/mime.conf
...
mimetype.assign = (
    ...
    ".woff" => "application/font-woff",
    ".woff2" => "application/font-woff",
    ...
    ".ico" => "image/x-icon",
    ...
)
...

1.2.4Start lighttpd

Finally, start the lighttpd web server.

Terminal ~ 1.2-5
$ sudo service lighttpd start

Check whether lighttpd is running using the top command with username flag ( -U ) and the username as www. Refer to the last column COMMAND, where lighttpd will be listed if the web server is running.

Terminal ~ 1.2-6
$ top -U www
last pid: 51995;  load averages:  2.27,  1.11,  0.55; battery: 98%
118 processes: 1 running, 116 sleeping, 1 zombie
CPU:  0.6% user,  0.0% nice,  0.0% system,  0.0% interrupt, 99.4% idle
Mem: 677M Active, 370M Inact, 286M Laundry, 502M Wired, 67M Buf, 2010M Free
Swap: 4034M Total, 548M Used, 3486M Free, 13% Inuse

  PID USERNAME    THR PRI NICE   SIZE    RES STATE    C   TIME    WCPU COMMAND
 6297 www           1  20    0    14M  2808K kqread   1   0:01   0.01% lighttpd
...

1.3Lighttpd vhosts

Lighttpd virtual hosts ( vhosts ) configuration allows to configure multiple virtual hosts mapped to respective website domains. The vhosts config files under the directory vhosts.d will enable the hosting of multiple websites on a single physical or virtual server.

1.3.1Lighttpd website directory

To create a Lighttpd website example.com, first, create a website's root directory and the virtual host configuration file.

1.3.1-1Website root directory

Create an example.com website's root directory. The Lightppd web server will serve the example.com website static files request from this root directory.

Create the css, js, font, xml, txt, and img resource directories to hold CSS, JavaScript, font, and image files, respectively.

Terminal ~ 1.3-1
# Website's root directory
$ sudo mkdir -p /usr/local/www/data/example.com/data

# Website's resource directory
$ cd /usr/local/www/data/example.com/data
$ sudo mkdir {css,js,font,xml,txt,img}

# Set permissions
$ chmod -R 755 /usr/local/www
$ chown -R www:www /usr/local/www

1.3.1-2Cache and log directory

Start the Lighttpd web server. When the lighttpd service is started, the web server will run the CppCMS application server process using FastCGI protocol through a Unix socket.

Terminal ~ 1.3-2
$ sudo mkdir -p /usr/local/var/lighttpd/{cache,log}/example.com

# Set permissions
$ sudo chmod -R 755 /usr/local/var/lighttpd
$ sudo chown -R www:www /usr/local/var/lighttpd

1.3.2Website vhosts config

Create the Lighttpd virtual host configuration file example.com.conf under the vhosts.d directory. Configure the virtual host config file example.com.conf to host the website domain example.com on the Lighttpd web server.

The url.rewrite-once section contains the URL rewrite rules. The first rule looks for CSS and JavaScript files under the css and js directory, respectively. The regular expression used to match the file name has a version component. Whenever the version number changes, the web browser will fetch the CSS or JavaScript files from the server instead of using cached files.

The web URL path https://example.com/base-20230601.css will be mapped to the css file ( /css/base.css ) under the css directory.

Terminal ~ 1.3-3
$ sudo nano -w -c /usr/local/etc/lighttpd/vhosts.d/example.com.conf
# VIRTUAL HOST (example.com)

$HTTP["host"] == "example.com" {

  var.vhost_name = "example.com"
  server.name = vhost_name

  server.document-root = server_root + "/" + vhost_name + "/data"
  accesslog.filename = log_root + "/" + vhost_name + "/access.log"
  deflate.cache-dir = cache_root + "/" + vhost_name

  static-file.exclude-extensions += (".h", ".c", ".hpp", ".cpp")

  setenv.add-response-header += (
	"Access-Control-Allow-Origin" => "*",
	"Access-Control-Allow-Methods" => "GET, POST"
  )

  url.rewrite-once = (
    "^/(.+)(\-[0-9]+)(\.)(css|js)$" => "/$4/$1$3$4",
    "^/(.+)(\.)(woff|woff2)$" => "/font/$1$2$3",
    "^/(.+)(\.)(xml|txt)$" => "/$3/$1$2$3",
    "^/(.+)(\.)(ico|jpg|png|gif|svg)$" => "/img/$1$2$3"
  )
}

1.3.2-1Restart lighttpd

Restart the Lighttpd web server. Assuming that the test.jpg image file already exists in the img directory, the image test.jpg can be accessed using the URL: https://example.com/test.jpg in the web browser.

Terminal ~ 1.3-4
$ sudo service lighttpd restart

1.4Troubleshoot lighttpd

If the Lighttpd web server is not working as expected after the installation and setup, the following are some Lighttpd troubleshooting options.

1.4.1Check lighttpd config

Check whether the Lighttpd configuration file has any errors. If Lighttpd is correctly configured, the lighttpd command with the flag -t will exit with Syntax OK.

The lighttpd command with the flag -tt will exit without errors if the external configuration files and modules are correctly loaded and initialized.

To execute the lighttpd command with flag -D, the Lighttpd server should not be running. Stop the server or kill the lighttpd process before using the flag -D. This is useful to find Shared object not found errors.

Terminal ~ 1.4-1
# -t: test config-file syntax, then exit
# -f: filename of the config-file
$ lighttpd -t -f /usr/local/etc/lighttpd/lighttpd.conf
Syntax OK

# -tt: test config-file syntax, load and init modules, then exit
$ lighttpd -tt -f /usr/local/etc/lighttpd/lighttpd.conf

# -D: don't go to background (default: go to background)
$ lighttpd -D -f /usr/local/etc/lighttpd/lighttpd.conf

1.4.2Lighttpd process

Find about the Lighttpd process information using the top command with the process owner username www as the input. The PID and RES mention the process ID and RAM used by the Lighttpd, respectively.

Terminal ~ 1.4-2
$ top -U www
...
 PID USERNAME    THR PRI NICE   SIZE    RES STATE   C   TIME    WCPU COMMAND
5388 www           1  20    0 34196K  4188K kqread  0   0:00   0.00% lighttpd
...

1.4.3Lighttpd port

Lighttpd is configured to use port 80 if the HAProxy and Lighttpd run on different servers. If the HAProxy and Lighttpd run on the same server, the HAProxy and the Lighttpd web server processes are bound to ports 80 and 8001, respectively.

Find Lighttpd run on which port using the sockstat (list open sockets) command.

Terminal ~ 1.4-3
$ sockstat -4 -l | egrep ":80"
...
www      lighttpd   5388 4  tcp4   *:8001                *:*
www      haproxy    1252 5  tcp4   *:80                  *:*
...

1.4.4Lighttpd unresponsive

Find the Lighttpd process ID using the ps (process status) command. The first column displays the process owner username: www and the second column contains the Lighttpd process ID: 5388. Use the kill command with Lighttpd process ID to end the current Lighttpd process. Finally, start the new Lighttpd process.

Terminal ~ 1.4-4
$ ps -aux | grep lighttpd
...
www        5388   0.0  0.1   16424   5612  -  S    17:45      0:00.98 /usr/local/sbin/lighttpd -f /usr/local/etc/lighttpd/lighttpd.conf
freebsd    5912   0.0  0.1   11508   2840  1  S+   03:48      0:00.00 grep lighttpd
...

# Use the above process ID
$ kill -9 5388
$ sudo service lighttpd start
Starting lighttpd.

Affiliate links

Setting up a custom Lighttpd web server requires a VPS with root access. Use our affiliate links to purchase a VPS or cloud server from third-party vendors. The affiliate commissions we earn facilitate, Free website access for everyone.

The affiliate links are listed in alphabetical order without any favor. Users are encouraged to refer to the Global Webdynamics LLP Terms of Service governing the Third-party vendors.