How to Upload Files to a Raspberry Pi Server

The Raspberry Pi is a little computer that you can become for as low as USD $5 and on which you can run many unlike types of software and build many different projects.

In this article, I'thou going to guide you lot through the process of setting it up as a home development server and deploying a full-stack JavaScript application that yous tin can access from exterior your network. This is keen for setting up your ain remote digital workspace, or simply to have command over the hardware you use for development.

What Do You Demand for This Raspberry Pi Home Server?

While this is now a Raspberry Pi 3 tutorial in item, it should even so work with models going back to the first generation—if you take an older model or a Raspberry Pi Nada, please permit us know your experience in the comments below.

In addition to the Raspberry Pi board itself, you will need:

  • A Micro USB charger
  • An Ethernet cablevision
  • A microSD card (minimum 8GB, and cards up to 32GB seem to work fine)

These will as well come in handy during the initial setup:

  • A USB keyboard
  • An HDMI cable and monitor

The Raspberry Pi OS: Raspbian

Installing an operating organisation onto a Raspberry Pi is elementary. First, using your computer, install the kicking image onto a microSD card. Then only insert the card into the Raspberry Pi and boot from there.

Raspbian is a Linux distribution ported from Debian 7.0 (Wheezy), and is the official OS for Raspberry Pi optimized for the device's compages. While at that place are other options for running your favorite OS on the Pi, we'll use Raspbian because of its simplicity.

This tutorial has been updated to work with this version (or later) of Raspbian:

          Kernel version : #ane SMP Debian 4.9.110-3+deb9u4 (2018-08-21) Kernel release : 4.nine.0-viii-amd64                  

To install Raspbian, head to the official download folio and download the nothing file with the latest Raspbian version.

Then, insert the microSD card into your computer's SD card slot or adapter. Depending on your estimator's operating system, follow the instructions provided on Raspberry'due south website for Linux, Mac Os, or Windows.

One time the process is finished, eject the SD card from your computer and insert information technology into the Raspberry Pi. Connect the Raspberry Pi to your router using the Ethernet cable, and plug in the Micro USB charger, which volition start the Raspberry Pi booting.

For the initial configuration, at that place are two options:

  • If yous have a USB keyboard and an HDMI monitor, you lot tin plug them into the Raspberry Pi for the initial setup.
    • Your Pi should recognize these devices as soon as they are plugged in.
    • The starting time time the Pi boots, information technology will automatically run raspi-config. Later the starting time boot, you will need to run sudo raspi-config yourself in order to configure the device.
  • If yous don't have them, you tin connect to your Raspberry Pi while it is on using SSH:
    • First, y'all need to find the IP address of your Raspberry Pi in your local network. This can be done by connecting to your router's admin page, or by using a network tool similar nmap.
    • Once you have the device'south IP accost, connect to it using SSH from your terminal (or through Putty if y'all're using Windows). The default user is pi, and the default password is raspberry. So, for example, if the IP address is 192.168.one.16, run ssh pi@192.168.1.16 and enter the password when prompted.
    • When you are connected, run sudo raspi-config.

raspi-config will walk you through the concluding setup. You lot tin configure all the options but the well-nigh important are the commencement two: to expand the filesystem, ensuring that all the SD carte storage is available for the OS, and to change the password for the default Pi user, so that your server will be protected from intruders.

Raspi-config

Install a Web Server (Nginx) on Your Raspberry Pi

Adjacent, you'll install the web server. I prefer Nginx considering it has a small-scale memory footprint, and because it plays well with Node.js (which you'll be setting up after). Other web servers, such as Apache or lighttpd, would work as well, but we'll utilise Nginx for this demonstration.

Before yous first installing annihilation, you need to be sure everything is upward-to-engagement by running these commands on the Pi:

          sudo apt-go update sudo apt-get upgrade                  

Then you can install Nginx using apt-get:

          sudo apt-get install nginx                  

Once installation is completed, start the server by running:

          sudo service nginx start                  

If you didn't have to figure out the local IP of your Raspberry Pi in the previous footstep, it's time to find out by running ifconfig. The output for your ethernet adaptor will be under eth0, and with its local IP address labeled inet addr.

One time you lot know the IP accost, you tin point your computer's browser at information technology, where you should see the default "Welcome to Nginx" message.

Open to the Web: Port Forwarding

You can skip this pace if you are not planning to access your Raspberry Pi from exterior your local network. Just for those wanting to access their server from other locations, let'southward make certain that'due south possible.

