GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Installare ReactJS e distribuirlo con Nginx su Ubuntu 20.04

ReactJS è una libreria JavaScript gratuita e open source per creare interfacce utente riutilizzabili. In questa guida installiamo Create React App, creiamo un'app React e la pubblichiamo dietro Nginx su Ubuntu 20.04.

Prerequisiti

  • Server Ubuntu 20.04 con almeno 2 GB di RAM.
  • Dominio che punti all'IP del server, ad esempio reactjs.example.com.
  • Accesso root o utente con privilegi sudo.

1. Aggiornare il sistema

apt-get update -y
apt-get install curl gnupg2 -y

2. Installare Node.js

curl -sL https://deb.nodesource.com/setup_14.x | bash -
apt-get install nodejs -y
npm install npm@latest -g
node -v

3. Installare Create React App

npm install -g create-react-app

4. Creare la prima app React

cd /opt
create-react-app reactproject
cd /opt/reactproject
npm start

Premi CTRL+C per fermare il server di sviluppo.

5. Creare un servizio systemd

nano /lib/systemd/system/react.service
[Service]
Type=simple
User=root
Restart=on-failure
WorkingDirectory=/opt/reactproject
ExecStart=npm start -- --port=3000
systemctl daemon-reload
systemctl start react
systemctl enable react
systemctl status react

6. Configurare Nginx come reverse proxy

apt-get install nginx -y
nano /etc/nginx/sites-available/reactjs.conf
upstream backend {
    server localhost:3000;
}

server {
    listen 80;
    server_name reactjs.example.com;

    location / {
        proxy_pass https://backend/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto http;
        proxy_redirect off;
    }
}
ln -s /etc/nginx/sites-available/reactjs.conf /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx

7. Accedere all'app

Apri https://reactjs.example.com nel browser. Per la produzione valuta npm run build e la pubblicazione di file statici.

Conclusione

Hai installato ReactJS, creato un servizio systemd e configurato Nginx come reverse proxy su Ubuntu 20.04.

Ubuntu
  1. Come installare R su Ubuntu 20.04

  2. Eseguire Firefox Nightly come Flatpak su Ubuntu: guida pratica

  3. Installare R su Ubuntu 16.04: guida rapida

  4. Installare Go su Ubuntu 18.04 e creare una semplice app

  5. Installare EPrints su Ubuntu 20.04

Come installare ReactJS su Ubuntu 21.04

Come installare Franz su Ubuntu 20.04

Installare ReactJS su Ubuntu 20.04 LTS: Node, Yarn e prima app

Installare Go in Ubuntu 20.04

Installare ReactJS su Ubuntu 20.04/18.04: guida passo passo

Come installare Go su Ubuntu 22.04