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

Support running behind nginx and Apache (PHP-FPM and mod_php) #3

Merged
merged 4 commits into from
Feb 5, 2021
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
90 changes: 87 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ on:

jobs:
PHPUnit:
runs-on: ubuntu-latest
name: PHPUnit (PHP ${{ matrix.php }})
runs-on: ubuntu-20.04
strategy:
matrix:
php:
Expand All @@ -17,8 +18,7 @@ jobs:
- 7.1
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
Expand All @@ -27,4 +27,88 @@ jobs:
if: ${{ matrix.php >= 7.3 }}
- run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy
if: ${{ matrix.php < 7.3 }}

Built-in-webserver:
name: Built-in webserver (PHP ${{ matrix.php }})
runs-on: ubuntu-20.04
strategy:
matrix:
php:
- 8.0
- 7.4
- 7.3
- 7.2
- 7.1
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- run: composer install --no-dev
- run: php examples/index.php &
- run: bash tests/await.sh
- run: bash tests/acceptance.sh

nginx-webserver:
name: nginx + PHP-FPM (PHP ${{ matrix.php }})
runs-on: ubuntu-latest
strategy:
matrix:
php:
- 8.0
- 7.4
- 7.3
- 7.2
- 7.1
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- run: composer install --no-dev
- run: docker run -d -v "$PWD":/home/frugal php:${{ matrix.php }}-fpm
- run: docker run -d -p 80:80 --link $(docker ps -qn1):php -v "$PWD":/home/frugal -v "$PWD"/examples/nginx/nginx.conf:/etc/nginx/conf.d/default.conf nginx:stable-alpine
- run: bash tests/await.sh http://localhost
- run: bash tests/acceptance.sh http://localhost

Apache-webserver:
name: Apache webserver (PHP ${{ matrix.php }})
runs-on: ubuntu-latest
strategy:
matrix:
php:
- 8.0
- 7.4
- 7.3
- 7.2
- 7.1
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- run: composer install --no-dev
- run: docker run -d -p 80:80 -v "$PWD":/home/frugal php:${{ matrix.php }}-apache sh -c "rmdir /var/www/html;ln -s /home/frugal/examples/apache /var/www/html;ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled; apache2-foreground"
- run: bash tests/await.sh http://localhost
- run: bash tests/acceptance.sh http://localhost

PHP-webserver:
name: PHP webserver (PHP ${{ matrix.php }})
runs-on: ubuntu-20.04
strategy:
matrix:
php:
- 8.0
- 7.4
- 7.3
- 7.2
- 7.1
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- run: composer install --no-dev
- run: php -S localhost:8080 examples/index.php &
- run: bash tests/await.sh
- run: bash tests/acceptance.sh
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ chapter, […]
Runs everywhere:

* Built-in webserver
* Apache & nginx
* Standalone
* nginx with PHP-FPM
* Apache with mod_fcgid and PHP-FPM
* Apache with mod_php
* PHP's development webserver

[…]

Expand Down Expand Up @@ -325,10 +327,26 @@ Runs everywhere:

## Tests

You can run some simple acceptance tests to verify the frameworks works
as expected by running:
To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](https://getcomposer.org/):

```bash
$ composer install
```

To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
```

Additionally, you can run some simple acceptance tests to verify the framework
examples work as expected behind your web server. Use your web server of choice
(see [deployment](#deployment)) and execute the tests with the URL to your
installation like this:

```bash
$ php examples/index.php
$ tests/acceptance.sh http://localhost:8080
```

Expand Down
5 changes: 5 additions & 0 deletions examples/apache/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
3 changes: 3 additions & 0 deletions examples/apache/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require __DIR__ . '/../index.php';
15 changes: 15 additions & 0 deletions examples/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
server {
root /home/frugal/examples/nginx/public;
index index.php index.html;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
3 changes: 3 additions & 0 deletions examples/nginx/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require __DIR__ . '/../../index.php';
7 changes: 6 additions & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ private function handleRequest(ServerRequestInterface $request, Dispatcher $disp

private function logRequestResponse(ServerRequestInterface $request, ResponseInterface $response): void
{
// only log for built-in webserver and PHP development webserver, others have their own access log
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'cli-server') {
return; // @codeCoverageIgnore
}

$this->log(
($request->getServerParams()['REMOTE_ADDR'] ?? '-') . ' ' .
'"' . $request->getMethod() . ' ' . $request->getUri()->getPath() . ' HTTP/' . $request->getProtocolVersion() . '" ' .
Expand All @@ -331,7 +336,7 @@ private function log(string $message): void
if (\PHP_SAPI === 'cli') {
echo $log;
} else {
fwrite(fopen('/dev/stderr', 'a'), $log);
fwrite(defined('STDERR') ? STDERR : fopen('php://stderr', 'a'), $log);
}
}

Expand Down
34 changes: 8 additions & 26 deletions tests/acceptance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,13 @@ match() {
(echo ""; echo "Error in test $n: Unable to \"grep $@\" this output:"; echo "$out"; exit 1) || exit 1
}

killall php 2>/dev/null
php examples/index.php >/dev/null &
sleep 0.2
out=$(curl -v $base/ 2>&1); match "HTTP/.* 200"
out=$(curl -v $base/test 2>&1); match -i "Location: /"
out=$(curl -v $base/invalid 2>&1); match "HTTP/.* 404"
out=$(curl -v $base/ 2>&1 -X POST); match "HTTP/.* 405"
out=$(curl -v $base/users/foo 2>&1); match "HTTP/.* 200" && match "Hello foo!"
out=$(curl -v $base/users 2>&1); match "HTTP/.* 404"
out=$(curl -v $base/users/ 2>&1); match "HTTP/.* 404"
out=$(curl -v $base/users/a/b 2>&1); match "HTTP/.* 404"

out=$(curl -v $base/ 2>&1) && match "HTTP/.* 200"
out=$(curl -v $base/test 2>&1) && match -i "Location: /"
out=$(curl -v $base/invalid 2>&1) && match "HTTP/.* 404"
out=$(curl -v $base/ 2>&1 -X POST) && match "HTTP/.* 405"
out=$(curl -v $base/users/foo 2>&1) && match "HTTP/.* 200" && match "Hello foo!"
out=$(curl -v $base/users 2>&1) && match "HTTP/.* 404"
out=$(curl -v $base/users/ 2>&1) && match "HTTP/.* 404"
out=$(curl -v $base/users/a/b 2>&1) && match "HTTP/.* 404"

killall php
php -S localhost:8080 examples/index.php >/dev/null 2>&1 &
sleep 0.2

out=$(curl -v $base/ 2>&1) && match "HTTP/.* 200"
out=$(curl -v $base/test 2>&1) && match -i "Location: /"
out=$(curl -v $base/invalid 2>&1) && match "HTTP/.* 404"
out=$(curl -v $base/ 2>&1 -X POST) && match "HTTP/.* 405"
out=$(curl -v $base/users/foo 2>&1) && match "HTTP/.* 200" && match "Hello foo!"
out=$(curl -v $base/users 2>&1) && match "HTTP/.* 404"
out=$(curl -v $base/users/ 2>&1) && match "HTTP/.* 404"
out=$(curl -v $base/users/a/b 2>&1) && match "HTTP/.* 404"

killall php
echo "OK ($n)"
13 changes: 13 additions & 0 deletions tests/await.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

base=${1:-http://localhost:8080}

for i in {1..10}
do
out=$(curl -v -X PROBE $base/ 2>&1) && exit 0 || echo -n .
sleep 0.1
done

echo
echo "$out"
exit 1