In a typical habitation network, devices continued to the router are invisible to the exterior globe. Merely your router can be reached from outside, using your network'south external IP address. Your router is responsible for determining which incoming traffic is allowed into the network, and which device information technology should be sent to.

When a device on the local network initiates a connection (for example, when you open a website on your browser), the router recognizes the incoming response traffic as being part of this connection, and allows it through. However, if the router receives incoming traffic that is not part of an open connexion (for example, when an outside device attempts to initiate a connexion with an inside device), it will block the incoming traffic from crossing into the network. This is an important security characteristic to protect the network!

So how tin you connect to your Pi from outside? The answer is port forwarding. The router must be configured to let incoming connections on specific ports to pass through, and exist sent to the correct device. By the default, the HTTP protocol uses port fourscore, and SSH uses port 22, so these are the ii ports that you demand to open on your router in order to admission your web application, and allow secure connections for managing your server.

Port forwarding.

The steps to configure your router to open and forward the ports may vary depending on your internet provider and the make of your router, but in whatever case, you should exist able to accomplish it through the advanced configuration options of your router's admin page. Just look for an choice with a name similar "Forwarding," "Port Forwarding," or "Network Address Translation."

You need to open a port for HTTP connections, and some other 1 for SSH. The bones idea consists of forwarding data addressed to these two external ports to your Raspberry Pi, with web traffic going to port lxxx where Nginx is listening, and SSH traffic going to port 22, where the SSH server accepts connections from external computers. Here's an example of how this might look in your router's configuration page:

Port forwarding configuration table.

Port forwarding configuration if your Raspberry Pi's internal IP accost is 192.168.i.16. All incoming traffic bound for ports eighty or 22 are forwarded to this internal address.

Y'all can determine your router's external IP address past simply typing "what's my ip accost" into Google. If you then motility outside your router'south network, you tin test that port forwarding is working past opening an SSH connexion with ssh pi@{external IP address}. Likewise, HTTP port forwarding tin can exist tested by entering the external IP address into your browser's address bar. Just continue in mind that port forwarding allows anyone from outside to access the device on these ports if they know your router'due south external IP.

If y'all don't have a static IP, so y'all can set upwards dynamic DNS. It's a very simple and easy step. You can set dynamic DNS from your router or you can configure your Raspberry Pi for it. I'one thousand not going to cover how to configure DDNS here, simply BitPi.co has a good tutorial on the subject if needed.

Install the Framework: Total-stack JavaScript

Y'all tin can run most web frameworks on height of Nginx, merely let's see how to go full-stack with JavaScript. To do this, yous need to install Node.js and MongoDB.

Nowadays, Node.js is easily installed onto a Raspberry Pi with:

          sudo apt-get install nodejs                  

In one case it finishes installing, you lot can check if information technology's working by running node -5.

Now you tin install MongoDB just by typing:

          sudo apt-become install mongodb                  

Only be aware that if you ever demand to plough off the Raspberry Pi, yous need to close down the service outset in lodge to avoid database corruption:

          sudo service mongodb stop                  

Deploy Your App

You tin can develop on your local motorcar, and and then button your changes to a Git repository on BitBucket. Since Raspbian comes with Git pre-installed, you can then pull your latest application code onto the device and run information technology.

Scaffold the Project

First permit's set some application code and push information technology to a Git repository. There are many means of starting an application, but 1 of my favorites is generator-athwart-fullstack, which scaffolds both server and client lawmaking.

Install generator-angular-fullstack on your computer:

          npm install -g generator-athwart-fullstack                  

Create a new directory for your application:

          mkdir my-app cd my-app                  

And scaffold the awarding:

          yo athwart-fullstack my-app                  

Create the Repository and Push the Code

Now create a repository in BitBucket, equally described here. Then set up your local directory:

          git init git remote add origin git@bitbucket.org:USER/REPO.git                  

Then you can commit and push the code:

          git add . git commit -m 'Initial commit' git push -u origin master                  

The generator comes with the grunt-build-control plugin, which allows you to commit the build lawmaking to a specific co-operative in your repository. But add the configuration for BitBucket to Gruntfile.js in your application's root directory:

          buildcontrol: {    options: {       dir: 'dist',       commit: true,       push button: true,       connectCommits: false,       message: 'Built %sourceName% from commit %sourceCommit% on branch %sourceBranch%'    },    bitbucket: {       options: {          remote: 'git@bitbucket.org:USER/REPO.git',          branch: 'build'       }    } }, // ...                  

At present run:

          grunt build                  

…to create the distribution binder, followed past this:

          grunt buildcontrol:bitbucket                  

…to commit and push button the code to the build branch in your repository.

Generate the SSH Key

You now take your lawmaking hosted. Before you tin can deploy it to your Raspberry Pi, you need to generate an SSH cardinal for the Raspberry Pi and add it to your BitBucket account. We'll run through this stride quickly, but if you have any trouble, please follow the BitBucket guide. So, log back into your Raspberry Pi terminal, and generate the public/private key pair:

          ssh-keygen                  

And then, starting time the agent:

          ssh-agent /bin/bash                  

And add the cardinal to the amanuensis:

          ssh-add /home/pi/.ssh/id_rsa                  

Now you just need to output the content of the public key:

          cat /domicile/pi/.ssh/id_rsa.pub                  

…so y'all can copy and paste it into BitBucket.

In BitBucket, click on your profile pic and go to Manage account. Under SECURITY, find SSH keys, and click the button Add key.

Clone the Repository

There isn't a convention for where to place the lawmaking of your apps, but y'all tin can create a /var/www directory and put all your projects there.

          cd /var sudo mkdir world wide web                  

To avoid the employ of sudo when you want to place files in the webroot, you lot can change the possessor to your Pi user, and the group to www-information, which is used by Nginx:

          sudo chown -R pi:www-data www cd world wide web                  

Now, y'all can clone the build co-operative of your repository and install the dependencies:

          git clone git@bitbucket.org:USER/REPO.git --branch build --single-branch cd REPO npm install --production                  

Once it's finished, you can beginning your app, setting the environment to production:

          export NODE_ENV=production; node server/app.js &                  

Now, bespeak your reckoner browser to the device IP address to check if it works.

Wish yous had a dev server you could phone call your own? You can, with a #RaspberryPi.

Configure Nginx Contrary Proxy

At that place is one more than step remaining to brand your application attainable from the exterior. Although Nginx is listening on port 80, where it will receive HTTP requests for your Pi, the Node application itself is listening on a different port (for example, port 8080). Therefore, you need to configure Nginx to act as a opposite proxy, recognizing requests intended for your application, and passing them to Node.

Nginx keeps the configuration file for each application information technology serves in the sites-available binder:

          cd /etc/nginx/sites-available/                  

Here, y'all can copy the default configuration file and edit at your convenience:

          sudo cp default my-app sudo nano my-app                  

The concluding configuration file should expect like this, with Nginx acting equally a proxy to the Node.js server:

          server {    listen 80;    root /var/www/my-app/;                  # identifies the location of the application y'all are configuring    server_name my-app.dev;                 # identifies the hostname used by this application'south traffic    location / {       proxy_pass http://localhost:8080/;   # configures the back-end destination for this traffic    } }                  

In order to enable this configuration, y'all need to create a symlink in the sites-enabled folder, where Nginx looks for active configurations during runtime:

          sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/my-app                  

And reload the service to actuate these changes:

          sudo service nginx reload                  

At this point, your application is gear up to receive HTTP traffic intended for the my-app.dev domain (thanks to the server_name my-app.dev directive you configured above). The concluding issue you lot need to solve is how to make traffic you send from outside match this domain name. Although you could buy a domain name and bespeak information technology to your IP, the hosts file comes to the rescue and makes that unnecessary.

On the workstation from which you lot will access the site, simply add your router'southward external IP address, and friction match it with the host name my-app.dev. Any HTTP traffic yous generate for my-app.dev will and then be sent directly to your router, with the correct host name in the Host HTTP header.

On Windows, with administrator privileges, y'all tin edit the file located in c:\windows\system32\drivers\etc\hosts with the notepad. On Linux and Mac you can use the terminal with sudo nano /etc/hosts and sudo nano /private/etc/hosts respectively.

          ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.i       localhost 255.255.255.255 broadcasthost ::1             localhost  212.124.126.242 my-app.dev    # add your host proper noun to the listing                  

What's Next?

Now that everything is set upward, you can deploy as many applications as you want to your Raspberry Pi, and install forever or pm2 to continue your Node.js servers alive.

And simply remember that if something goes wrong, y'all can just wipe the SD card and starting time over again from scratch!

Raspberry Pi development server in action.

dayhuserapposse.blogspot.com

Source: https://www.toptal.com/raspberry-pi/how-to-turn-your-raspberry-pi-into-a-development-server

0 Response to "How to Upload Files to a Raspberry Pi Server"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel