Setting Up a Raspberry Pi Server - ECE-180D-WS-2024/Wiki-Knowledge-Base GitHub Wiki

Intro

In the vast landscape of microcontrollers and IOT devices, the Raspberry Pi is a versatile powerhouse. One of its most popular applications is as a server, where it can host websites, control other IOT devices, and much more! If you have an application that needs to run 24/7 or interface with other IOT devices, a Raspberry Pi server can be invaluable. Running applications on the Raspberry Pi server takes away the headache of keeping your computer running all the time or having your web application down when you don’t have a device available. The Raspberry Pi can also be found on popular sites like Amazon for as little as $25 (depending on the model you purchase), making it a cheap and easily accessible IOT solution. Let’s take a look at how to set up our Raspberry Pi server and some example uses!

Setup

The Raspberry Pi is a compact computer that runs a Linux-based operating system called Raspian OS. We can use any Raspberry Pi model, but the computing power and memory size for different models will vary. This tutorial uses the Raspberry Pi 3. The SSH server we set up will allow other devices on the same WiFi network as the Raspberry Pi to interface with the RasPi and send files/run scripts on the RasPi.

Download the Raspberry Pi OS

  1. We first image the Raspian OS onto a microSD card. The microSD card should be at least 32 GB so that there is adequate space for both the OS and your application files. The OS can be loaded onto the microSD card using the Raspberry Pi Imager software [1]
  1. Insert the microSD card into the microSD slot on the RasPi and turn it on for your first boot! You should be prompted with a window to set up WiFi and other settings. Make sure to check “Enable SSH” in the “Services” tab for our web server

Update Your RasPi and Set up SSH Connection

  1. Verify your RasPi’s internet connection and open up a command terminal. Run these commands in the terminal:
    sudo apt update
    sudo apt upgrade
    sudo apt install openssh-server
    
    These commands will update your RasPi system and install the OpenSSH server package [2] which we will use to set up our SSH server. The SSH server should already be running after installing the package and we can verify this by the command
    sudo service ssh status
    
    If the server is not running, start it up with
    sudo service ssh start
    
  2. Once the server is running, check its IP address using the command
    hostname -I
    

Interface With the RasPi Terminal on Another Device

  1. On another device connected to the same WiFi network, open a terminal and use this command to connect to the RasPi via its IP address
    ssh pi@<RASPI_ADDRESS_HERE>
    
    • Replace <RASPI_ADDRESS_HERE> with the correct IP address. You will be prompted to enter the SSH password you set when you first booted the RasPi
  1. Congratulations! You are now connected to the RasPi’s terminal and can interact with it remotely from your personal device as long as you are connected to the same WiFi network and the RasPi is powered on.

Transfer Files from Another Device to RasPi

Ok, now we can run scripts remotely on the RasPi terminal from our personal device! But how do we get our scripts/applications onto the RasPi remotely? We can use a variety of file transfer applications such as WinSCP, Cyberduck, or FileZilla. For this part of the tutorial, we will use WinSCP but a similar procedure applies for the other file transfer applications.

  1. On WinSCP, configure a new session with the following parameters

    • File protocol: SFTP
    • Host name: <RASPI_ADDRESS_HERE>
    • Port number : 22
    • Username: Your RasPi username (usually ‘pi’ by default)
    • Password: Your RasPi password

    You can save this session so that you do not have to retype these parameters when you connect to the RasPi in the future. After saving the session, click login to connect to the RasPi

  1. The WinSCP interface should have two panels: one panel represents the RasPi directory while the other panel represents the directory on your current device. You can freely navigate the directories on either panel and drag files from your local directory panel to the RasPi panel to transfer them.
  1. Now you are able to transfer files from your device to the RasPi remotely! After transferring the necessary files, click “Disconnect” on WinSCP and SSH into the RasPi using a terminal window. We can now run the transferred files in the Raspi terminal.

Use Cases

Home automation[3]:

If you have many IOT devices on the same WiFi network as the RasPi, they can be managed via a RasPi server. You can write a script in Python that uses terminal commands to send instructions or gather data from each IOT device (using their IP addresses). Some devices that can be managed by the RasPi include smart lights, home thermostats, sound systems, and door locks. Using the RasPi as a control hub allows us to interface with all these devices from a single location. Running this script on the RasPi also means that our control system will always be online, and we don’t have to worry about keeping the script running on our personal computer.

File server[4]:

Even simpler than running scripts/programs, the RasPi can be used as a generic file server. Assuming a sufficiently large microSD card or external storage device connected to the RasPi, the RasPi can be used to store and share files between devices on the local network. Other devices can connect to the RasPi on file transfer applications like WinSCP and pull the desired files into their local directory. We can even write a script that tells the RasPi to constantly scan identical directories across multiple devices and keep a most up-to-date directory in its local storage. This essentially makes the RasPi a file version control server that can synchronize files across many devices.

Personal Website[5]:

The RasPi can be used to host a website via various web server packages (e.g. Apache, Nginx). There are many tutorials on how to instantiate the web server, move relevant HTML/CSS/JS files into the web server directory, then access the website on your local network. This website can be used as an interface for your home automation control hub, file server, or other applications. You can also make your website accessible on the public internet by configuring your WiFi router to forward external requests to the RasPi’s local IP address. Doing this would allow you to send commands to your RasPi through the website even when your device is not on the same local network.

Introduction to Docker on Raspberry Pi [6]:

While the Raspberry Pi is already a powerful tool for hosting servers and managing IoT devices, its capabilities can be significantly enhanced by using Docker. Docker is a platform that allows you to package applications into containers—lightweight, standalone, and executable packages that include everything needed to run the software, such as code, runtime, system tools, libraries, and settings. This containerization ensures that applications run consistently across different environments. By using Docker on your Raspberry Pi, you can run multiple applications simultaneously, each in its own isolated environment, preventing conflicts between applications. Additionally, Docker simplifies the process of deploying, managing, and scaling applications, making it easier to handle complex projects. This setup is particularly useful for developers who need to maintain a consistent development environment or for those looking to deploy a variety of applications on a single Raspberry Pi. The flexibility and efficiency provided by Docker extend the potential uses of the Raspberry Pi, transforming it into a versatile and powerful server platform.

Benefits of Using Docker on Raspberry Pi:

  • Isolation: Each application runs in its own container, ensuring that it doesn’t interfere with other applications.
  • Portability: Docker containers can be easily moved between different environments, making deployment and scaling straightforward.
  • Efficiency: Containers are lightweight and start quickly, using less memory and resources than virtual machines.

Using Docker on your Raspberry Pi opens up a vast array of possibilities, allowing you to efficiently run and manage multiple applications in an isolated environment. Whether you're setting up a web server, deploying a database, or launching any other service, Docker provides a robust and flexible solution that simplifies deployment and management. The containerization offered by Docker ensures consistent performance and easy scalability, making it an ideal choice for both development and production environments. This versatility transforms your Raspberry Pi into a powerful and adaptable server capable of handling a wide range of tasks.

Docker Setup

Setting Up Docker on Your Raspberry Pi

  1. Prepare Your Raspberry Pi, first ensure your Raspberry Pi is updated:
    sudo apt update
    sudo apt upgrade
    
  2. Now install docker by running the following command:
    curl -sSL https://get.docker.com | sh
    
  3. Add Your User to the Docker Group, this allows you to run Docker commands without using sudo:
    sudo usermod -aG docker pi
    
  4. Verify Docker Installation. Log out and back in, then verify the installation by running:
    docker --version
    

Running a Web Server in a Docker Container

  1. Pull a Docker Image. For this example, we will use the nginx web server. Pull the official Nginx image from Docker Hub:
    docker pull nginx
    
  2. Run the Nginx Container. Start a container using the Nginx image:
    docker run --name my-nginx -p 80:80 -d nginx
    
  3. Access the Web Server. Open a web browser and navigate to your Raspberry Pi's IP address. You should see the Nginx welcome page.

For reference you can find the official Nginx image on Docker Hub [7]

Managing Docker Containers

  1. List Running Containers. To see a list of all running containers, use:
    docker ps
    
  2. Stop a Container. To stop the Nginx container:
    docker stop my-nginx
    
  3. Remove a Container. To remove the stopped Nginx container:
    docker rm my-nginx
    

Conclusion

Altogether, the Raspberry Pi is a compact, powerful solution for server-based projects. It is also easily scalable with other RaspPis/IOT devices, which greatly expands its potential uses and capabilities. People have performed advanced projects, such as daisy-chaining RasPi servers to extend WiFi coverage or distributing control tasks between a network of RasPis. The possibilities are truly endless, which is what makes developing RasPi server applications so exciting.

References

[1] https://www.raspberrypi.com/software/

[2] https://www.openssh.com/

[3] https://www.electromaker.io/blog/article/9-best-raspberry-pi-smart-home-software-options/

[4] https://raspberrytips.com/raspberry-pi-file-server/

[5] https://www.instructables.com/Host-your-website-on-Raspberry-pi/

[6] https://docs.docker.com/get-started/overview/

[7] https://hub.docker.com/_/nginx