Home Database Mysql Tutorial Deploy a Node.js Application Using MySQL and Prisma on a Raspberry Pi

Deploy a Node.js Application Using MySQL and Prisma on a Raspberry Pi

Jan 06, 2025 am 04:06 AM

Deploy a Node.js Application Using MySQL and Prisma on a Raspberry Pi

Deploying applications has become increasingly accessible, with a range of free and paid hosting options like Render, AWS, and DigitalOcean. However, for developers who want to learn, experiment, and deploy applications without recurring hosting fees, a Raspberry Pi offers an excellent alternative. This compact yet powerful device allows you to create your own Linux-based server for hosting web applications.

In this blog, we’ll explore how to deploy a TypeScript Node.js application using MySQL (MariaDB on Raspberry Pi) and Prisma ORM on a Raspberry Pi. Additionally, we’ll configure NGINX for reverse proxying and use Ngrok to expose the application to the internet. Let’s dive in!


Tools Overview

Raspberry Pi

A low-cost, single-board computer that runs a Linux-based operating system. It’s ideal for creating your own server for IoT or web applications.

Node.js & TypeScript

Node.js is a runtime environment for executing JavaScript on the server, and TypeScript adds static typing to JavaScript, making the codebase more maintainable.

MySQL (MariaDB on Raspberry Pi)

A popular relational database system, MariaDB is a compatible replacement for MySQL and is lightweight enough for a Raspberry Pi.

Prisma ORM

An Object-Relational Mapping (ORM) tool that simplifies database interactions with a type-safe query language and schema migrations.

NGINX

A high-performance HTTP server and reverse proxy server. It helps route traffic to your Node.js application.

Ngrok

A tunneling tool that exposes your locally hosted applications to the internet securely without complex network configurations.


Prerequisites

  1. A working Raspberry Pi - Ensure that SSH is enabled on the Raspberry Pi and you can access it remotely.
  2. Github Repository - Your Node.js TypeScript application should be hosted in a GitHub repository for easy deployment.
  3. Ngrok Account - Create a free account on Ngrok to obtain an auth token for exposing your Raspberry Pi app to the internet.

Setting Up Raspberry Pi

  1. Install the OS

    Setup your Raspberry Pi with an OS like Raspberry Pi OS. Use the Raspberry Pi Imager to find other OS compatible to your Raspberry Pi.

  2. Find the IP Address

    Use a tool like Angry IP Scanner to discover your Raspberry Pi’s IP address. Ensure the Raspberry Pi is connected to the same network as your local machine.

  3. Check Raspberry Pi Status

    ping <IP_ADDRESS_OF_RPI>
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  4. SSH Into the Raspberry Pi

    ssh <username>@<IP_ADDRESS_OF_RPI>
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    Replace with your Raspberry Pi’s username and with the IP address and then enter the password.

  5. Update the System

    sudo apt update && sudo apt upgrade
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  6. Install Git

    Check if git is installed. If not, run the below command to install git

    sudo apt install git
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

Installing Node.js

To install node js, we will be using nvm (Node Version Manager). It allows you to quickly install and use different version of node via command line.

  1. Install NVM

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. Verify Installation

    nvm --version
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  3. Install the Latest LTS Version of Node.js

    nvm install --lts
    
    Copy after login
    Copy after login
    Copy after login
  4. Verify Node.js and npm Installation

    node --version
    # v22.12.0
    
    npm --version
    # 10.9.0
    
    Copy after login
    Copy after login
    Copy after login

Setting Up MySQL (MariaDB)

