1. TOP
  2. Raspberry Pi
  3. Setup
  4. Initial Setup and Server Construction

日本語 English

Raspberry Pi Initial Setup and Server Construction

* This page contains promotions (Amazon Associates links).

Notes on the steps to make a Raspberry Pi usable as a server for PLC data collection and web display.
From OS initial setup (Wi-Fi, SSH, static IP) to installing Java, FTP, Apache, PHP, Node.js and MariaDB, summarized together with the actual commands to type. You can paste them as is with the "Copy" button at the lower right of each command.

For an overview of the Raspberry Pi itself and how to choose a model, see "What Is a Raspberry Pi".

Environment

ItemDescription
HardwareRaspberry Pi 4 Model B(4GB)
OSRaspberry Pi OS(64-bit)
ConnectionStatic IP on wired LAN (eth0), Wi-Fi for initial setup
How to set a static IP differs by OS version. Bookworm (October 2023 onward) and later use NetworkManager (nmcli); Bullseye and earlier use dhcpcd. You can check your version with cat /etc/os-release.
PR

Equipment used in these steps

  • Raspberry Pi 4 Model B (4GB)The unit these steps were tested on. If buying new, the same steps work on a Pi 5Amazon
  • USB-C power supply (5V/3A)Insufficient power causes SD card corruption and unstable operationAmazon
  • microSD card (32GB or more)A high-endurance card is recommended because the database lives on itAmazon
  • Case (with fan)Heat management, since the DB and web server run constantlyAmazon

As an Amazon Associate, this site earns from qualifying purchases.

Setup

Download

Download Raspberry Pi Imager

https://www.raspberrypi.com/software/

Run the downloaded "Raspberry Pi Imager" and write to a microSD card. Here Raspberry Pi OS (64-bit) is set up on a Raspberry Pi 4. Other OSes such as Ubuntu can also be selected.

Raspberry Pi Imager device
Selcect your Raspberry Pi device
Raspberry Pi Imager operating system
Choose operating system
Raspberry Pi Imager storage device
Select your storage device
Raspberry Pi Imager hostname
Choose hostname

After entering the following items and completing installation, the screen looks like this

Raspberry Pi Imager Write Complete
Write Complete

Workflow

CategoryItemDescription
Software Configuration ToolWi-Fi, locale, SSHWhen changing settings after writing the OS
Static IPAssign a static IP to the wired LAN
Update packagesapt update / upgrade
SoftwareJavaJDK
FTPvsftpd
Apache2Web server
PHPPHP running on Apache
Node.js, npm, Chart.jsJavaScript runtime and graph library
NumPy, MatplotlibNumerical computation and graphs in Python
DatabaseMariaDBInstallation, DB/user creation, external connections
Log outputChecking executed SQL (for debugging)
DB migrationBackup and restore
Visualization (separate pages)MetabaseA BI tool that graphs DB data in the browser (runs on the Raspberry Pi)
Grafana Cloud (free tier)Visualize the Raspberry Pi's DB from Grafana in the cloud (viewable from outside too)

Initial setup

Software Configuration Tool

To configure after writing the OS, you can change settings with raspi-config.

$ sudo raspi-config
Software Configuration Tool menu
Software Configuration Tool menu

Static IP

Set a static IP on the wired LAN (eth0) so PLCs and other PCs can access it at a fixed address. The following addresses are used as an example here. Change them to suit your environment.

ItemSetting example
IP address192.168.0.100/24
Default gateway192.168.0.1
DNS192.168.0.1

Bookworm and later (NetworkManager)Current

First check the name of the connection profile.

$ sudo nmcli connection show
NAME                UUID                                  TYPE      DEVICE
preconfigured       55dd2019-38f9-4b21-b160-d6408043729b  wifi      wlan0
lo                  cff20310-e389-4dd9-b607-b36de92abca9  loopback  lo
Wired connection 1  54d7c64b-aef7-3bde-b00d-249f1e0b383e  ethernet  eth0

The row whose DEVICE is eth0 (Wired connection 1 in this example) is the wired LAN profile. Set a static IP on this profile (a single-line command).

$ sudo nmcli connection modify 'Wired connection 1' ipv4.method manual ipv4.addresses 192.168.0.100/24 ipv4.gateway 192.168.0.1 ipv4.dns 192.168.0.1

Apply the settings.

$ sudo nmcli connection reload
$ sudo nmcli connection up 'Wired connection 1'

Check that the settings were applied.

$ ip -4 addr show eth0
When configuring while connected via SSH: if you run connection up while connected via SSH over the wired LAN, the connection drops when the address changes. Reconnect with the new IP address. Working over Wi-Fi is safer.

Bullseye and earlier (dhcpcd)Older OS

Edit /etc/dhcpcd.conf.

$ sudo nano /etc/dhcpcd.conf
/etc/dhcpcd.conf
# Add the following at the end of the file
interface eth0
static ip_address=192.168.0.100/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
noipv6

Save and reboot.

$ sudo reboot

Updating packages

First update the package list and upgrade installed packages to the latest versions.

$ sudo apt update
$ sudo apt upgrade -y

Software setup

Java

Installing the JDK also installs the JRE (runtime).

$ sudo apt install -y default-jdk
$ java -version
On Bookworm, default-jdk installs Java 17. The latest Metabase requires Java 25, so if you use Metabase, install Java 25 separately as described on the "Metabase Setup" page.

FTP(vsftpd)

Install an FTP server to transfer files from a PC.

$ sudo apt install -y vsftpd

Open the configuration file and remove the leading # from the following lines to enable them.

