-
Update your system. It's needed, otherwise I found I couldn't install node.js with
sudo apt update
This will get the update list (not actually install the packages)
Sudo apt upgrade
-
Install Node.js and npm. React requires Node.js and npm (Node Package Manager) to run.
sudo apt install curl
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo
apt install nodejs
Note: if you clone a repo from github, use
npm install
command to install all the dependencies in the first time. -
Verify Node.js and npm version
usenode -v
andnpm -v
-
Create a new React project
npx create-react-app my-app
cd my-app
npm start
-
Create the app to release npm run build
npm run build
This will generates a 'build' directory that contains all the static files(html, css, javascript) necessary to run the React app.
-
Update your system, as in last step Install React
-
Install Apache
sudo apt install apache2 -y
Once it's installed, Apache should start automatically. You can check if it's running by entering your Rpi's IP address (or just 'localhost' )in a browser, which shows a default apache page from
/var/www/html
. -
Copy 'build' folder to Rpi Use whatever method. Simply you can just use a usb key. (or you can use ssh if you want).
Then use this command to put the content of 'build' to folder /var/www/html (which is created by Apache by default):
sudo mv ~/build/* /var/www/html/
You may want to use this command to delete content if you mistake anything:
sudo rm -rf /var/www/html/*
Check the directory is empty by listing its contents:
ls /var/www/html
-
Check the website. You can check if it's running by entering your Rpi's IP address (or just 'localhost' )in a browser
Knowledge. If you're using a education network which block port 22, the we will have to use Cloudflare Tunnel [https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/ ]
Cloudflare Tunnel can connect HTTP web servers, ssh servers to Cloudflare.
-
Install cloudflared
download and install with command line tool according to the guideline.
-
Authenticate cloudflared
cloudflared tunnel login
If you Rpi is too slow to login, you can alternatively paste the info to your laptop to authenticate it.
-
Create a tunnel and name it
cloudflared tunnel create tunnel1
Here, I name it as 'tunnel1'. This command will
- generate a tunnel credentials file in the cloudflared directory on you Rpi,
- Create a subdomain of .cfargotunnel.com
- This command will return the tunnel's UUID which will be used in following steps
Use this command:
cloudflared tunnel list
to confirm that the tunnel has been successfully created. -
Create a configuration file
In your .cloudflared directory, created a config.yml file:
url: http://localhost:8000 tunnel: <Tunnel-UUID> credentials-file: /root/.cloudflared/<Tunnel-UUID>.json
This will configure the tunnel to route traffic from a given origin to the hostname of your choice
-
Start routing traffic Assign a CNAME record that points traffic to your tunnel subdomain.
usage:
cloudflared tunnel route dns <UUID or NAME> <hostname>
e.g.:
cloudflared tunnel route dns tunnel1 arthurqiangli.com
use the following command to see if the route has been successfully established, you can also check it on your cloudflared account.
cloudflared tunnel route ip show
-
Run the tunnel
Run the tunnel to proxy incoming traffic from the tunnel to services(unlimited) running on you RPI :
usage:
cloudflared tunnel run <UUID or NAME>
e.g.:
sudo cloudflared tunnel run tunnel1