Contact Us

Email: info@mohitdesigns.com
Mobile: +91-9718991639

web server on raspberry pi

How to Easily Create a Web Server on Raspberry Pi Zero 2 W – No Expertise Needed!

  • System on chip Broadcom BCM2712, 2.4GHz quadcore 64-bit ARM Cortex-A76 with 512KB L2 caches and a 2MB shared L3 cache
  • 8GB LPDDR4X-4267 SDRAM
  • Dual 4Kp60 HDMI display output with HDR support
8,301.00 INR
Is prime

Setting up a web server can seem challenging, but with Raspberry Pi Zero 2 W and Node.js, you’ll be hosting your website in no time. In this step-by-step guide, we’ll walk you through how to create a web server on raspberry pi zero 2 w using Node.js, and you don’t need to be an expert!

Why Raspberry Pi Zero 2 W?

The Raspberry Pi Zero 2 W is a tiny yet powerful device. Its small size and low power consumption make it ideal for hosting small-scale web servers. With built-in Wi-Fi, it’s the perfect platform for experimenting with web hosting, especially for beginners!

What You’ll Need

Before we jump into the details, make sure you have the following items ready:

  1. Raspberry Pi Zero 2 W
  2. microSD card (8GB or larger)
  3. micro USB power supply
  4. Wi-Fi connection
  5. USB keyboard and monitor (for initial setup)
  6. A computer to configure the Pi remotely (optional)

Step 1: Prepare Your Raspberry Pi Zero 2 W

First, you’ll need to install Raspberry Pi OS on your Pi.

Download and Install Raspberry Pi OS

  1. Visit the Raspberry Pi website and download Raspberry Pi Imager.
  2. Insert the microSD card into your computer and open the Imager tool.
  3. Select Raspberry Pi OS (Lite) – a minimal operating system that’s perfect for running a web server.
  4. Write the image to your microSD card and wait for the installation to finish.

Insert the microSD card into your Raspberry Pi Zero 2 W and power it up!

Enable SSH (Optional but Recommended)

For remote access, enabling SSH will allow you to manage your Raspberry Pi Zero 2 W without needing a keyboard and monitor every time.

  1. After flashing the OS, open the microSD card’s boot directory.
  2. Create an empty file named ssh (no extension required).
  3. Now, SSH will be enabled when the Pi boots up.

Connect to Your Raspberry Pi via SSH

You can now remotely access your Raspberry Pi. Find your Pi’s IP address using your router or a tool like nmap, and then connect using a terminal on your computer:

ssh pi@your_pi_ip_address

The default username is pi and the password is raspberry. (You can change these later for security.)

Step 2: Install Node.js

With SSH access ready, the next step is to install Node.js. Node.js is perfect for running a web server using JavaScript, making it an ideal fit for a small device like the Raspberry Pi.

Install Node.js

1. First, update your package list:

sudo apt update

2. Install Node.js and npm (Node Package Manager):

sudo apt install nodejs npm -y

3. Verify that Node.js is installed correctly by checking the version:

node -v

4. Do the same for npm:

npm -v

Once installed, you are ready to build your web server with Node.js!

Step 3: Create a Simple Node.js Web Server

Now that Node.js is installed, let’s create a basic web server.

1. First, navigate to your home directory:

cd ~

2. Create a directory for your web server:

mkdir my-web-server
cd my-web-server

3. Initialize a new Node.js project:

npm init -y

This creates a package.json file, which manages your project’s dependencies.

4. Create a simple web server using Node.js:

nano server.js

5. In the server.js file, add the following code:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('<h1>Welcome to My Raspberry Pi Zero 2 W Web Server!</h1>');
});

const PORT = 3000;
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});

This script creates a simple web server that listens on port 3000 and serves an HTML response.

6. Save the file and exit the editor.

Step 4: Start Your Web Server

Now, let’s start the web server!

1. Run the Node.js web server by entering the following command:

node server.js

2. Open your browser and navigate to http://your_pi_ip_address:3000. You should see a message that says:

Welcome to My Raspberry Pi Zero 2 W Web Server!

This means your Node.js server is running correctly.

Step 5: Keep Your Server Running with PM2 (Optional but Recommended)

By default, your server will stop running if you close the terminal session. To ensure your Node.js server stays active, even after reboots or disconnections, you can use PM2, a process manager for Node.js.

1. Install PM2 globally:

sudo npm install pm2@latest -g

2. Start your server with PM2:

pm2 start server.js

3. To make PM2 start your server on boot:

pm2 startup
pm2 save

Now your server will restart automatically if the Raspberry Pi reboots!

Conclusion: Web Server on Raspberry Pi Zero 2 W

Congratulations! You’ve successfully created a web server using Node.js on your Raspberry Pi Zero 2 W. This setup is perfect for small projects, learning more about web servers, or even hosting lightweight websites.

With this simple guide, you’ve turned your Raspberry Pi into a powerful little web server. Whether you’re serving static content, building dynamic web apps with Node.js, or experimenting with new ideas, the Raspberry Pi Zero 2 W provides an affordable and accessible platform to get started.

The possibilities are endless, and as you grow more comfortable with this setup, you can explore more advanced configurations like SSL encryption, databases, and custom web apps. Keep experimenting and have fun building!