Bootstrap: library
From: ubuntu:16.04

%help
Nginx 1.10.3 web server in a Ubuntu 16.04 container.

for more information,
visit: https://github.com/sylabs/examples/tree/master/http-server/nginx-php-web-server

Usage:

  setup:
  $ mkdir -p nginx/php
  $ touch nginx/index.php  # add your php script here.

  instance start:
  $ sudo singularity instance start -B nginx/:/srv/nginx/ -B nginx/php/:/run/php/ nginx.sif nginx

  or to shell:
  $ sudo singularity shell -B nginx/:/srv/nginx/ -B nginx/php/:/run/php/ nginx.sif

  you may need to run this, after shelling into the container, but before starting nginx.
  > fuser -k 80/tcp

%startscript
fuser -k 80/tcp
nginx -t
/etc/init.d/php7.0-fpm restart
/etc/init.d/nginx restart

%runscript
cat << EOF
Nginx 1.10.3 web server in a Ubuntu 16.04 container."

for more information,"
visit: https://github.com/sylabs/examples/tree/master/http-server/nginx-php-web-server"

Usage:"

  setup:"
  $ mkdir -p nginx/php"
  $ touch nginx/index.php  # add your php script here."

  instance start:"
  $ sudo singularity instance start -B nginx/:/srv/nginx/ -B nginx/php/:/run/php/ nginx.sif nginx"

  or to shell:"
  $ sudo singularity shell -B nginx/:/srv/nginx/ -B nginx/php/:/run/php/ nginx.sif"

  you may need to run this, after shelling into the container, but before starting nginx."
  > fuser -k 80/tcp"

EOF

%post
echo "=> installing: nginx, and php..."
apt-get -y update
apt-get -y install nginx
apt-get -y install php-fpm php-mysql

echo "=> creating nginx directory: /srv/nginx/"
mkdir /srv/nginx
mkdir /srv/nginx/php

mkdir /run/php
touch /run/php/php7.0-fpm.sock

echo "=> creating bind files..."
touch /srv/nginx/error.log
touch /srv/nginx/access.log
touch /srv/nginx/index.php
touch /srv/nginx/php7.0-fpm.log
touch /srv/nginx/php7.0-fpm.log
touch /srv/nginx/nginx.pid

echo "=> binding files..."
rm -f /var/log/nginx/error.log
ln -s /srv/nginx/error.log /var/log/nginx/error.log

rm -f /var/log/nginx/access.log
ln -s /srv/nginx/access.log /var/log/nginx/access.log

rm -f /var/www/html/index.php
ln -s /srv/nginx/index.php /var/www/html/index.php

rm -f /var/log/php7.0-fpm.log
ln -s /srv/nginx/php7.0-fpm.log /var/log/php7.0-fpm.log

rm -f /run/nginx.pid
ln -s /srv/nginx/nginx.pid /run/nginx.pid

echo "=> prepare nginx..."
mkdir /var/lib/nginx/body
mkdir /srv/nginx/body
ln -s /var/lib/nginx/body /srv/nginx/body

echo "=> preparing nginx config file..."
cat << EOF > /etc/nginx/sites-available/default 
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html/;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
        	fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    	}

	    location ~ /\.ht {
    	    deny all;
    	}

}

EOF

echo "=> testing nginx..."
nginx -t

echo "=> preparing php config file..."
cat << EOF > /etc/php/7.0/fpm/php-fpm.conf 
[global]

pid = /srv/nginx/php7.0-fpm.pid

error_log = /srv/nginx/php7.0-fpm.log

include=/etc/php/7.0/fpm/pool.d/*.conf

EOF

echo "=> done."
