Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lighttpd fastcgi #17

Merged
merged 5 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FROM debian:buster-slim
FROM pdok/lighttpd:1.4.67
LABEL maintainer="PDOK dev <pdok@kadaster.nl>"

USER root

# apt-get python3-pip on debian:buster will install python3.7
RUN apt-get -y update \
&& apt-get install -y \
Expand All @@ -16,15 +18,10 @@ RUN apt-get -y update \
libjpeg-dev \
&& rm -rf /var/lib/apt/lists/*

RUN pip3 install Numpy PyYAML boto3 Pillow requests Shapely eventlet gunicorn uwsgi prometheus_client lxml azure-storage-blob
RUN pip3 install flup Numpy PyYAML boto3 Pillow requests Shapely eventlet gunicorn uwsgi prometheus_client lxml azure-storage-blob pyproj==2.2.0
# use the PDOK fork of MapProxy. This is MapProxy version 1.13.1 but patched with https://github.com/mapproxy/mapproxy/pull/608
RUN pip3 install git+https://github.com/PDOK/mapproxy.git@pdok-1.13.2-patched-2

# when overwriting the CMD with a uwsgi command it's good practice to not run it as root
RUN groupadd -g 1337 mapproxy \
&& useradd --shell /bin/bash --gid 1337 -m mapproxy \
&& usermod -a -G sudo mapproxy

RUN apt-get clean

# default dir needed for the cache_data
Expand All @@ -33,6 +30,21 @@ RUN chmod a+rwx /srv/mapproxy/cache_data

WORKDIR /srv/mapproxy

ADD config/lighttpd.conf /srv/mapproxy/config/lighttpd.conf
ADD config/include.conf /srv/mapproxy/config/include.conf
ADD config/log.ini /srv/mapproxy/config/log.ini

ADD config/start.py /srv/mapproxy/start.py
RUN chmod +x start.py

USER www

ENV DEBUG 0
ENV MIN_PROCS 4
ENV MAX_PROCS 8
ENV MAX_LOAD_PER_PROC 1
ENV IDLE_TIMEOUT 20

EXPOSE 80

CMD gunicorn -k gthread --user=1337 --group=1337 --chdir /srv/mapproxy/config --threads=16 --workers=1 -b :80 config:application --no-sendfile --access-logfile '-' --error-logfile '-'
CMD ["lighttpd", "-D", "-f", "/srv/mapproxy/config/lighttpd.conf"]
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@

```docker
docker build -t pdok/mapproxy .
docker run --rm -d -p 80:80 --name mapproxy-example -v `pwd`/examples/config:/srv/mapproxy/config pdok/mapproxy
docker run --rm -d -p 8080:80 --name mapproxy-example -v `pwd`/examples/config/mapproxy.yaml:/srv/mapproxy/config/mapproxy.yaml pdok/mapproxy

docker stop mapproxy-example
```

## Introduction

This project aims to fulfill the need for creating a [Docker](https://www.docker.com) base image that can be used in a scalable infrastructure like [Kubernetes](https://kubernetes.io/) and still is easy to use with a single docker run command. That's why we created this docker images containing in which we don't COPY config during the docker build and include "pip install .. gunicorn uwsgi .."
This project aims to fulfill the need for creating a
[Docker](https://www.docker.com) base image that can be used in a scalable
infrastructure like [Kubernetes](https://kubernetes.io/) and still is easy to
use with a single docker run command. That's why we created this docker images
containing in which we don't COPY config during the docker build.

## What will it do

In it simplist form it will create an [Mapproxy](https://mapproxy.org/) application that is easy to use. The only thing required is to add you own mapproxy.yaml configuration. For more complex deployments like docker-compose and/or kubernetes it will provide a starting point in creating multi-container/pods deployments.
In it simplist form it will create an [Mapproxy](https://mapproxy.org/)
application that is easy to use. The only thing required is to add you own
mapproxy.yaml configuration. For more complex deployments like docker-compose
and/or kubernetes it will provide a starting point in creating
multi-container/pods deployments.

## Usage

Expand All @@ -31,13 +39,37 @@ docker build -t pdok/mapproxy .

### Run

This image can be run straight from the commandline. A volumn needs to be mounted on the container directory /srv/mapproxy/config.
This image can be run straight from the commandline. The mapproxy config file
needs to be mounted on the container path `/srv/mapproxy/config/mapproxy.yaml`.

```docker
docker run -d -p 80:80 --name mapproxy-example -v `pwd`/examples/config:/srv/mapproxy/config pdok/mapproxy
docker run -d -p 80:80 --name mapproxy-example -v `pwd`/examples/config/mapproxy.yaml:/srv/mapproxy/config/mapproxy.yaml pdok/mapproxy
```

Running the example above will start a empty mapproxy. On the url <http://localhost/demo> the test page can be accessed. Replacing the example mapproxy.yaml with your own will start a mapproxy with that configuration.
Running the example above will start a empty mapproxy. On the url
<http://localhost:8080/mapproxy/demo> the test page can be accessed. Replacing the example
mapproxy.yaml with your own will start a mapproxy with that configuration.

This docker image uses lighttpd as a proxy. It's possible to add additional
lighttpd config by mounting a `include.conf` file on the container path
`/srv/mapproxy/config/include.conf`. It's also possible to provide your
own complete lighttpd config by mounting a `lighttpd.conf` file on the
container path `/srv/mapproxy/config/lighttpd.conf`.

## Environment variables

**DEBUG** Set the log level for both lighttpd and mapproxy to debug. This
results is verbose logging which includes in request logging (Default: 0)

**MIN_PROCS** Sets the minimum fastcgi processes to start (Default: 4)

**MAX_PROCS** Upper limit of fastcgi processes to start (Default: 8)

**MAX_LOAD_PER_PROC** Maximum number of waiting processes on average per
process before a new process is spawned (Default: 1)

**IDLE_TIMEOUT** Number of seconds before a unused process gets
terminated (Default: 20)

## Docker-compose

Expand Down
1 change: 1 addition & 0 deletions config/include.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Overwrite with extra lighttpd config
38 changes: 38 additions & 0 deletions config/lighttpd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
server.modules += ( "mod_setenv" )
server.modules += ( "mod_fastcgi" )
server.modules += ( "mod_rewrite" )
server.modules += ( "mod_magnet" )

server.document-root = "/var/www/"
server.port = 80
server.tag = ""

server.username = "www"
server.groupname = "www"

server.errorlog = "/dev/stderr"

mimetype.assign = (
".xml" => "application/xml",
".png" => "image/png",
".jpeg" => "image/jpeg",
".jpg" => "image/jpeg"
)

include "include.conf"

fastcgi.debug = env.DEBUG

fastcgi.server = (
"/mapproxy" => (
"mapproxy" => (
"socket" => "/tmp/mapproxy-fastcgi.socket",
"bin-path" => "/srv/mapproxy/start.py",
"check-local" => "false",
"min-procs" => env.MIN_PROCS,
"max-procs" => env.MAX_PROCS,
"max-load-per-proc" => env.MAX_LOAD_PER_PROC,
"idle-timeout" => env.IDLE_TIMEOUT
)
)
)
4 changes: 2 additions & 2 deletions examples/config/log.ini → config/log.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ keys=mapproxy,source_requests
keys=default,requests

[logger_root]
level=INFO
level=ERROR
handlers=mapproxy

[logger_source_requests]
level=INFO
level=ERROR
qualname=mapproxy.source.request
propagate=0
handlers=source_requests
Expand Down
17 changes: 17 additions & 0 deletions config/start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3

if __name__ == '__main__':
from os import path,environ
from mapproxy.wsgiapp import make_wsgi_app
from flup.server.fcgi import WSGIServer
from logging.config import fileConfig
import logging

logging.config.fileConfig(r'/srv/mapproxy/config/log.ini', {'here': path.dirname(__file__)})

if environ.get('DEBUG') == '1':
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('mapproxy.source.request').setLevel(logging.DEBUG)

application = make_wsgi_app(r'/srv/mapproxy/config/mapproxy.yaml')
WSGIServer(application).run()
13 changes: 0 additions & 13 deletions examples/config/config.py

This file was deleted.

16 changes: 16 additions & 0 deletions examples/config/include.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
server.modules += ( "mod_auth" )
server.modules += ( "mod_status" )

status.status-url = "/server-status"
auth.backend = "plain"
auth.backend.plain.userfile = "/srv/mapproxy/config/lighttpd.auth"

$HTTP["url"] =~ "^/server-status" {
auth.require = ( "" =>
(
"method" => "basic",
"realm" => "Protected Monitoring Area",
"require" => "user=admin"
)
)
}
43 changes: 0 additions & 43 deletions examples/config/lighttpd.conf

This file was deleted.

12 changes: 0 additions & 12 deletions examples/config/uwsgi.ini

This file was deleted.

6 changes: 3 additions & 3 deletions examples/docker-compose/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# docker-compose

Docker-compose example running mapproxy demo as a uwsgi application with lighttpd as webserver exposing a prometheus metrics end-point.
Docker-compose example running mapproxy demo exposing a prometheus metrics end-point.

## TL;DR

Expand All @@ -11,6 +11,6 @@ docker-compose down

## endpoints

- <http://localhost/mapproxy/demo/>
- <http://localhost/server-status>
- <http://localhost:8080/mapproxy/demo/>
- <http://localhost:8080/server-status>
- <http://localhost:9117/metrics>
47 changes: 10 additions & 37 deletions examples/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,24 @@ version: '2.1'
services:
mapproxy:
image: pdok/mapproxy
restart: unless-stopped
command: ["bash", "-c","uwsgi --ini /srv/mapproxy/config/uwsgi.ini"]
environment:
- UWSGI_PROCESSES=1
- UWSGI_THREADS=16
- UWSGI_CHDIR=/srv/mapproxy/config
- UWSGI_PYARGV=/srv/mapproxy/config/mapproxy.yaml
- UWSGI_WSGI_FILE=/srv/mapproxy/config/config.py
labels:
nl.pdok.application.group: "mapproxy"
SERVICE_CHECK_HTTP: "/"
SERVICE_NAME: demo-mapproxy
volumes:
- ./../config:/srv/mapproxy/config
- demo:/tmp/
networks:
mapproxy-exporter:
lighttpd:
image: pdok/lighttpd
restart: unless-stopped
command: ["bash", "-c","lighttpd -D -f /srv/lighttpd/lighttpd.conf"]
restart: unless-stopped
ports:
- 80:80
labels:
nl.pdok.application.group: "lighttpd"
SERVICE_CHECK_HTTP: "/"
SERVICE_NAME: demo-lighttpd
- 8080:80
environment:
- DEBUG=1
volumes:
- ./../config:/srv/lighttpd
- demo:/tmp/
- ./../config/mapproxy.yaml:/srv/mapproxy/config/mapproxy.yaml
- ./../config/include.conf:/srv/mapproxy/config/include.conf
- ./../config/lighttpd.auth:/srv/mapproxy/config/lighttpd.auth
networks:
mapproxy-exporter:
exporter:
image: lusotycoon/apache-exporter:v0.7.0
restart: unless-stopped
command: ["-scrape_uri=http://admin:admin@lighttpd/server-status?auto"]
command: ["-scrape_uri=http://admin:admin@mapproxy/server-status?auto"]
ports:
- 9117:9117
labels:
nl.pdok.application.group: "exporter"
SERVICE_CHECK_HTTP: "/"
SERVICE_NAME: demo-exporter
networks:
mapproxy-exporter:
volumes:
demo:
mapproxy-exporter:
networks:
mapproxy-exporter:
mapproxy-exporter:
2 changes: 1 addition & 1 deletion examples/k8s/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# k8s

K8s example running mapproxy demo as a uwsgi application with lighttpd as webserver exposing a prometheus metrics end-point.
K8s example running mapproxy demo exposing a prometheus metrics end-point.

## TL;DR

Expand Down
Loading