Skip to content

Instantly share code, notes, and snippets.

@minimum2scp
Last active October 4, 2016 02:26
Show Gist options
  • Save minimum2scp/b8f964aef8fdcff928296ca9849406b3 to your computer and use it in GitHub Desktop.
Save minimum2scp/b8f964aef8fdcff928296ca9849406b3 to your computer and use it in GitHub Desktop.
nginx mainline のsource debをベースに、nginx-module-njsを参考にしてngx_mrubyをDSOなサブパッケージnginx-modules-mrubyとしてビルドしてみる
--- a/debian/control 2016-05-31 23:38:38.000000000 +0900
+++ b/debian/control 2016-06-10 02:59:02.587870054 +0900
@@ -64,3 +64,10 @@
Depends: ${misc:Depends}, ${shlibs:Depends}, nginx (= ${source:Version})
Description: nJScript module
This package contains dynamic nJScript module for nginx.
+
+Package: nginx-module-mruby
+Architecture: any
+Priority: extra
+Depends: ${misc:Depends}, ${shlibs:Depends}, nginx (= ${source:Version})
+Description: mruby module
+ This package contains dynamic mruby module for nginx.
FROM minimum2scp/baseimage-jessie:latest
ARG NGINX_VERSION=1.11.4
ARG NGX_MRUBY_VERSION=v1.18.6
ARG DEB_BASE_REVISION=1~jessie
ARG DEB_NGINX_MODULE_MRUBY_VERSION=${NGINX_VERSION}-${NGX_MRUBY_VERSION}
RUN sed -i -e 's/httpredir.debian.org/ftp.jp.debian.org/' /etc/apt/sources.list
RUN curl http://nginx.org/keys/nginx_signing.key | apt-key add -
RUN echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" > /etc/apt/sources.list.d/nginx.list
RUN echo "deb-src http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list.d/nginx.list
RUN apt-get update
RUN apt-get install -y --no-install-recommends fakeroot devscripts git patch ruby rake bison
RUN apt-get build-dep -y nginx=${NGINX_VERSION}-${DEB_BASE_REVISION}
USER debian
RUN mkdir /home/debian/build
WORKDIR /home/debian/build
RUN apt-get source nginx=${NGINX_VERSION}-${DEB_BASE_REVISION}
WORKDIR /home/debian/build/nginx-${NGINX_VERSION}
RUN git clone https://github.com/matsumoto-r/ngx_mruby.git debian/extra/ngx_mruby/
RUN cd debian/extra/ngx_mruby && git checkout ${NGX_MRUBY_VERSION}
ADD rules.diff /tmp/rules.diff
ADD control.diff /tmp/control.diff
ADD nginx-module-mruby.install /home/debian/build/nginx-${NGINX_VERSION}/debian/nginx-module-mruby.install
ADD nginx-module-mruby.preinst /home/debian/build/nginx-${NGINX_VERSION}/debian/nginx-module-mruby.preinst
RUN echo "${DEB_NGINX_MODULE_MRUBY_VERSION}" > /home/debian/build/nginx-${NGINX_VERSION}/debian/nginx-module-mruby.version
RUN patch -p1 < /tmp/rules.diff
RUN patch -p1 < /tmp/control.diff
RUN debchange --local '+local' 'Added mruby dynamic modules subpackage'
RUN debuild -rfakeroot -uc -us
USER root
WORKDIR /home/debian/build
RUN dpkg -i nginx_${NGINX_VERSION}-${DEB_BASE_REVISION}+local1_amd64.deb nginx-module-mruby_${DEB_NGINX_MODULE_MRUBY_VERSION}~`lsb_release -cs`_amd64.deb
RUN apt-get install -f -y --no-install-recommends
ADD nginx-conf.diff /tmp/nginx-conf.diff
RUN cd /etc; patch -p1 < /tmp/nginx-conf.diff && rm /tmp/nginx-conf.diff
WORKDIR /

Build command

docker build -t minimum2scp/ngx_mruby:dev .

Binary package version/revision

override_dh_gencontrol in debian/rule defines sub package version/revision:

override_dh_gencontrol:
        for p in $(PKGS); do \
                if [ -e debian/$$p.version ]; then \
                        dpkg-gencontrol -p$$p -ldebian/changelog -Tdebian/$$p.substvars -Pdebian/$$p -v`cat debian/$$p.version`~`lsb_release -cs`; \
                else \
                        dpkg-gencontrol -p$$p -ldebian/changelog -Tdebian/$$p.substvars -Pdebian/$$p ; \
                fi ; \
        done

