Project

General

Profile

[Solved] How to properly configure lighttpd in Ubuntu?

Added by Sourabh1107 about 3 years ago

Hello Al,

Recently I started working with "lighttpd" webserver to host local webserver to place some files. I am using Ubuntu 20.04 machine within the docker container. I am following this guide [[https://wiki.archlinux.org/title/lighttpd]] . I have installed lighttpd package using apt install lighttpd . The lighttpd.conf looks like:

server.modules = (
        "mod_indexfile",
        "mod_access",
        "mod_alias",
        "mod_redirect",
)

server.document-root        = "/var/www/html" 
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log" 
server.pid-file             = "/run/lighttpd.pid" 
server.username             = "www-data" 
server.groupname            = "www-data" 
server.port                 = 81
server.bind                 = "localhost" 
server.tag                  = "lighttpd" 

# strict parsing and normalization of URL for consistency and security
# https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_http-parseoptsDetails
# (might need to explicitly set "url-path-2f-decode" = "disable" 
#  if a specific application is encoding URLs inside url-path)
server.http-parseopts = (
  "header-strict"           => "enable",# default
  "host-strict"             => "enable",# default
  "host-normalize"          => "enable",# default
  "url-normalize-unreserved"=> "enable",# recommended highly
  "url-normalize-required"  => "enable",# recommended
  "url-ctrls-reject"        => "enable",# recommended
  "url-path-2f-decode"      => "enable",# recommended highly (unless breaks app)
 #"url-path-2f-reject"      => "enable",
  "url-path-dotseg-remove"  => "enable",# recommended highly (unless breaks app)
 #"url-path-dotseg-reject"  => "enable",
 #"url-query-20-plus"       => "enable",# consistency in query string
)

index-file.names            = ( "index.php", "index.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/" 
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.conf.pl" 
include "/etc/lighttpd/conf-enabled/*.conf" 

#server.compat-module-load   = "disable" 
server.modules += (
        "mod_compress",
        "mod_dirlisting",
        "mod_staticfile",
)

Also I have a simple HTML file in /var/www/html/ to display Hello World!

But when I start lighttpd using /etc/init.d/lighttpd start I get:

(network.c.311) can't bind to socket: 127.0.0.1:81 Address already in use

And when I open browser (ex, chrome) and launch localhost:81 , it shows This site can’t be reached. localhost refused to connect.

I tried with /etc/init.d/lighttpd stop and started again but it results in the same error.

When I do netstat -ntulp :

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:81            0.0.0.0:*               LISTEN      -

I expected a PID should have been assigned to lighttpd

Can anyone please let me know what is the issue here and how can it be resolved?

Thanks in advance.

P.S: Please let me know if any info is missing here.


Replies (8)

RE: How to properly configure lighttpd in Ubuntu? - Added by gstrauss about 3 years ago

P.S: Please let me know if any info is missing here.

If you multi-post, you should link them in both places, i.e. https://stackoverflow.com/questions/71749557/how-to-properly-configure-lighttpd-in-ubuntu/71752846

The output you shared suggests that some process is still running and listening on port 81.

As root run netstat -ntulp to find the pid and have the process name listed. You can also list the processes running as user www-data with ps -u www-data. You probably want to kill the pid to whatever process is listening on 81.

RE: [Solved] How to properly configure lighttpd in Ubuntu? - Added by Sourabh1107 about 3 years ago

Hello gstrauss, thanks for the response. I am the "root" user and no other users are created. There was a running process on 81 and I killed it. Now doing /etc/init.d/lighttpd start shows * Starting web server lighttpd . But when I launch 127.0.0.1:81 from the browser, it shows This site can’t be reached. localhost refused to connect.

RE: [Solved] How to properly configure lighttpd in Ubuntu? - Added by gstrauss about 3 years ago

If your browser is not on the same machine as the server, then 127.0.0.1 is not what you think it is.

RE: [Solved] How to properly configure lighttpd in Ubuntu? - Added by Sourabh1107 about 3 years ago

Yes my browser is also on the same machine as where I am running lighttpd. I am running lighttpd from the docker container.

RE: [Solved] How to properly configure lighttpd in Ubuntu? - Added by Sourabh1107 about 3 years ago

Sourabh1107 wrote in RE: [Solved] How to properly configure lighttpd in Ubuntu?:

Yes my browser is also on the same machine as where I am running lighttpd. I am running lighttpd from the docker container.

I also tried to assign my IP address to server.bind But is results in (network.c.311) can't bind to socket: 192.168.x.xxx:81 Cannot assign requested address

RE: [Solved] How to properly configure lighttpd in Ubuntu? - Added by gstrauss about 3 years ago

Your problem is likely an issue with your understanding of containers and networking. lighttpd works just fine inside a container. However, lighttpd does not configure the container -- you do -- and lighttpd running inside a container is limited to how you configured the container.

This is not the site to teach you how to configure containers so you will not get further answers here on how to configure your container.

RE: [Solved] How to properly configure lighttpd in Ubuntu? - Added by Sourabh1107 about 3 years ago

Hello @gstrauss, thanks for the clarification. After properly setting up docker container to allow port forwarding I am able to use lighttpd. And also I can access files from localhost.

Now I am trying to change from "HTTP" to "HTTPS". I modified lighttpd.conf to include:

$HTTP["scheme"] == "http" {
    $HTTP["host"] =~ "(.*)(:[0-9]+|)$" {  ## %1 contains hostname without port
        url.redirect = (".*" => "https://%1$0")
    }
}

But this results in

Unable to connect.

I also tried with [[https://redmine.lighttpd.net/projects/1/wiki/HowToRedirectHttpToHttps#redirect-everything-from-a-non-standard-port-ie-not-80-from-http-to-https]]. But I have same response.

Can you please let me know what could be the issue here?

Thanks in advance.

RE: [Solved] How to properly configure lighttpd in Ubuntu? - Added by gstrauss about 3 years ago

Can you please let me know what could be the issue here?

Same as before. As I said above: "Your problem is likely an issue with your understanding of containers and networking."

You have configured lighttpd to send a redirect, and your browser is unable to connect to that target and port.

    (1-8/8)