Avís
Aquest article és antic i és possible que part de la informació estigui desactualitzada o ja no sigui vàlida actualment.
Si tens cap dubte o necessites confirmar algun detall, et recomanem que contactis amb el nostre equip de suport per rebre informació actualitzada i assistència personalitzada.
Parse Server és un backend (BaaS) que agilitza enormement el desenvolupament d'aplicacions, ja que inclou SDK per a plataformes com Android, iOS, PHP, JavaScript i Unity, entre altres entorns disponibles. Així, els desenvolupadors poden evitar haver de crear una API i reduir dràsticament els temps de desenvolupament.
A més, inclou funcionalitats com un tauler d'administració, notificacions push, integració de l'inici de sessió de tercers com Facebook, Google o Apple, correus electrònics de registre i recuperació de contrasenya, entre moltes altres característiques.
A continuació, et mostrem detalladament els passos necessaris per instal·lar Parse-Server i Parse-Dashboard i crear un certificat SSL de Let's Encrypt amb nGinx per protegir les connexions en un servidor Ubuntu 20.04 a Clouding.
Requisits de l'entorn
- Ubuntu 20.04
- MongoDB
- NodeJS
- Yarn
- nGinx
- Let's Encrypt
Obrir els ports necessaris al tallafoc de Clouding
Els ports que utilitzen Parse Server i Parse Dashboard són el 1337 i el 4040, respectivament. Tanmateix, com que generarem certificats SSL amb Let's Encrypt, hauràs de fer servir externament el port 1338 per a Parse Server i els ports 80 i 443 per a Parse Dashboard. Utilitzarem nGinx com a servidor intermediari que redirigirà el trànsit als ports 1337 i 4040.
Tallafoc
Per obrir els ports necessaris al tauler de Clouding, pots seguir els passos de l'enllaç següent: Com crear una regla de tallafoc.
El resultat hauria de ser com el següent:
Instal·lació de MongoDB
Tot i que Parse Server admet tant MongoDB com Postgresql, ens decantem per la primera perquè ofereix més integració, rendiment i facilitat en la configuració inicial, ja que no cal crear cap esquema inicial com en el cas de PostgreSql.
Per instal·lar MongoDB, hauràs d'executar les ordres següents:
# apt update# apt upgrade# apt install mongodb-server -y# systemctl status mongodbroot@parse-tutorial:~/parse-server# systemctl status mongodb
● mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-04-01 15:19:12 CEST; 29min ago
Docs: man:mongod(1)
Main PID: 554 (mongod)
Tasks: 34 (limit: 2280)
Memory: 180.7M
CGroup: /system.slice/mongodb.service
└─554 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc/mongodb.confAmb aquestes ordres, ja tindràs el servidor de bases de dades preparat perquè Parse Server pugui emmagatzemar les dades.
Instal·lació de NodeJS juntament amb el gestor de paquets Yarn
Per poder desplegar tant Parse-Server com Parse-Dashboard, hauràs d'instal·lar l'última versió estable de NodeJS. Per facilitar la instal·lació de tots dos, instal·larem el gestor de paquets Yarn. Per fer-ho, hauràs d'executar la sèrie d'ordres següent:
Afegeix el repositori de NodejS.
# curl -sL https://deb.nodesource.com/setup_lts.x | bash -Instal·la l'última versió estable de NodeJS.
# apt install nodejs -yInstal·la el gestor de paquets yarn.
# npm install -g yarnComprova la versió de Node instal·lada.
# node --version
v14.16.0Ara ja tens totes les dependències prèvies per poder instal·lar Parse Server i el Dashboard.
Instal·lació de Parse Server
La instal·lació de Parse Server amb el gestor de paquets Yarn és molt senzilla. Per fer-la, hauràs d'executar l'ordre següent:
# yarn global add parse-serverCrea el fitxer "config.json", on especificaràs la configuració del servidor Parse.
# mkdir parse-server
# cd parse-server
# vi config.jsonCopia el contingut següent:
{
"appName": "Clouding Parse Server",
"databaseURI": "mongodb://localhost:27017/parsedb",
"appId": "418627a8-de68-4989-ad28-5dc5802887eb",
"javascriptKey" : "418627a8-de68-4989-ad28-5dc5802887eb",
"restAPIKey" : "418627a8-de68-4989-ad28-5dc5802887eb",
"clientKey" : "418627a8-de68-4989-ad28-5dc5802887eb",
"masterKey": "4938193c-3d20-4243-9eb9-031ca1ce9f18",
"serverURL": "http://localhost:1337/parse",
"publicServerURL": "https://9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host:1338/parse",
"port": 1337
}Inicia el servidor Parse.
# nohup parse-server config.json &Comprova que el port de Parse Server està a l'escolta.
# ss -ant | grep 1337
LISTEN 0 511 0.0.0.0:1337 0.0.0.0:*Un cop instal·lat el servidor Parse, pots instal·lar el Dashboard per facilitar l'administració diària del servidor.
Instal·lació de Parse Server Dashboard
Igual que a l'apartat anterior, la instal·lació del Dashboard es fa fàcilment amb l'ordre següent:
# yarn global add parse-dashboardCrea el fitxer "parse-dashboard-config.json".
# vi parse-dashboard-config.jsonCopia la configuració següent.
{
"apps": [
{
"serverURL": "https://9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host:1338/parse",
"appId": "418627a8-de68-4989-ad28-5dc5802887eb",
"masterKey": "4938193c-3d20-4243-9eb9-031ca1ce9f18",
"allowInsecureHTTP": "true",
"appName": "Clouding Parse App"
}
],
"users": [
{
"user":"Clouding",
"pass":"12345"
}
]
}Inicia el Dashboard.
# nohup parse-dashboard --dev --config ./parse-dashboard-config.json &Comprova que el port de Parse Server està a l'escolta.
# ss -ant | grep 4040
LISTEN 0 511 0.0.0.0:4040 0.0.0.0:*
Perfecte! Ja tens instal·lats tant el servidor Parse com el Dashboard.
Instal·lació i configuració de nGinx
Hauràs d'utilitzar nGinx com a servidor intermediari invers per a Parse Server i Parse Dashboard. A més, fer-ho així et facilitarà la instal·lació de certificats SSL i et permetrà gaudir de les renovacions automàtiques sense haver d'accedir al servidor per configurar-lo cada tres mesos. Per fer-ho, hauràs de seguir els passos següents:
Instal·la nGinx.
# apt install -y nginxComprova l'estat del servei de nGinx.
# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-04-01 15:19:13 CEST; 49min ago
Per a aquest exemple, utilitzem l'identificador que s'inclou en cada VPS de Clouding. En aquest cas, "9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host".
Crea el fitxer per al nou domini amb què treballaràs:
# vi /etc/nginx/sites-available/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.hostCopia la configuració següent. Com pots veure, el port 80 redirigeix el trànsit al port 4040 del Dashboard i el port 1338 el redirigeix al port 1337 del servidor Parse.
server {
listen 80;
server_name 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host;
location / {
proxy_pass http://127.0.0.1:4040/;
}
}
server {
listen 1338;
server_name 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host;
location / {
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:1337/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
# habilita la compatibilitat amb WS
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 300;
}
}
Crea l'enllaç simbòlic per activar el lloc nou.
# ln -s /etc/nginx/sites-available/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host /etc/nginx/sites-enabled/Comprova que la configuració sigui correcta.
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reinicia el servei de nGinx.
# systemctl restart nginxUn cop tinguis el servidor de nGinx configurat, pots crear el certificat SSL per poder utilitzar el protocol https.
Instal·lació del certificat SSL amb Let's Encrypt
Hauràs d'utilitzar els certificats SSL de Let's Encrypt i, per fer-ho, hauràs d'instal·lar Let's Encrypt de la manera següent:
# apt install certbot python3-certbot-nginxCrea el certificat per al domini "9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host".
# certbot --installer nginx -d 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.hostRespon les preguntes tal com es mostra a continuació. Comprova que el procés acabi correctament amb el missatge "Congratulations! Your certificate and chain have been saved at:".
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Nginx Web Server plugin (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel): 1
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): clouding@clouding.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled
https://9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host/privkey.pem
Your cert will expire on 2021-06-30. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
Torna a editar el fitxer "9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host" per configurar el port 1338 perquè també funcioni amb SSL.
# vi /etc/nginx/sites-available/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.hostL'hauràs d'editar perquè quedi de la manera següent:
server {
server_name 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host;
location / {
proxy_pass http://127.0.0.1:4040/;
}
listen 443 ssl; # gestionat per Certbot
ssl_certificate /etc/letsencrypt/live/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host/fullchain.pem; # gestionat per Certbot
ssl_certificate_key /etc/letsencrypt/live/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host/privkey.pem; # gestionat per Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # gestionat per Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # gestionat per Certbot
}
server {
listen 1338 ssl;
server_name 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host;
location / {
proxy_pass http://127.0.0.1:1337/;
}
ssl_certificate /etc/letsencrypt/live/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host/fullchain.pem; # gestionat per Certbot
ssl_certificate_key /etc/letsencrypt/live/9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host/privkey.pem; # gestionat per Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # gestionat per Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # gestionat per Certbot
}
server {
if ($host = 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host) {
return 301 https://$host$request_uri;
} # gestionat per Certbot
listen 80;
server_name 9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host;
return 404; # gestionat per Certbot
}Ja està! Ara ja pots accedir al teu Parse-Server i Parse-Dashboard de manera segura amb un certificat SSL de Let's Encrypt.
Accedir al Dashboard de Parse
Per accedir al Dashboard, obre un navegador i ves a l'URL https://9a5d1810-92bf-44f0-a0bd-a880724d9f96.clouding.host on hauries de veure la finestra d'inici de sessió següent.
Introdueix les credencials configurades anteriorment i fes clic a "Log In". Hauries de veure el Dashboard següent, amb el qual ja podràs començar a treballar.
Configuració de Parse-Server i Parse-Dashboard com a serveis del sistema amb Systemd
Per no haver d'iniciar els serveis de Parse Server i Parse Dashboard manualment cada vegada que es reinicia el servidor, segueix els passos següents:
# cd /etc/systemd/system/Crea el fitxer de configuració del servei següent.
# vi parse.server.serviceCopia el contingut següent.
[Unit]
Description=Parse Server service
After=mongodb.service
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/usr/local/bin/parse-server /root/parse-server/config.json
[Install]
WantedBy=multi-user.targetInicia i habilita el servei.
# systemctl start parse.server.service
# systemctl enable parse.server.serviceFes el mateix per al servei del Dashboard.
# vi parse.server.dashboard.service
Copia la configuració següent.
[Unit]
Description=Parse Server Dashboard service
After=parse.server.service
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/usr/local/bin/parse-dashboard --dev --config /root/parse-server/parse-dashboard-config.json
[Install]
WantedBy=multi-user.targetInicia i habilita el servei.
# systemctl start parse.server.dashboard.service
# systemctl enable parse.server.dashboard.serviceComprovació del funcionament de l'entorn
Una de les maneres més fàcils de comprovar que la instal·lació funciona correctament és accedir al teu Dashboard > API Console > Rest Console. Emplena els camps tal com et mostrem a continuació per crear un usuari nou i fes clic a "Send Query".
Ves a Browser > User i comprova que l'usuari creat està disponible.
Amb els passos detallats en aquest tutorial, ja tens el teu propi servidor de Parse a punt per desenvolupar aplicacions de manera més ràpida i segura. Esperem que t'hagi estat útil! :)