So, nginx and nginx-debug, nginx-module-xxx (except for nginx-module-njs, nginx-module-mruby) will be

${NGINX_VERSION}-${DEB_BASE_REVISION}+local1

But nginx-module-njs and nginx-module-mruby will be

`cat debian/$$p.version`~`lsb_release -cs`

Therefore nginx-module-mruby will be

${NGINX_VERSION}-${NGX_MRUBY_VERSION}~jessie
diff --git a/nginx/conf.d/default.conf b/nginx/conf.d/default.conf
index 34aeb9a..de11a1e 100644
--- a/nginx/conf.d/default.conf
+++ b/nginx/conf.d/default.conf
@@ -10,6 +10,17 @@ server {
index index.html index.htm;
}
+ location /mruby {
+ mruby_content_handler_code '
+ if server_name == "NGINX"
+ Server = Nginx
+ elsif server_name == "Apache"
+ Server = Apache
+ end
+ Server::rputs "Hello #{Server::module_name}/#{Server::module_version} world!"
+ ';
+ }
+
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
diff --git a/nginx/nginx.conf b/nginx/nginx.conf
index e4bad8d..ab35916 100644
--- a/nginx/nginx.conf
+++ b/nginx/nginx.conf
@@ -5,6 +5,7 @@ worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
+load_module /usr/lib/nginx/modules/ngx_http_mruby_module.so;
events {
worker_connections 1024;
objs/ngx_http_mruby_module.so usr/lib/nginx/modules
#!/bin/sh
case "$1" in
install)
cat <<BANNER
----------------------------------------------------------------------
The mruby dynamic module for nginx has been installed.
To enable this module, add the following to /etc/nginx/nginx.conf
and reload nginx:
load_module modules/ngx_http_mruby_module.so;
Please refer to the module documentation for further details:
https://github.com/matsumoto-r/ngx_mruby/
----------------------------------------------------------------------
BANNER
;;
upgrade|abort-upgrade)
;;
*)
echo "preinst called with unknown argument \`$1'" >&2
exit 0
;;
esac
#DEBHELPER#
exit 0
diff --git a/debian/rules b/debian/rules
index 8cbec54..a0f9ffb 100755
--- a/debian/rules
+++ b/debian/rules
@@ -1,6 +1,6 @@
#!/usr/bin/make -f
-#export DH_VERBOSE=1
+export DH_VERBOSE=1
CFLAGS ?= $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS ?= $(shell dpkg-buildflags --get LDFLAGS)
WITH_HTTP2 := $(shell printf \
@@ -9,7 +9,7 @@ WITH_HTTP2 := $(shell printf \
echo "--with-http_v2_module")
PKGS = nginx nginx-dbg \
nginx-module-xslt nginx-module-geoip nginx-module-image-filter \
- nginx-module-perl nginx-module-njs
+ nginx-module-perl nginx-module-njs nginx-module-mruby
COMMON_CONFIGURE_ARGS := \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
@@ -44,6 +44,8 @@ COMMON_CONFIGURE_ARGS := \
--with-http_geoip_module=dynamic \
--with-http_perl_module=dynamic \
--add-dynamic-module=debian/extra/njs-0.1.2/nginx \
+ --add-dynamic-module=debian/extra/ngx_mruby \
+ --add-module=debian/extra/ngx_mruby/dependence/ngx_devel_kit \
--with-threads \
--with-stream \
--with-stream_ssl_module \
@@ -61,7 +63,10 @@ COMMON_CONFIGURE_ARGS := \
%:
dh $@
-override_dh_auto_configure: configure_debug
+override_dh_auto_configure: configure_debug configure_mruby
+
+configure_mruby:
+ (cd debian/extra/ngx_mruby && ./configure --with-ngx-src-root=../../../ && make build_mruby_with_fpic && make generate_gems_config_dynamic)
override_dh_strip:
dh_strip --dbg-package=nginx-dbg
@@ -130,4 +135,5 @@ override_dh_gencontrol:
override_dh_clean:
dh_clean
+ - (cd debian/extra/ngx_mruby && make clobber )
rm -f debian/*init.d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment