303

Nginx Unit with Docker, PHP, and Laravel

 6 years ago
source link: https://bitpress.io/nginx-unit-laravel-demo/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx Unit with Docker, PHP, and Laravel

Sep 22, 2017 • Paul Redmond

As part of the announced Nginx Platform, Nginx is working on a dynamic web application server, Nginx Unit. Unit supports fully dynamic reconfiguration using a RESTful JSON API, supports multiple application languages and versions can run simultaneously. Right now Unit supports Python, PHP, and Go; with planned support for JavaScript/Node.js, Java, and Ruby.

NGINX Unit is a dynamic web application server, designed to run applications in multiple languages. Unit is lightweight, polyglot, and dynamically configured via API. The design of the server allows reconfiguration of specific application parameters as needed by the engineering or operations.

NGINX Unit is currently available as a beta. As such, it is suitable for use in a testing environment but is not recommended for use in production.

Unit is beta right now, so I decided to start experimenting with it for PHP applications. You can check out my Nginx Unit Demo repository, which runs a Laravel application with Nginx Unit in Docker.

While researching, I decided to use the ubuntu:xenial image to make things easier instead of building from source. Here’s the Dockerfile for reference:

FROM ubuntu:xenial
RUN set -xe \
&& apt-get -y update \
&& apt-get -y install --no-install-recommends curl php \
&& curl http://nginx.org/keys/nginx_signing.key | apt-key add - \
&& echo "deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list \
&& echo "deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx" | tee -a /etc/apt/sources.list \
&& apt-get -y update \
&& apt-get -y install unit \
&& unitd --version
WORKDIR /www/laravel
COPY ./src /www/laravel
RUN chown -R www-data:www-data /www/laravel
CMD ["unitd", "--no-daemon"]

One of the exciting features of Unit for me was the ability to run multiple language configurations (i.e., a Golang and PHP application) and how you define the configuration objects through an API. If you check out my repository, after the Docker container starts, I run the following to define the PHP application:

#!/usr/bin/env bash
container=$(docker run --rm -d -v $(pwd)/src:/www/laravel -p 8300:8300 nginx-unit)
docker exec -ti $container curl -X PUT -d @/www/laravel/app.json --unix-socket /var/run/control.unit.sock http://localhost
echo "Container running @ $container"
echo "Here's the complete configuration object:"
docker exec -ti $container curl --unix-socket /var/run/control.unit.sock http://localhost

And here’s my demo configuration object for the Laravel application:

{
"listeners": {
"*:8300": {
"application": "laravel-demo"
}
},
"applications": {
"laravel-demo": {
"type": "php",
"workers": 20,
"user": "www-data",
"group": "www-data",
"root": "/www/laravel/public",
"index": "index.php"
}
}
}

Take my repo for a spin and let me know what you think @paulredmond!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK