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
| Item | Description |
|---|---|
| Hardware | Raspberry Pi 4 Model B(4GB) |
| OS | Raspberry Pi OS(64-bit) |
| Connection | Static IP on wired LAN (eth0), Wi-Fi for initial setup |
cat /etc/os-release.
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.




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

Workflow
| Category | Item | Description |
|---|---|---|
| Software Configuration Tool | Wi-Fi, locale, SSH | When changing settings after writing the OS |
| Static IP | Assign a static IP to the wired LAN | |
| Update packages | apt update / upgrade | |
| Software | Java | JDK |
| FTP | vsftpd | |
| Apache2 | Web server | |
| PHP | PHP running on Apache | |
| Node.js, npm, Chart.js | JavaScript runtime and graph library | |
| NumPy, Matplotlib | Numerical computation and graphs in Python | |
| Database | MariaDB | Installation, DB/user creation, external connections |
| Log output | Checking executed SQL (for debugging) | |
| DB migration | Backup and restore | |
| Visualization (separate pages) | Metabase | A 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
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.
| Item | Setting example |
|---|---|
| IP address | 192.168.0.100/24 |
| Default gateway | 192.168.0.1 |
| DNS | 192.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 eth0The 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.1Apply the settings.
$ sudo nmcli connection reload $ sudo nmcli connection up 'Wired connection 1'
Check that the settings were applied.
$ ip -4 addr show eth0connection 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# 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
noipv6Save and reboot.
$ sudo rebootUpdating 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
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 vsftpdOpen the configuration file and remove the leading # from the following lines to enable them.
$ sudo nano /etc/vsftpd.confwrite_enable=YES ascii_upload_enable=YES ascii_download_enable=YES
Restart the service.
$ sudo systemctl restart vsftpdApache2
$ sudo apt install -y apache2Open 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.phpNode.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
<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-matplotlibsudo 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-serverInitial 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.
| Question | Meaning | Test environment | Production |
|---|---|---|---|
| Switch to unix_socket authentication | Whether to switch to authentication via the Linux user | n | n |
| Change the root password? | Whether to set or change the DB root password | n | Y |
| Remove anonymous users? | Whether to remove anonymous users | n | Y |
| Disallow root login remotely? | Whether to prevent root login from external PCs | n | Y |
| Remove test database and access to it? | Whether to remove the test database (test) | n | Y |
| Reload privilege tables now? | Whether to apply the settings now | Y | Y |
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-- 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# 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 mariadbFrom 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# 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 = 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.sql2. Create the database (destination)
Create testDB at the destination if it does not exist yet.
$ mysqladmin -u testuser -p create testDB3. Restore (destination)
Restore with the mysql (mariadb) command, not mysqldump.
$ mysql -u testuser -p testDB < testDB_backup.sqlSummary
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
Introduces the history, specs of current models and PLC integration configurations

OMRON FINS communication
FINS communication with OMRON products from Python

KEYENCE Host Link communication
Host Link communication with KEYENCE PLCs from Python

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.
