Skip to content

Commit

Permalink
nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
dzuelke committed Apr 3, 2014
1 parent e42dc1c commit 3497e1b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
2 changes: 2 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ mkdir -p $BUILD_DIR/.heroku/php/etc/php/conf.d
cp "$BP_DIR/conf/php/conf.d/ext-opcache.ini" $BUILD_DIR/.heroku/php/etc/php/conf.d
# Apache; copy in our config
cp $BP_DIR/conf/apache2/httpd.conf.default $BUILD_DIR/.heroku/php/etc/apache2/httpd.conf
# nginx; copy in our config
cp $BP_DIR/conf/nginx/nginx.conf.default $BUILD_DIR/.heroku/php/conf/nginx.conf

# handle extensions
if [ -f composer.json ]; then
Expand Down
42 changes: 35 additions & 7 deletions bin/heroku-php-boot-nginx → bin/heroku-php-nginx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@ set -o pipefail
# fail harder
set -eu

DIR=`pwd`
php_passthrough() {
local dir=`dirname $1`
local file=`basename $1`
local out=`basename $file .php`
if [[ "$out" != "$file" ]]; then
out="$dir/$out"
php $1 > $out
echo $out
else
echo $1
fi
}

# we need this in configs
export DIR=`pwd`
export DOCUMENT_ROOT=$DIR
# set a default port if none is given
export PORT=${PORT:-$(( $RANDOM+1024 ))}
echo "Booting on port $PORT..." >&2

nginx_user_config=""
php="php-fpm --nodaemonize -y $DIR/vendor/heroku/heroku-buildpack-php/conf/php/php-fpm.conf -c $DIR/vendor/heroku/heroku-buildpack-php/conf/php/php.ini"
Expand All @@ -15,17 +32,20 @@ while getopts ":n:f:t" opt; do
case $opt in
n)
if [[ -z $OPTARG ]]; then
echo "Must give additional httpd.conf path (relative to '$DIR')" >&2
echo "Must give additional nginx.conf path (relative to '$DIR')" >&2
exit 1
fi
nginx_user_config="include $DIR/$OPTARG;"
echo "Using additional Apache2 configuration file '$DIR/$OPTARG'" >&2
# FIXME: check if it exists
nginx_user_config=$(php_passthrough "$DIR/$OPTARG")
echo "Using additional Nginx configuration file '$nginx_user_config'" >&2
nginx_user_config="include $nginx_user_config;"
;;
f)
if [[ -z $OPTARG ]]; then
echo "Must give custom php-fpm.conf path (relative to '$DIR')" >&2
exit 1
fi
# FIXME: check if it exists
php="$php -y $DIR/$OPTARG" # can safely repeat to overwrite
echo "Using custom PHP-FPM configuration file '$DIR/$OPTARG'" >&2
;;
Expand All @@ -34,6 +54,7 @@ while getopts ":n:f:t" opt; do
echo "Must give custom php.ini path (relative to '$DIR')" >&2
exit 1
fi
# FIXME: check if it exists
php="$php -c $DIR/$OPTARG" # can safely repeat to overwrite
echo "Using custom PHP INI file '$DIR/$OPTARG'" >&2
;;
Expand All @@ -51,19 +72,26 @@ done
shift $((OPTIND-1))

if [ "$#" == "1" ]; then
# FIXME: check if it exists, strip trailing slashes
DOCUMENT_ROOT=$DIR/$1
echo "DOCUMENT_ROOT changed to '$DOCUMENT_ROOT'" >&2
fi

# make a shared pipe; we'll write the name of the process that exits to it once that happens, and wait for that event below
wait_pipe=`mktemp -t -u "waitpipe-$PORT.XXXXXXXXXX"`
# this particular call works on Linux and Mac OS (will create a literal ".XXXXXX" on Mac, but that doesn't matter).
wait_pipe=`mktemp -t "waitpipe-$PORT.XXXXXX" -u`
rm -rf $wait_pipe
mkfifo $wait_pipe

# start FPM; write "php-fpm" to the shared pipe if it exits
echo "Starting php-fpm..." >&2
( $php; echo "php-fpm" > $wait_pipe )&
# start apache; write "php-fpm" to the shared pipe if it exits
( nginx -g "include $DIR/vendor/heroku/heroku-buildpack-php/conf/nginx/heroku.conf; $nginx_user_config"; echo "nginx" > $wait_pipe )&
# start nginx; write "nginx" to the shared pipe if it exits
echo "Starting nginx..." >&2
nginx_config=$(php_passthrough "$DIR/vendor/heroku/heroku-buildpack-php/conf/nginx/heroku.conf.php")
( nginx -g "include $nginx_config; $nginx_user_config"; echo "nginx" > $wait_pipe )&

# TODO: trap SIGINT/SIGERM/EXIT/QUIT and kill subprocesses

# wait for something to come from the shared pipe
read exitproc < $wait_pipe
Expand Down
27 changes: 27 additions & 0 deletions conf/nginx/heroku.conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
daemon off;

http {
server {
listen <?=getenv('PORT')?:'8080'?>;

root <?=getenv('DOCUMENT_ROOT')?:getenv('DIR')?:getcwd()?>;

location ~ \.php {
try_files $uri 404;

include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:4999;
fastcgi_buffers 256 4k;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location / {
index index.php index.html index.htm;
}
}
}
34 changes: 34 additions & 0 deletions conf/nginx/nginx.conf.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#user nobody;
worker_processes auto;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;
}

0 comments on commit 3497e1b

Please sign in to comment.