For Raspberry Pi OS, we will be installing MariaDB.

  1. Install the MariaDB SQL Server

    sudo apt install mariadb-server
    
    Copy after login
    Copy after login
    Copy after login
  2. Secure MariaDB Installation

    sudo mysql_secure_installation
    
    Copy after login
    Copy after login
    Copy after login

    Follow the prompts to secure your database.

    • Enter the current root password: - Press Enter when asked to enter the current password for the root user (since it hasn’t been set yet).
    • Set the root password: - Type n when prompted to set the root password (we’ll set it later).
    • Remove anonymous users: - Type Y to remove anonymous users and improve security. (For testing purposes, you can type n to keep anonymous users.)
    • Disallow root login remotely: - Type n to allow root login remotely (optional but less secure).
    • Remove the test database: - Type y to remove the test database and access to it. (Type n if you want to keep it.)
  3. Login to MariaDB Client

    sudo mysql
    
    Copy after login
    Copy after login
    Copy after login
  4. Setup a Root password for MariaDB

    First, we need to tell the database server to reload the grant tables.

    MariaDB [(none)]> FLUSH PRIVILEGES;
    
    Copy after login
    Copy after login
    Copy after login

    Change the root password with below query.

    MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY '<new_password>';
    
    Copy after login
    Copy after login

    Replace with your own password.

    Use the exit command to exit from MariaDB CLI.

    MariaDB [(none)]> exit;
    Bye
    
    Copy after login
    Copy after login
  5. Login to MariaDB Client With Root User

    ping <IP_ADDRESS_OF_RPI>
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    Enter the password for the root user.

Setting up Database and User

Let's create a new database and a user. We will be granting all privileges to the new user for the new database we have created.

  1. Create a Database

    ssh <username>@<IP_ADDRESS_OF_RPI>
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. Create a New User With Password

    sudo apt update && sudo apt upgrade
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  3. Grant Privilege To New User Created

    sudo apt install git
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  4. Flush The Privileges Table

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  5. Exit from mysql client using exit command.

  6. Login With New User

    nvm --version
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    Enter the password you used while creating the user.

  7. Verify User Can List The Database

    nvm install --lts
    
    Copy after login
    Copy after login
    Copy after login

That’s it! We will use this database and user in our application.


Setup your Node.js Application

  1. Clone Your Github Repository

    node --version
    # v22.12.0
    
    npm --version
    # 10.9.0
    
    Copy after login
    Copy after login
    Copy after login
  2. Navigate To Your Project Repository

    sudo apt install mariadb-server
    
    Copy after login
    Copy after login
    Copy after login
  3. Install Project Dependencies

    sudo mysql_secure_installation
    
    Copy after login
    Copy after login
    Copy after login
  4. Compile TypeScript Code

    sudo mysql
    
    Copy after login
    Copy after login
    Copy after login

Make sure you have configured the outDir property in your tsconfig.json file. This specifies the directory where the compiled JavaScript code will be generated. By default, it’s commonly set to dist, but you can customize it based on your project structure.

Setting up environment variables (Optional)

If your project uses environment variables, you need to set them on your Raspberry Pi. You can create a .env file in the root directory of your project to store all the environment variables.

  1. Create .env File

    MariaDB [(none)]> FLUSH PRIVILEGES;
    
    Copy after login
    Copy after login
    Copy after login
  2. Update .env File

    MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY '<new_password>';
    
    Copy after login
    Copy after login
  3. Enter your Environment Variables

    MariaDB [(none)]> exit;
    Bye
    
    Copy after login
    Copy after login

    Replace the , and with the one you created in the previous steps.

    Save the file by pressing Ctrl O, then press Enter, and exit the editor using Ctrl X.

Migrate Prisma Schema

If you are using Prisma, all the schema files will be located inside the prisma/schema directory. We will now deploy these schemas to the database.

Run the below command

sudo mysql -u root -p
Copy after login

This command will use the DATABASE_URL provided in the .env file to deploy the schemas to the database. You can verify the deployment by logging into the MySQL client and using the command SHOW TABLES; to list all the tables.


Setting Up PM2

PM2 is a production process manager for Node.js applications which helps in managing and keeping the application online. Install PM2 to manage your Node.js application.

ping <IP_ADDRESS_OF_RPI>
Copy after login
Copy after login
Copy after login
Copy after login

