Skip to content

Instantly share code, notes, and snippets.

@dattasaurabh82
Last active September 10, 2018 10:15
Show Gist options
  • Save dattasaurabh82/dda2970c351205c48ce1cb5e053820c1 to your computer and use it in GitHub Desktop.
Save dattasaurabh82/dda2970c351205c48ce1cb5e053820c1 to your computer and use it in GitHub Desktop.
Create your own private git server, make it accessible outside local network and deploy your own private node apps.

Personal Git and Deployment SERVER setup with access outside network

Hardware used (My personal basic setup):

  1. raspberry pi zero w (You can choose any debian based basic server hardware like old ubuntu machine etc.)
  2. Dell 1TB external USB harddrive
  3. BerryBoot to install latest raspbian in ext HDD and let pi boot from there rather than SD card.
  4. It is connected to my VPN router with LAN cable and wifi is disabled.

1.Dependency installs:

1. Install git :

sudo apt-get install wget git-core

2. Change the root password:

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):

  1. 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"
}
  1. Get the current IP through ifconfig | grep inet (hoping that this is your AP that you edited in wpa_supplicant.conf file and want to attach to in future).
  2. 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>

4. Install ngrok, open ports and make it load after boot from system level:

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

  1. mkdir /opt/ngrok
  2. Download ngrok and place it in /opt/ngrok/
  3. cd /opt/ngrok && sudo chmod u+x ngrok
  4. Link your account cd /opt/ngrok && ./ngrok authtoken <Your token>
  5. 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>
  1. 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
  1. 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

6. Install node using NVM.

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

7. Install forever (just in case)

sudo -i npm install forever -g

2. How to use the git server:

2.1 ON GIT SERVER create a bare project git dir:

  1. Log in to your server
  2. cd gits
  3. git init --bare <your-project-name>.git

2.2 ON CLIENT SIDE:

For pushing first time:
  1. cd into your project dir.
  2. 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!]

3. How to use the deployment server part:

1. Install node ( mentioned before)(I mostly use node server so...)

2. Clone and test your server

  1. 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.)
  2. npm install(because if you are smart you didn't include the node_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.)
  3. test your server sudo node <PATH TO YOUR REPO IN EXT HDD>/server.js.

3. Install forever and test your server

  1. sudo -i npm install forever -g
  2. test your server with sudo forever <path to your dir in the ext hdd >

4. Create a service for your server

  1. 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
  1. sudo chmod 644 /lib/systemd/system/app_server.service enables the service.
  2. sudo systemctl daemon-reload
  3. sudo systemctl enable app_server.service
  4. sudo systemctl start app_server.service
  5. check status by both sudo systemctl status app_server.service and ps aux | grep node
  6. sudo reboot
  7. Check if your serevr is running and you can access it on local network over it's fixed.
  8. Also then go to ngrok's status panel nad grab the link to your app. (It should be something like http:// and https:// bith and after that subdomain.ngrok.io (If you gave it a subdomain or else it will be a random number))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment