- raspberry pi zero w (You can choose any debian based basic server hardware like old ubuntu machine etc.)
- Dell 1TB external USB harddrive
- BerryBoot to install latest raspbian in ext HDD and let pi boot from there rather than SD card.
- It is connected to my VPN router with LAN cable and wifi is disabled.
sudo apt-get install wget git-core
sudo passwd root
3. Give it a fixed IP (In case you also want to access it within your local network, but don't want to check IP address from router all the time):
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Your Country code>
network={
ssid="YOUR_SSID"
psk="PASSWORD"
}
- Get the current IP through
ifconfig | grep inet
(hoping that this is your AP that you edited inwpa_supplicant.conf
file and want to attach to in future). sudo nano /etc/dhcpcd.conf
If you are going for wifi (wlan0
), add these lines at the bottom of dhcpcd.conf
# Custom static IP address for wlan0
interface wlan0
static ip_address=<Previoulsy given IP by router>
static routers=<Router's IP address>
static domain_name_servers=<Router's IP address>
If you are going for ethernet (eth0
), add these lines at the bottom of dhcpcd.conf
# Custom static IP address for eth0
interface eth0
static ip_address=<Previoulsy given IP by router>
static routers=<Router's IP address>
static domain_name_servers=<Router's IP address>
I have a pro plan on ngrok so that I can have links and domains
of choice and multiple other benefits over using a free version
mkdir /opt/ngrok
- Download ngrok and place it in
/opt/ngrok/
cd /opt/ngrok && sudo chmod u+x ngrok
- Link your account
cd /opt/ngrok && ./ngrok authtoken <Your token>
- Create a config file:
sudo nano /opt/ngrok/ngrok.yml
and change it according to your needs.
In case you need help, here is the ngrok doc.
A sample config file for exposing ssh ports and web app server ports
authtoken: <input your auth token you got from ngrok>
region: us
web_addr: localhost:5000
tunnels:
ssh-access:
addr: 22
proto: tcp
remote_addr: <Pro plan holders can input a fixed remote addr from ngrok>
web_app:
addr: <your web server's http port>
proto: http
subdomain: <give it a name>
- Add a ngrok service:
sudo nano /etc/systemd/system/ngork.service
[Unit]
Description=ngrok
After=network.target
[Service]
ExecStart=/opt/ngrok/ngrok start --all --config /opt/ngrok/ngrok.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
Type=simple
[Install]
WantedBy=multi-user.target
- Start ngork service by typing:
systemctl daemon-reload
systemctl enable ngrok.service
systemctl start ngrok.service
5. (Optional): Install your ssh public key on this server for secure hassle free log ins and pushes:
I followed these steps from raspberry pi's website's official guide
sudo apt update -y && sudo apt upgrade -y
git clone https://github.com/creationix/nvm.git ~/.nvm
sudo echo "source ~/.nvm/nvm.sh" >> ~/.bashrc && sudo echo "source ~/.nvm/nvm.sh" >> ~/.profile
sudo reboot
nvm --version
nvm install 8.11.4
nvm install --lts
nvm use --lts
# then update npm
npm -g install npm@latest --allow-root --unsafe-perm
sudo -i npm install forever -g
- Log in to your server
cd gits
git init --bare <your-project-name>.git
For pushing first time:
cd
into your project dir.- Then add, commit and push
git init
git add .
git commit -m "commit message" -a
On local network or fixed IP servers:
git remote add origin <user>@<local IP address>:/absolute_path_to_your_project/<your-project-name>.git
Or for ngrok-ed tcp exposed server:
git remote add origin ssh://<user>@1.tcp.ngrok.io:<ngrok_given_port>absolute_path_to_your_project/<your-project-name>.git
git push -u origin master
[Note: Have a readme file, license file and gitignore file. These help!]
git clone
your project from this repo (I know it's funny that you ae cloning in the same machine but you know got doesn't create folders, although you can checkout a branch, this is safer.)npm install
(because if you are smart you didn't include thenode_modules
dir from your project directory and hence now you have to install them. Better practise as if anything breaks becasue of diff suystem requirements for the node modules, you'll know what it is.)- test your server
sudo node <PATH TO YOUR REPO IN EXT HDD>/server.js
.
sudo -i npm install forever -g
- test your server with
sudo forever <path to your dir in the ext hdd >
sudo nano /lib/systemd/system/app_server.service
[Unit]
Description=web app server # Give it a description
After=network.target # wait for it to load after netwrok is enabled
[Service]
WorkingDirectory=</PATH TO YOUR PROJECT'S SERVER DIRECTORY>
ExecStart=/bin/bash -c 'exec </PATH TO YOUR NODE BINARY> </PATH TO YOUR PROJECT'S SERVER> >> </PATH TO A LOG FILE IF YOUR SERVER HAS STDOUT> 2>&1''
Restart=on-failure
User=<SERVER's USER ACCOUNT> # IN my case it's just 'pi'
[Install]
WantedBy=multi-user.target
sudo chmod 644 /lib/systemd/system/app_server.service
enables the service.sudo systemctl daemon-reload
sudo systemctl enable app_server.service
sudo systemctl start app_server.service
- check status by both
sudo systemctl status app_server.service
andps aux | grep node
sudo reboot
- Check if your serevr is running and you can access it on local network over it's fixed.
- Also then go to ngrok's status panel nad grab the link to your app. (It should be something like
http://
andhttps://
bith and after thatsubdomain.ngrok.io
(If you gave it a subdomain or else it will be a random number))