Configuring NGINX

  1. Install NGINX

    ssh <username>@<IP_ADDRESS_OF_RPI>
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. Create a Site Configuration

    sudo apt update && sudo apt upgrade
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  3. Add the Below Code

    sudo apt install git
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    Here’s a breakdown of each part:

    listen 80; This directive tells NGINX to listen on port 80, which is the default port for HTTP traffic.

    server_name ; This specifies the domain name or IP address of your Raspberry Pi. Replace with the actual IP address of your Raspberry Pi. NGINX will respond to requests sent to this address.

    location / { ... } This block defines how NGINX should handle requests to the root URL (/). Essentially, this tells NGINX that whenever a request is made to the root, it should be forwarded to the backend (your Node.js application) running on the specified port.

    proxy_pass http://localhost:YOUR_NODE_JS_PORT; This is the key line that forwards incoming requests to your Node.js application. Replace YOUR_NODE_JS_PORT with the actual port where your Node.js app is running (for example, 5000). The requests will be sent to the Node.js application running on the same machine (localhost).

    proxy_http_version 1.1; This sets the HTTP version to 1.1 for the proxy connection, which ensures better handling of certain features like WebSockets.

    proxy_set_header Upgrade $http_upgrade; This header allows WebSocket connections to be upgraded, which is important for real-time applications.

    proxy_set_header Connection 'upgrade'; This header is used alongside the Upgrade header to manage WebSocket connections, ensuring that the connection is properly upgraded from HTTP to WebSocket.

    proxy_set_header Host $host; This passes the original Host header from the client request to the backend server. This is useful for applications that rely on the original Host header (e.g., for routing or virtual hosting).

    proxy_cache_bypass $http_upgrade; This ensures that WebSocket connections bypass any caching mechanisms, allowing real-time communication to work without interference from caching.

    Save the file by pressing Ctrl O, then press Enter, and exit the editor using Ctrl X.

  4. Enable the Site Configuration

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  5. Test NGINX Configuration

    nvm --version
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    If the test is successfull, you will see something like below:

    ping <IP_ADDRESS_OF_RPI>
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  6. Restart NGINX Server To Apply the Changes

    ssh <username>@<IP_ADDRESS_OF_RPI>
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  7. Check NGINX Server Status

    sudo apt update && sudo apt upgrade
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

Running the Application

  1. Navigate to your project

  2. Start Your Application Using PM2

    If you have setup a script in package.json, use the below command:

    sudo apt install git
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    Or, you can directly run you application using index.js file in your dist directory:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    You can also check the logs using below command:

    nvm --version
    
    Copy after login
    Copy after login
    Copy after login
    Copy after login

Now, check your app by entering the IP address of your Raspberry Pi in the browser on your local machine. It should work. Make sure both your local machine and Raspberry Pi are connected to the same network; otherwise, it will not work.


Exposing Your App To The World Using Ngrok

Now that you have deployed your app to the Raspberry Pi, you can only access the app from the same network in which the Raspberry Pi is running. To expose it to the internet, we need to use port forwarding.

You can set up port forwarding using your router settings, but in this case, I will be using ngrok. Ngrok is useful for development, allowing us to run our apps for testing purposes for free.

Make sure to create an account by visiting https://dashboard.ngrok.com/login. You will need the auth token to configure ngrok on the Raspberry Pi.

  1. Install Ngrok

    nvm install --lts
    
    Copy after login
    Copy after login
    Copy after login
  2. Add your auth token to ngrok configuration file

    node --version
    # v22.12.0
    
    npm --version
    # 10.9.0
    
    Copy after login
    Copy after login
    Copy after login
  3. Disable default nginx config file

    sudo apt install mariadb-server
    
    Copy after login
    Copy after login
    Copy after login
  4. Test NGINX configuration

    sudo mysql_secure_installation
    
    Copy after login
    Copy after login
    Copy after login
  5. Restart NGINX server to apply the changes

    sudo mysql
    
    Copy after login
    Copy after login
    Copy after login
  6. Deploy your app online

    MariaDB [(none)]> FLUSH PRIVILEGES;
    
    Copy after login
    Copy after login
    Copy after login

    This should provide a URL like https://xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx.ngrok-free.app/ that forwards traffic to your Node.js app. You can navigate to this URL from any other network and access your application.


Summary

In this guide, we successfully deployed a TypeScript Node.js application with MySQL and Prisma on a Raspberry Pi. We configured NGINX as a reverse proxy and used Ngrok to make the application accessible over the internet. With this setup, you have your own cost-effective, self-hosted development server.

This approach is perfect for learning and experimenting with full-stack application deployment, all while gaining valuable experience in server management.

Let me know if you deploy your application using this guide—I’d love to hear about your experience! ?

The above is the detailed content of Deploy a Node.js Application Using MySQL and Prisma on a Raspberry Pi. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

See all articles