| 1 |
Advanced PSGI usage |
| 2 |
=================== |
| 3 |
|
| 4 |
LL::NG is built on `Plack <http://plackperl.org/>`__, so it can be used |
| 5 |
with any compatible server: |
| 6 |
|
| 7 |
- `Starman <https://metacpan.org/pod/starman>`__ |
| 8 |
- `Twiggy <https://metacpan.org/pod/twiggy>`__ |
| 9 |
- `Twiggy::Prefork <https://metacpan.org/pod/Twiggy::Prefork>`__ |
| 10 |
- `Feersum <https://metacpan.org/pod/feersum>`__ |
| 11 |
- uWSGI using `uWSGI PSGI |
| 12 |
plugin <http://uwsgi-docs.readthedocs.io/en/latest/Perl.html>`__ |
| 13 |
- **Alternative**: :doc:`Node.js handler<nodehandler>` can be used as |
| 14 |
FastCGI server, only for application protection |
| 15 |
|
| 16 |
uWSGI or :doc:`Node.js FastCGI server<nodehandler>` may provide the |
| 17 |
highest performance. |
| 18 |
|
| 19 |
FastCGI server replacement |
| 20 |
-------------------------- |
| 21 |
|
| 22 |
A ``llng-server.psgi`` is provided in example directory. It is designed |
| 23 |
to replace exactly FastCGI server. You can use it : |
| 24 |
|
| 25 |
- with a FCGI Plack server, but you just have to change |
| 26 |
llng-fastcgi-server engine *(in |
| 27 |
/etc/default/lemonldap-ng-fastcgi-server)* to have the same result. |
| 28 |
Available engines: |
| 29 |
|
| 30 |
- `FCGI <https://metacpan.org/pod/Plack::Handler::FCGI>`__ |
| 31 |
**(default)**. It can use the following managers: |
| 32 |
|
| 33 |
- `FCGI::ProcManager <https://metacpan.org/pod/FCGI::ProcManager>`__ |
| 34 |
(default) |
| 35 |
- `FCGI::ProcManager::Constrained <https://metacpan.org/pod/FCGI::ProcManager::Constrained>`__ |
| 36 |
- `FCGI::ProcManager::Dynamic <https://metacpan.org/pod/FCGI::ProcManager::Dynamic>`__ |
| 37 |
|
| 38 |
- `AnyEvent::FCGI <https://metacpan.org/pod/Plack::Handler::AnyEvent::FCGI>`__ |
| 39 |
- `FCGI::EV <https://metacpan.org/pod/Plack::Handler::FCGI::EV>`__ |
| 40 |
- `FCGI::Engine <https://metacpan.org/pod/Plack::Handler::FCGI::Engine>`__ |
| 41 |
- `FCGI::Engine::ProcManager <https://metacpan.org/pod/Plack::Handler::FCGI::Engine::ProcManager>`__ |
| 42 |
- `FCGI::Async <https://metacpan.org/pod/Plack::Handler::FCGI::Async>`__ |
| 43 |
|
| 44 |
- with uWSGI **(see below)** |
| 45 |
|
| 46 |
|
| 47 |
.. attention:: |
| 48 |
|
| 49 |
Starman, Twiggy,... are HTTP servers, not FastCGI ones! |
| 50 |
|
| 51 |
You can also replace only a part of it to create a specialized FastCGI |
| 52 |
server (portal,...). Look at ``llng-server.psgi`` example and take the |
| 53 |
part you want to use. |
| 54 |
|
| 55 |
There are also some other PSGI files in examples directory. |
| 56 |
|
| 57 |
LL::NG FastCGI Server |
| 58 |
~~~~~~~~~~~~~~~~~~~~~ |
| 59 |
|
| 60 |
``llng-fastcgi-server`` can be started with the following options: |
| 61 |
|
| 62 |
==================== ===================== ===================== ========================================================================================== |
| 63 |
Command-line options Environment variable Explanation |
| 64 |
------------------------------------------ --------------------- ------------------------------------------------------------------------------------------ |
| 65 |
Short Long |
| 66 |
==================== ===================== ===================== ========================================================================================== |
| 67 |
-p --pid PID Process PID |
| 68 |
-u --user USER Unix uid |
| 69 |
-g --group GROUP Unix gid |
| 70 |
-n --proc NPROC Number of process to launch *(FCGI::ProcManager)* |
| 71 |
-s --socket SOCKET Socket to listen to |
| 72 |
-l --listen LISTEN Listening address. Examples: ``host:port``, ``:port``, ``/socket/path`` |
| 73 |
-f --customFunctionsFile CUSTOM_FUNCTIONS_FILE File to load for custom functions |
| 74 |
-e --engine ENGINE Plack::Handler engine, default to FCGI *(see below)* |
| 75 |
\ --plackOptions Other options to path to Plack. Can bu multi-valued. Values must look like ``--key=value`` |
| 76 |
==================== ===================== ===================== ========================================================================================== |
| 77 |
|
| 78 |
See ``llng-fastcgi-server(1)`` manpage. |
| 79 |
|
| 80 |
Some examples |
| 81 |
^^^^^^^^^^^^^ |
| 82 |
|
| 83 |
FCGI with FCGI::ProcManager::Constrained |
| 84 |
|
| 85 |
.. code-block:: shell |
| 86 |
|
| 87 |
llng-fastcgi-server -u nobody -g nobody -s /run/llng.sock -n 10 -e FCGI \ |
| 88 |
--plackOptions=--manager=FCGI::ProcManager::Constrained |
| 89 |
|
| 90 |
FCGI::Engine::ProcManager |
| 91 |
|
| 92 |
.. code-block:: shell |
| 93 |
|
| 94 |
llng-fastcgi-server -u nobody -g nobody -s /run/llng.sock -n 10 \ |
| 95 |
-e FCGI::Engine::ProcManager |
| 96 |
|
| 97 |
Using uWSGI |
| 98 |
~~~~~~~~~~~ |
| 99 |
|
| 100 |
You have to install uWSGI PSGI plugin. Then for example, start |
| 101 |
llng-server.psgi *(simple example)*: |
| 102 |
|
| 103 |
.. code-block:: shell |
| 104 |
|
| 105 |
/usr/bin/uwsgi --plugins psgi --socket :5000 --uid www-data --gid www-data --psgi /usr/share/lemonldap-ng/llng-server/llng-server.psgi |
| 106 |
|
| 107 |
You will find in LL::NG Nginx configuration files some comments that |
| 108 |
explain how to configure Nginx to use uWSGI instead of LL::NG FastCGI server. |
| 109 |
|
| 110 |
Using Debian lemonldap-ng-uwsgi-app package |
| 111 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 112 |
|
| 113 |
lemonldap-ng-uwsgi-app installs a uWSGI application: |
| 114 |
``/etc/uwsgi/apps-available/llng-server.yaml``. To enable it, link it in |
| 115 |
``apps-enabled`` and restart your uWSGI daemon: |
| 116 |
|
| 117 |
.. code-block:: shell |
| 118 |
|
| 119 |
apt install uwsgi uwsgi-plugin-psgi |
| 120 |
cd /etc/uwsgi/apps-enabled |
| 121 |
ln -s ../apps-available/llng-server.yaml |
| 122 |
service uwsgi restart |
| 123 |
|
| 124 |
Then adapt your Nginx configuration to use this uWSGI app. |
| 125 |
|
| 126 |
Configuration |
| 127 |
^^^^^^^^^^^^^ |
| 128 |
|
| 129 |
To serve large requests with uWSGI, you could have to modify in uWSGI |
| 130 |
and/or Nginx init files several options. Example: |
| 131 |
|
| 132 |
.. code-block:: ini |
| 133 |
|
| 134 |
workers = 4 |
| 135 |
buffer-size = 65535 |
| 136 |
limit-post = 0 |
| 137 |
|
| 138 |
.. code-block:: nginx |
| 139 |
|
| 140 |
client_max_body_size 300M; |
| 141 |
proxy_send_timeout 600; |
| 142 |
proxy_read_timeout 600; |
| 143 |
proxy_connect_timeout 600; |
| 144 |
uwsgi_read_timeout 120; |
| 145 |
uwsgi_send_timeout 120; |
| 146 |
|
| 147 |
.. note:: |
| 148 |
Nginx natively includes support for upstream servers speaking the uwsgi protocol since version 0.8.40. |
| 149 |
To improve performances, you can switch from a TCP socket to an UDS socket by editing |
| 150 |
``llng-server.yaml``: |
| 151 |
|
| 152 |
.. code-block:: ini |
| 153 |
|
| 154 |
uwsgi: |
| 155 |
plugins: psgi |
| 156 |
socket: /run/uwsgi.sock |
| 157 |
|
| 158 |
and adapting Nignx configuration files: |
| 159 |
|
| 160 |
.. code-block:: nginx |
| 161 |
|
| 162 |
# With uWSGI |
| 163 |
include /etc/nginx/uwsgi_params; |
| 164 |
uwsgi_pass unix:/run/uwsgi.sock; |
| 165 |
uwsgi_param LLTYPE psgi; |
| 166 |
uwsgi_param SCRIPT_FILENAME $document_root$sc; |
| 167 |
uwsgi_param SCRIPT_NAME $sc; |
| 168 |
# Uncomment this if you use Auth SSL: |
| 169 |
#uwsgi_param SSL_CLIENT_S_DN_CN $ssl_client_s_dn_cn; |
| 170 |
|
| 171 |
|
| 172 |
Protect a PSGI application |
| 173 |
-------------------------- |
| 174 |
|
| 175 |
LL::NG provides ``Plack::Middleware::Auth::LemonldapNG`` that can be used |
| 176 |
to protect any PSGI application: it works exactly like a LL::NG handler. |
| 177 |
Simple example: |
| 178 |
|
| 179 |
.. code-block:: perl |
| 180 |
|
| 181 |
use Plack::Builder; |
| 182 |
|
| 183 |
my $app = sub { ... }; |
| 184 |
builder { |
| 185 |
enable "Auth::LemonldapNG"; |
| 186 |
$app; |
| 187 |
}; |
| 188 |
|
| 189 |
More advanced example: |
| 190 |
|
| 191 |
.. code-block:: perl |
| 192 |
|
| 193 |
use Plack::Builder; |
| 194 |
|
| 195 |
my $app = sub { ... }; |
| 196 |
|
| 197 |
# Optionally ($proposedResponse is the PSGI response of Lemonldap::NG handler) |
| 198 |
sub on_reject { |
| 199 |
my($self,$env,$proposedResponse) = @_; |
| 200 |
# ... |
| 201 |
} |
| 202 |
|
| 203 |
builder { |
| 204 |
enable "Auth::LemonldapNG", |
| 205 |
llparams => { |
| 206 |
# ... |
| 207 |
}, |
| 208 |
on_reject => \&on_reject; |
| 209 |
$app; |
| 210 |
}; |
| 211 |
|