$ sudo nano /etc/vsftpd.conf
/etc/vsftpd.conf
write_enable=YES
ascii_upload_enable=YES
ascii_download_enable=YES

Restart the service.

$ sudo systemctl restart vsftpd
FTP sends passwords unencrypted. Use it only on closed networks such as the company LAN. If SSH is enabled, you can also use SFTP (encrypted) with WinSCP or similar.

Apache2

$ sudo apt install -y apache2

Open http://192.168.0.100/ in a browser on your PC; if the Apache default page appears, it worked. Put files to publish in /var/www/html/.

PHP

$ sudo apt install -y php
$ php -v

If Apache is installed first, the PHP module for Apache is installed too. To check operation, create the following file and open http://192.168.0.100/info.php in a browser (delete it after checking).

$ echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php

Node.js, npm, Chart.js

$ sudo apt install -y nodejs npm
$ node -v
$ npm -v

Install the graph library Chart.js in the directory of the project that uses it (no sudo needed).

$ cd ~/myproject
$ npm install chart.js
If you only use it in Apache web pages, you can also load it from a CDN (<script src="https://cdn.jsdelivr.net/npm/chart.js">) instead of installing it with npm. In environments without internet access, such as inside a factory, copy node_modules/chart.js/dist/chart.umd.js to /var/www/html/ and use it.

NumPy, Matplotlib

$ sudo apt install -y python3-numpy python3-matplotlib
On Bookworm and later, sudo pip install cannot install packages into the system Python (externally-managed-environment error). When using libraries not available via apt, create a virtual environment (python3 -m venv) and use pip inside it.

MariaDB (MySQL-Compatible Database)

Installation

$ sudo apt install -y mariadb-server

Initial setup

$ sudo mariadb-secure-installation

(Previously mysql_secure_installation; the command name is now the one above)
You are asked the following six questions in order.

QuestionMeaningTest environmentProduction
Switch to unix_socket authenticationWhether to switch to authentication via the Linux usernn
Change the root password?Whether to set or change the DB root passwordnY
Remove anonymous users?Whether to remove anonymous usersnY
Disallow root login remotely?Whether to prevent root login from external PCsnY
Remove test database and access to it?Whether to remove the test database (test)nY
Reload privilege tables now?Whether to apply the settings nowYY

Here, for testing, loose settings are used (almost all "n"). For constant operation on site, set them as in the "Production" column.

Creating the database and user

$ sudo mariadb
MariaDB
-- Create database
CREATE DATABASE testDB;
SHOW DATABASES;                                   -- Check

-- Create user (user: testuser  password: passwd)
CREATE USER 'testuser'@'%' IDENTIFIED BY 'passwd';
SELECT user, host FROM mysql.user;                -- Check

-- Grant privileges (all privileges on testDB)
GRANT ALL PRIVILEGES ON testDB.* TO 'testuser'@'%';
SHOW GRANTS FOR 'testuser'@'%';                   -- Check

EXIT;
'%' means connections are allowed from any host. If privileges on all databases are needed, change testDB.* to *.*. Always change the password to something hard to guess.

Check that you can log in with the created user.

$ mariadb -u testuser -p
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| testDB             |
+--------------------+

Allowing connections from outside (PCs, PLCs)

By default, connections are allowed only from the Raspberry Pi itself (127.0.0.1). Open the configuration file.

$ sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
50-server.cnf
# bind-address = 127.0.0.1   ← comment out by adding # at the start
bind-address = 0.0.0.0        # ← listen on all networks

Restart MariaDB.

$ sudo systemctl restart mariadb

From a PC, you can connect with host 192.168.0.100 and port 3306. When writing directly from a PLC to the DB (such as the OMRON NJ/NX DB connection), use the same address and user.

Log Output (Checking Executed SQL)

Use this when you want to see the SQL actually executed during debugging.

$ sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
50-server.cnf (in the [mysqld] section)
# Log all SQL
general_log_file = /var/log/mysql/mysql.log
general_log      = 1

# Log slow SQL (10 seconds or more)
slow_query_log      = 1
slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time     = 10
$ sudo systemctl restart mariadb
$ sudo tail -f /var/log/mysql/mysql.log
general_log writes out all SQL, so the log grows quickly and shortens microSD card life. When you are done checking, set it back to general_log = 0. Set general_log = 1 only when you need the log.

DB Migration (Backup and Restore)

Steps for migrating a database from another PC or Raspberry Pi.

1. Backup (source)

$ mysqldump -u testuser -p testDB > testDB_backup.sql

2. Create the database (destination)

Create testDB at the destination if it does not exist yet.

$ mysqladmin -u testuser -p create testDB

3. Restore (destination)

Restore with the mysql (mariadb) command, not mysqldump.

$ mysql -u testuser -p testDB < testDB_backup.sql

Summary

Now one Raspberry Pi has a "web server (Apache, PHP)" and a "database (MariaDB)".
All that remains is to read data from the PLC with Python or similar, record it in MariaDB and display it with Metabase or Grafana, and you have a system for checking equipment status from a browser. For how to communicate with PLCs, see the related pages below.

Related pages

What Is a Raspberry Pi?

What is a Raspberry Pi

Introduces the history, specs of current models and PLC integration configurations

OMRON FINS communication

OMRON FINS communication

FINS communication with OMRON products from Python

KEYENCE Host Link communication

KEYENCE Host Link communication

Host Link communication with KEYENCE PLCs from Python

MITSUBISHI MC protocol communication

MITSUBISHI MC protocol communication

MC protocol communication with MITSUBISHI PLCs from Python

Reference

Raspberry Pi is a trademark of Raspberry Pi Ltd.
As an Amazon Associate, this site earns from qualifying purchases.

Comments