Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions etc/nbmp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# optional
mem=2048m
# optional
cores=4
# optional port forward
hostfwd=::8181-:80
# optional qmp
qmp_port=4445
# optional serial
serial_port=5556
# optional path to share with guest
share=service/nbmp/files
100 changes: 100 additions & 0 deletions service/nbmp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# NBMP

## Introduction

"NBMP" stands for "NetBSD Bozohttpd Mariadb PHP". It's the equivalent of "LAMP" term for "Linux Apache MariaDB PHP" used for web developpment.

This humble service provides these programs and minimal dependencies to let you do some web developpement, tests, POC, etc... on smolBSD. By default, NBMP uses the last available version of PHP but you can provide the version you want at launch time.

Because of all these dependencies the first launch of a microvm takes some time as well as the start of MariaDB compared with the speed of smolBSD. The next launches are faster except if you want a new version of PHP for an existent microvm : new packages (PHP and dependencies) will take time to dowload and install (see [§Provide PHP version to use](#provide-php-version-to-use)).

Moreover, each launch drops existing databases and loads your own `.sql` files in MariaDB. So, the time to arrival is stretched (see [§No drop databases at launch](#no-drop-databases-at-launch) to avoid it).

But, if you are using an already created microvm without specify PHP version and without delete databases, the boot is relatively fast !

`www` and`.sql` files are stored in a local folder on your filesystem. It is mounted on the microvm to `/mnt` and bozohttpd has his `slashdir` (like Apache "DocumentRoot" folder) on it. Consequently, every file you put/change/delete into it, is instantly put/changed/deleted into the microvm.

> [!CAUTION]
> Do not use this service for prod. No security stuff are set.

## Prerequisites
NBMP stack offered by this service provides packages :
- php
- php-curl
- php-mysqli
- php-pdo
- php-pdo_mysql
- mariadb-server
- mariadb-client
- bozohttpd

Also, feel free to edit `etc/nbmp.conf` file as needed.

Concerning your web developpement files :
- put your `.sql` files in the `service/nbmp/files/sql` folder
- put your `www` files (`.php`, `.html`, `.css`, etc...) in `service/nbmp/files/www`

`index.php`, `info.php` as well as `.sql` scripts coming with this service stand for example and can be deleted.

## Usage
Building on GNU/Linux or MacOS
```sh
$ bmake SERVICE=nbmp build
```
Building on NetBSD
```sh
$ make SERVICE=nbmp base
```

Start the service:
```sh
./startnb.sh -f etc/nbmp.conf -d
```

Finally, go to [http://localhost:8181](http://localhost:8181) and enjoy!


<div align="center" markdown="1"><img src="capture.png" /></div>
This is the example index page.


## To go further
### System console
You can have access to the system console keeping microvm foreground. Don't use `-d` option (for "daemonize") with `./startnb.sh`. After shutdown or to quit the microvm, press `Ctrl+a x`. Avoid quitting microvm before system shuts down correctely, otherwise the next boot will performs auto repaire actions on filesystem and MariaDB does not appreciate that.

If the microvm is launched via [smolBSD VM Manager](https://github.com/NetBSDfr/smolBSD/tree/main/app), you can clic on the terminal icon to get access to the console. Otherwise, or if it launched "daemonized", you can use :

```sh
telnet localhost 5556
```

See `etc/nbmp.conf` file if you want to change `serial_port` value.

> [!IMPORTANT]
> Only one connection at a time is permit.

### MariaDB console
For your application dev/debugging sessions, use the microvm console to get access to the MariaBD console with `mariadb` command. This connection is made with the unix socket, so no password is required.

### Logs
If you want to check logs from MariaDB and PHP, go to `/mnt/logs` folder into microvm or `service/nbmp/files/logs` into your filesystem. It's finally the same location : one is mounted on the other.

MariaDB logs are stored in `/mnt/logs/mariadb.log` into microvm or `service/nbmp/files/logs/mariaDB.log` into your filesystem. To change it, edit `/etc/rc.d/mariadb` to modify `--log-error` value.

PHP errors are displayed in the web pages and written into the `/mnt/logs/php.log` file into microvm or `service/nbmp/files/logs/php.log` into your filesystem. To change it, edit `display_errors` and `error_log` variables in `/usr/pkg/etc/php/8.4/php.ini`.

For theses logs behaviors, if you want your changes to be persistent for each microvm creation, edit `service/nbmp/etc/rc` file.

### Provide PHP version to use
See [http://cdn.netbsd.org/pub/pkgsrc/packages/NetBSD/x86_64/11.0/All/](http://cdn.netbsd.org/pub/pkgsrc/packages/NetBSD/x86_64/11.0/All/) to check PHP version you want and :
```sh
./startnb.sh -f etc/nbmp.conf -d -e PHP=8.3
```

### No drop databases at launch
Each microvm launch drops all databases by default. If you want to keep existing data, use :
```sh
./startnb.sh -f etc/nbmp.conf -d -e nodeletesql=yes
```

Service made with ❤.
Binary file added service/nbmp/capture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 130 additions & 0 deletions service/nbmp/etc/rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/sh
. /etc/include/basicrc
. /etc/include/choupi
. /etc/include/mount9p
. /etc/include/pkgin

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/pkg/bin:/usr/pkg/sbin
export TERM=tmux-256color
sqldir="/mnt/sql"
wwwdir="/mnt/www"
wwwroot="/var/www"
logsdir="/mnt/logs"


if [ -d ${logsdir} ]; then
rm -fr ${logsdir}/*
else
mkdir ${logsdir}
fi

hostname nbmp

# bash improvements.
if [ ! -f /root/.bashrc ]; then
cat >>/root/.bashrc<<EOF
PS1="\u@\h:\w # "
alias ll='ls -al'
EOF
fi

# Search of the last PHP version in the NetBSD repository if no "PHP"
# variable is given with startnb.sh.
if [ ! -n "${PHP}" ]; then
echo -n "${INFO} no PHP version provided. Using PHP "
# Output : "php84-8.4.16"
PHP_FULL=$(pkgin search php | grep -E '^php[0-9]*-[0-9]' | tail -1 | cut -d " " -f 1)

# Output : "8.4"
PHP=$(echo ${PHP_FULL%.*} | cut -d "-" -f 2)

# Output : "84"
PHP_NO_DOT=$(echo ${PHP_FULL#php} | cut -d "-" -f 1)
else
echo -n "${INFO} using PHP v"
PHP_NO_DOT=$(echo ${PHP} | tr -d ".")
fi

echo "${PHP}"

PKGS="curl mariadb-server mariadb-client bozohttpd"
PKGS_PHP="php${PHP_NO_DOT} php${PHP_NO_DOT}-mysqli php${PHP_NO_DOT}-curl php${PHP_NO_DOT}-pdo php${PHP_NO_DOT}-pdo_mysql"

# If mariadb user does not exist, this means that the configuration has not been done.
# So, let's do it now. Otherwise, let's continue.
if ! id mariadb >/dev/null 2>&1; then
echo "${ARROW} Installing other packages "
pkgin -y install $PKGS

echo "${ARROW} MariaDB server settings"
cp /usr/pkg/share/examples/rc.d/mariadb /etc/rc.d/mariadb
echo "mariadb=yes" >> /etc/rc.conf
# Logs destination.
sed -i'' 's#--log-error=/var/log/mariadb/error.log#--log-error='${logsdir}/mariadb.log'#g' /etc/rc.d/mariadb


echo "${ARROW} Bozohttpd settings"
rmdir ${wwwroot}
ln -snf ${wwwdir} ${wwwroot}
fi

# Management of PHP with version given by startnb.sh if any.
if [ ! -d /usr/pkg/etc/php/${PHP} ]; then
echo "${ARROW} PHP settings ..."
pkgin -y install ${PKGS_PHP}
pkgin clean
# Logs destination.
sed -i'' 's/display_errors = Off/display_errors = On/g' /usr/pkg/etc/php/${PHP}/php.ini
sed -i'' 's#;error_log = syslog#error_log = '${logsdir}'/php.log#g' /usr/pkg/etc/php/${PHP}/php.ini
fi

echo "${ROCKET} MariaDB launching..."
/etc/rc.d/mariadb start >/dev/null 2>&1
exitCode=$?

# "sleep", otherwhise, the next test happens too fast : MariaDB start slowly.
while [ ! -e "/tmp/mysql.sock" ]; do
sleep 0.1
done

if [ ${exitCode} -ne 0 ]; then
echo -e "${ERROR} It seems MariaDB has fail to launch.\nExit\n\n"
return -1
fi

if [ "${nodeletesql}" = "yes" ]; then
echo "${INFO} use 'nodeletesql' -> do nothing with sql..."
else
echo "${INFO} drop existing databases..."
dbs=$(mariadb -B -s -e "show databases" | grep -vE "^(information_schema|performance_schema|sys|mysql|test)$")
for db in ${dbs}; do
mariadb -e "drop database ${db}"
done

echo "${INFO} loading sql files... "
for sql in ${sqldir}/*.sql; do
if [ ! -f "${sql}" ]; then echo "${WARN} no sql to load ${WARN}"; break; fi
echo -e "\t${ARROW} processing '$(basename $sql)'"
mariadb < $sql
done
fi

echo "${ROCKET} Bozohttpd launching..."
/usr/libexec/bozohttpd -b -x index.php -C .php /usr/pkg/libexec/cgi-bin/php${PHP_NO_DOT} ${wwwdir}
# No log management for bozohttpd. The only way to have it, is to start
# with "-d" ("debug") and "-f" ("foreground") options to see logs in stdin.
# bozohttp use syslog but it's not installed into smolBSD. Moreover,
# "bozohttpd is designed to be small, simple and relatively featureless,
# hopefully increasing its security." - From bozohttpd(8) man page

exitCode=$?
if [ ${exitCode} -ne 0 ]; then
echo -e "${ERROR} It seems bozohttpd has fail to launch.\nExit\n\n"
return -1
fi

echo -e "\n\n${ROCKET} Ready to be used !\n(use http://localhost:8181 by default to see your web pages)\n\n"

bash --rcfile /root/.bashrc

. /etc/include/shutdown
43 changes: 43 additions & 0 deletions service/nbmp/files/sql/example-bug_tracker.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE DATABASE IF NOT EXISTS bug_tracker;
USE bug_tracker;

CREATE TABLE developers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
favorite_excuse VARCHAR(100)
) ENGINE=InnoDB;

CREATE TABLE projects (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
deadline DATE
) ENGINE=InnoDB;

CREATE TABLE bugs (
id INT AUTO_INCREMENT PRIMARY KEY,
project_id INT,
developer_id INT,
title VARCHAR(100),
severity VARCHAR(20),
status VARCHAR(20),
comment TEXT,
CONSTRAINT fk_bug_project FOREIGN KEY (project_id) REFERENCES projects(id),
CONSTRAINT fk_bug_developer FOREIGN KEY (developer_id) REFERENCES developers(id)
) ENGINE=InnoDB;

INSERT INTO developers (name, favorite_excuse) VALUES
('Alice', 'Works on my machine'),
('Bob', 'Probably a cache issue'),
('Charlie', 'Let’s rewrite it in Rust');

INSERT INTO projects (name, deadline) VALUES
('NextGen Website', '2025-12-31'),
('Mobile App v2', '2024-06-01');

INSERT INTO bugs (project_id, developer_id, title, severity, status, comment) VALUES
(1, 1, 'Quantum Bug', 'Critical', 'Open',
'Bug disappears when debugger is attached.'),
(1, 2, 'Random Null Pointer', 'Major', 'In Progress',
'Happens only on Fridays after 4pm.'),
(2, 3, 'UI Misaligned by 1px', 'Minor', 'Closed',
'Caused a 2-hour meeting.');
43 changes: 43 additions & 0 deletions service/nbmp/files/sql/example-infrastructure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE DATABASE IF NOT EXISTS infrastructure;
USE infrastructure;

CREATE TABLE data_centers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
location VARCHAR(50)
) ENGINE=InnoDB;

CREATE TABLE servers (
id INT AUTO_INCREMENT PRIMARY KEY,
data_center_id INT,
hostname VARCHAR(50),
os VARCHAR(50),
status VARCHAR(30),
uptime_days INT,
CONSTRAINT fk_server_dc FOREIGN KEY (data_center_id) REFERENCES data_centers(id)
) ENGINE=InnoDB;

CREATE TABLE incidents (
id INT AUTO_INCREMENT PRIMARY KEY,
server_id INT,
incident_type VARCHAR(50),
description TEXT,
CONSTRAINT fk_incident_server FOREIGN KEY (server_id) REFERENCES servers(id)
) ENGINE=InnoDB;

INSERT INTO data_centers (name, location) VALUES
('Main DC', 'Unknown (classified)'),
('Backup DC', 'Basement under the stairs');

INSERT INTO servers (data_center_id, hostname, os, status, uptime_days) VALUES
(1, 'prod-server-01', 'Linux', 'Running', 487),
(1, 'legacy-monolith', 'Windows Server 2008', 'Undead', 3920),
(2, 'test-server', 'Linux', 'Crying softly', 3);

INSERT INTO incidents (server_id, incident_type, description) VALUES
(2, 'Reboot Attempt',
'Server refused to reboot and threatened to break production.'),
(3, 'Disk Full',
'Disk filled by log files nobody reads.'),
(1, 'Network Issue',
'Fixed by unplugging and plugging the cable back in.');
44 changes: 44 additions & 0 deletions service/nbmp/files/sql/example-security_system.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE DATABASE IF NOT EXISTS security_system;
USE security_system;

CREATE TABLE roles (
id INT AUTO_INCREMENT PRIMARY KEY,
role_name VARCHAR(50),
risk_level VARCHAR(20)
) ENGINE=InnoDB;

CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
password_hash VARCHAR(100),
role_id INT,
last_password_change DATE,
CONSTRAINT fk_user_role FOREIGN KEY (role_id) REFERENCES roles(id)
) ENGINE=InnoDB;

CREATE TABLE login_attempts (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
attempt_time DATETIME,
success TINYINT(1),
note TEXT,
CONSTRAINT fk_login_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB;

INSERT INTO roles (role_name, risk_level) VALUES
('Admin', 'Extremely High'),
('Developer', 'High'),
('Intern', 'Terrifying');

INSERT INTO users (username, password_hash, role_id, last_password_change) VALUES
('admin', 'admin', 1, '2013-01-01'),
('john.dev', 'P@ssw0rd', 2, '2024-02-15'),
('intern42', 'azerty', 3, '2025-01-01');

INSERT INTO login_attempts (user_id, attempt_time, success, note) VALUES
(1, NOW(), 1,
'Logged in successfully. Security team cried quietly.'),
(3, NOW(), 0,
'Caps Lock was ON. Again.'),
(2, NOW(), 1,
'Authenticated after resetting password for the 5th time this month.');
Loading