Ubuntu 16.04 LTS: Installazione di PostGREST con NGINX

Per installare PostgREST, per semplicità impersoniamo l’utenti root con:

sudo su

Poi partiamo con l’installazione dei prerequisiti software cosa che si fa’ velocemente con:

apt-get install libpq-dev

poi ci posizioniamo in:

cd /tmp

e scaricare l’eseguibile con:

wget https://github.com/begriffs/postgrest/releases/download/v0.4.2.0/postgrest-0.4.2.0-ubuntu.tar.xz

Se avete un po’ di tempo controllate che la versione sia l’ultima facendo un giretto con il vostro browser su:

https://github.com/begriffs/postgrest/releases

Una volta scaricato lo si scompatta con il comando:

tar xvf postgrest-0.4.2.0-ubuntu.tar.xz

e lo si sposta in:

/usr/local/bin

con il comando:

mv ./postgrest /usr/local/bin/postgrest

Eventualmente controllate che i permessi siano omogenei con gli altri file presenti nella stessa directory.

A questo punto creiamo il file di configurazione in:

/etc/postgrest.conf

Con le seguenti direttive:

db-uri = "postgres://postgres:<password di postgres>@localhost/scuola247"
db-schema = "public"
db-anon-role = "postgrest"
db-pool = 10

In questa maniera abbiamo esposto tutto lo schema public al server PostGREST che è accetabile per fare delle prove ma chiaramente in produzione andrò rivisto.
Adesso bisogna creare l’utente per impersonare le richieste anonime in postgresql:

sudo su postgres
psql
create role postgrest nologin;
grant postgrest to postgres;
grant usage on schema public to postgrest;
grant select on all tables in schema public to postgrest;

Usciamo con un:

\q
exit

E facciamo quindi partire il nostro server con:

/usr/local/bin/postgrest /etc/postgrest.conf

Verifichiamo il funzionamento con:

 curl 127.0.0.1:3000/school_years

Ora creiamo il servizio per far partire il server all’avvio del sistemza, dobbiamo quindi creare il file:

/lib/systemd/service/postgrest.service

che contiene le seguenti istruzioni:

#
# systemd service for managing PostgREST server
# 
[Unit]
Description=PostREST REST Server for PostgreSQL Database Server
Requires=postgresql.service
After=postgresql.service
Documentation=https://postgrest.com

[Service]
Type=simple
ExecStart=/usr/local/bin/postgrest /etc/postgrest.conf
ExecReload=/usr/bin/killall -HUP postgrest
Restart=on-failure

[Install]
Alias=postgrest.service

per abilitarlo usiamo:

sudo systemctl enable postgrest

ricordo brevemente gli altri comandi per far partire, fermare, ripartire e ricaricare la configurazione:

sudo systemctl start postgrest
sudo systemctl stop postgrest
sudo systemctl restart postgrest
sudo systemctl reload postgrest

infine se facciamo delle modifiche allo script:

/lib/systemd/service/postgrest.service

ricordiamoci di segnalarlo al sistema con:

sudo systemctl daemon-reload

Ora dobbiamo fare in modo di collegare NGINX che ci gestisce la connessione HTTPS con PostgREST per fare ciò modifichiamo la configurazione del web server di NGINX aggiungendo:

 #
 # expose API to the outside world
 #
 location /api/ {
     proxy_pass http://localhost:3000/;
 }

Ricordiamo di far ripartire NGINX con:

systemctl restart nginx

L’esempio del file di NGINX completo è:

##
## Pgadmin reverse proxy
##

# Default server configuration
#
server {
 listen 80 default_server;
 #
 # SSL configuration
 #
 listen 443 ssl default_server;
 #
 # Self signed certs generated by the ssl-cert package
 # Don't use them in a production server!
 #
 include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP

index index.html index.htm index.nginx-debian.html;

server_name scuola247 scuola247.fulcro.net scuola247.sede.fulcro.net;

#
 # expose PgAdmin to the outside world
 #
 location / {
 proxy_pass http://127.0.0.1:5050/;
 }
 #
 # expose API to the outside world
 #
 location /api/ {
 proxy_pass http://localhost:3000/;
 }
}

gli altri parametri che possiamo usare nel file di configurazione sono:

#
 # Here is the full list of configuration parameters.
 #
 # Name Type Default Required
 # db-uri String Y
 # db-schema String Y
 # db-anon-role String Y
 # db-pool Int 10
 # server-host String *4
 # server-port Int 3000
 # server-proxy-uri String
 # jwt-secret String
 # secret-is-base64 Bool False
 # max-rows Int ∞
 # pre-request String
 #
 # db-uri
 #
 # The standard connection PostgreSQL URI format.
 # Symbols and unusual characters in the password
 # or other fields should be percent encoded to
 # avoid a parse error.
 # On older systems like Centos 6, with older
 # versions of libpq, a different db-uri syntax
 # has to be used.
 # In this case the URI is a string of space
 # separated key-value pairs (key=value), so the
 # example above would be:
 # "host=host user=user port=5432 dbname=dbname password=pass".
 # Also allows connections over Unix sockets for higher performance.
 #
 # db-schema
 #
 # The database schema to expose to REST clients.
 # Tables, views and stored procedures in this schema
 # will get API endpoints.
 #
 # db-anon-role
 #
 # The database role to use when executing commands
 # on behalf of unauthenticated clients.
 #
 # db-pool
 #
 # Number of connections to keep open in PostgREST’s
 # database pool.
 # Having enough here for the maximum expected
 # simultaneous client connections can improve
 # performance.
 # Note it’s pointless to set this higher than
 # the max_connections GUC in your database.
 #
 # server-host
 #
 # Where to bind the PostgREST web server.
 #
 # server-port
 #
 # The port to bind the web server.
 #
 # server-proxy-uri
 #
 # Overrides the base URL used within the OpenAPI
 # self-documentation hosted at the API root path.
 # Use a complete URI syntax scheme:
 # [//[user:password@]host[:port]][/]path[?query][#fragment].
 # Ex. https://postgrest.com

Come sempre sperando che questo vi possa essere utile ….

Andrea

Lascia un commento