diff --git a/ChangeLog.md b/ChangeLog.md index bee65c7..1228014 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Fixed Quickstart app so that it runs in a Docker container or with a local PHP server ## [v1.0.5] - 2024-10-14 ### Fixed diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 7045925..0000000 --- a/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -ARG PHP_VERSION=8.3 -ARG DEBIAN_VERSION=bookworm -ARG COMPOSER_VERSION=2.7.6 - -FROM composer:${COMPOSER_VERSION} as composer - -FROM php:${PHP_VERSION}-cli-${DEBIAN_VERSION} - -COPY --from=composer /usr/bin/composer /usr/local/bin/composer - -RUN apt-get update && apt-get install -y --no-install-recommends \ - git=1:2.* libzip-dev=1.* unzip=6.0* zip=3.0* \ - && docker-php-ext-install zip \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* diff --git a/Makefile b/Makefile index 537c5a6..437e0da 100644 --- a/Makefile +++ b/Makefile @@ -1,59 +1,37 @@ ARGS_PHPUNIT ?= DOCKER := $(if $(LRN_SDK_NO_DOCKER),,$(shell which docker)) +DOCKER_COMPOSE := docker compose # PHP Evolution SUPPORTED_PHP_VERSIONS = 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3 -PHP_VERSION = $(lastword ${SUPPORTED_PHP_VERSIONS}) +PHP_VERSION ?= $(lastword ${SUPPORTED_PHP_VERSIONS}) DEBIAN_VERSION-7.1 = buster DEBIAN_VERSION-7.2 = buster DEBIAN_VERSION-7.3 = bullseye DEBIAN_VERSION-7.4 = bullseye DEBIAN_VERSION-8.0 = bullseye DEBIAN_VERSION-def = bookworm -DEBIAN_VERSION = $(or $(DEBIAN_VERSION-$(PHP_VERSION)),$(DEBIAN_VERSION-def)) +DEBIAN_VERSION ?= $(or $(DEBIAN_VERSION-$(PHP_VERSION)),$(DEBIAN_VERSION-def)) COMPOSER_VERSION-7.1 = 2.2 COMPOSER_VERSION-def = 2.7.6 -COMPOSER_VERSION = $(or $(COMPOSER_VERSION-$(PHP_VERSION)),$(COMPOSER_VERSION-def)) -LOCALHOST = 0.0.0.0 - -IMAGE = php-cli-composer:$(PHP_VERSION)-$(DEBIAN_VERSION)-$(COMPOSER_VERSION) +COMPOSER_VERSION ?= $(or $(COMPOSER_VERSION-$(PHP_VERSION)),$(COMPOSER_VERSION-def)) TARGETS = all build devbuild prodbuild \ quickstart check-quickstart install-vendor \ dist dist-test dist-zip release \ lint test test-coverage test-integration-env test-unit \ clean clean-dist clean-test clean-vendor + .PHONY: $(TARGETS) .default: all -ifneq (,$(DOCKER)) -TTYFLAGS := $(shell if [ -t 0 ] ; then echo "-it"; else echo "-t"; fi) -# Re-run the make command in a container -DKR = docker container run $(TTYFLAGS) --rm \ - -v $(CURDIR):/srv/sdk/php:z,delegated \ - -v lrn-sdk-php_cache:/root/.composer \ - -w /srv/sdk/php \ - -e LRN_SDK_NO_DOCKER=1 \ - -e ENV -e REGION -e VER \ - -p 8000:8000 \ - $(if $(findstring dev,$(ENV)),--net host) \ - $(IMAGE) - -$(TARGETS): $(if $(shell docker image ls -q --filter reference=$(IMAGE)),,docker-build) - $(DKR) make -e MAKEFLAGS="$(MAKEFLAGS)" $@ - -docker-build: - docker image build \ - --progress plain \ - --build-arg PHP_VERSION=$(PHP_VERSION) \ - --build-arg DEBIAN_VERSION=$(DEBIAN_VERSION) \ - --build-arg COMPOSER_VERSION=$(COMPOSER_VERSION) \ - -t $(IMAGE) . -.PHONY: docker-build lrn-test-all lrn-test-clean - - -else +docker-build: install-vendor + $(DOCKER_COMPOSE) build php nginx + +.PHONY: docker-build + +# Local development targets without Docker DIST_PREFIX = learnosity_sdk- SRC_VERSION := $(shell git describe | sed s/^v//) DIST = $(DIST_PREFIX)$(SRC_VERSION) @@ -64,15 +42,19 @@ COMPOSER_INSTALL_FLAGS = --no-interaction --optimize-autoloader --classmap-autho PHPCS= ./vendor/bin/phpcs PHPUNIT = ./vendor/bin/phpunit -### -# quickstart rules -### -quickstart: VENDOR_FLAGS = --no-dev -quickstart: install-vendor - cd docs/quickstart && php -S $(LOCALHOST):8000 +quickstart: $(if $(DOCKER),docker-build) $(if $(DOCKER),docker,local)-quickstart + +docker-quickstart: VENDOR_FLAGS = --no-dev +docker-quickstart: install-vendor + $(DOCKER_COMPOSE) up -d + +local-quickstart: VENDOR_FLAGS = --no-dev +local-quickstart: install-vendor + php -S localhost:8000 -t docs/quickstart check-quickstart: vendor/autoload.php $(COMPOSER) install $(COMPOSER_INSTALL_FLAGS) --no-dev; + ### # internal tooling rules #### @@ -103,15 +85,11 @@ test-integration-env: build ### # dist rules -# -# build a dist zip file from the distdir, THEN run the tests in the dist dir, -# to avoid polluting the distfile with dev dependencies ### dist: dist-test -# We want to clean first before copying into the .distdir so that we have a clean copy dist-zip: clean-test clean-dist - mkdir -p .$(DIST) # use a hidden directory so that it doesn't get copied into itself + mkdir -p .$(DIST) cp -R * .version .$(DIST) mv .$(DIST) $(DIST) rm -rf $(DIST)/vendor/ @@ -119,7 +97,6 @@ dist-zip: clean-test clean-dist rm -rf $(DIST)/release.sh zip -qr $(DIST).zip $(DIST) -# run tests in the distdir dist-test: dist-zip install-vendor $(PHPUNIT) --do-not-cache-result --no-logging --configuration=$(DIST)/phpunit.xml @@ -132,6 +109,7 @@ composer.lock: composer.json clean: clean-dist clean-test clean-vendor rm -rf $(DIST_PREFIX)*.zip + $(DOCKER_COMPOSE) down -v clean-dist: rm -rf $(DIST_PREFIX)*/ @@ -145,16 +123,7 @@ clean-vendor: rm -rf vendor rm -f composer.lock -# Aliases - -devbuild: build -prodbuild: dist - -# The following are real targets, not phony ones - -vendor: - $(COMPOSER) install $(COMPOSER_INSTALL_FLAGS) - +# Package contents PKG_CONTENTS = .version \ CONTRIBUTING.md LICENSE.md README.md REFERENCE.md ChangeLog.md \ composer.json bootstrap.php phpunit.xml \ @@ -169,4 +138,3 @@ $(DIST)/vendor: $(DIST) $(DIST).zip: $(DIST)/vendor zip -qr $(DIST).zip $(DIST) -endif diff --git a/README.md b/README.md index ea38b53..5250a61 100644 --- a/README.md +++ b/README.md @@ -100,14 +100,44 @@ For production use, you should install the SDK using the Composer package manage ## Quick start guide Let's take a look at a simple example of the SDK in action. In this example, we'll load an assessment into the browser. -### **Start up your web server** -You can start the demo by running the following command in the SDK directory: - - make quickstart - -If your PHP server is up, we'll assume that your web server is available at this local address: - - http://localhost:8000 +### Running the Quickstart Application + +### **Option 1: Running with Docker (Recommended)** +To run the quickstart application using Docker, ensure you have Docker and Docker Compose installed on your system. Then follow these steps: + +1. Clone the repository (if you haven't already): + + ```bash + git clone https://github.com/Learnosity/learnosity-sdk-php.git + cd learnosity-sdk-php + ``` + +2. Start the Docker containers: + + ```bash + make quickstart + ``` + +### **Option 2: Running Locally** +If you prefer to run the application locally without Docker, follow these prerequisites: + +1. Clone the repository (if you haven't already): + + ```bash + git clone https://github.com/Learnosity/learnosity-sdk-php.git + cd learnosity-sdk-php + ``` + +2. Ensure you have PHP 8.x installed +3. Install Composer (https://getcomposer.org/download/) + +4. Start the built-in PHP development server: + + ```bash + make quickstart + ``` + +**Note:** Local setup requires PHP 8 runtime libraries and may have additional system dependencies. The Docker method provides a more consistent and isolated environment. (For more information about the web server configuration, [click here](https://help.learnosity.com/hc/en-us/articles/360000757757-Environment-Setup-Guide)) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..62d0e0e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,43 @@ +version: '3.8' + +networks: + quickstart-network: + driver: bridge + +services: + nginx: + container_name: learnosity-php-sdk-nginx + build: + context: . + dockerfile: docker/nginx/Dockerfile + ports: + - "8000:8000" + volumes: + - .:/var/www/html + environment: + APPLICATION_SERVER_LANGUAGE: php + UPSTREAM_APPLICATION_SERVICE: php:9000 + depends_on: + - php + networks: + quickstart-network: + aliases: + - nginx + + php: + container_name: learnosity-php-sdk-php-frm + build: + context: . + dockerfile: docker/php/Dockerfile + args: + PHP_VERSION: ${PHP_VERSION:-8.3} + DEBIAN_VERSION: ${DEBIAN_VERSION:-bookworm} + COMPOSER_VERSION: ${COMPOSER_VERSION:-2.7.6} + volumes: + - .:/var/www/html + environment: + - SERVER_NAME=localhost + networks: + quickstart-network: + aliases: + - php diff --git a/docker/nginx/Dockerfile b/docker/nginx/Dockerfile new file mode 100644 index 0000000..537f000 --- /dev/null +++ b/docker/nginx/Dockerfile @@ -0,0 +1,11 @@ +FROM nginx:stable-alpine + +# Copy custom nginx configuration +COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf + +WORKDIR /var/www/html + +# Optional: Add any additional Nginx-specific configurations or setup +EXPOSE 8000 + +CMD ["nginx", "-g", "daemon off;"] diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf new file mode 100644 index 0000000..a5ec324 --- /dev/null +++ b/docker/nginx/default.conf @@ -0,0 +1,19 @@ +server { + listen 8000; + server_name localhost; + root /var/www/html/docs/quickstart; + index index.php index.html; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + fastcgi_pass php:9000; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile new file mode 100644 index 0000000..a0ed4da --- /dev/null +++ b/docker/php/Dockerfile @@ -0,0 +1,24 @@ +ARG PHP_VERSION=8.3 +ARG DEBIAN_VERSION=bookworm +ARG COMPOSER_VERSION=2.7.6 + +FROM php:${PHP_VERSION}-fpm-${DEBIAN_VERSION} + +# Install necessary dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + git=1:2.* \ + libzip-dev=1.* \ + unzip=6.0* \ + zip=3.0* \ + && docker-php-ext-install zip \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +# Configure PHP-FPM to listen on TCP/IP +RUN sed -i 's/listen = \/run\/php-fpm.sock/listen = 127.0.0.1:9000/g' /usr/local/etc/php-fpm.d/www.conf + +WORKDIR /var/www/html + +EXPOSE 9000 + +CMD ["php-fpm"]