diff --git a/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot b/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot index d1a007f9..d3e6a0d0 100644 --- a/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot +++ b/debian-live-config/config/hooks/normal/9999-wrolpi.hook.chroot @@ -60,6 +60,9 @@ echo "Controller pre-installed" # Configure nginx. cp /opt/wrolpi/etc/raspberrypios/nginx.conf /etc/nginx/nginx.conf cp /opt/wrolpi/etc/raspberrypios/50x.html /var/www/50x.html +# Copy wrolpi.conf which defines the location blocks for reverse proxy. +[ -f /etc/nginx/conf.d/default.conf ] && rm /etc/nginx/conf.d/default.conf +cp /opt/wrolpi/etc/raspberrypios/wrolpi.conf /etc/nginx/conf.d/wrolpi.conf # WROLPi needs a few privileged commands. cp /opt/wrolpi/etc/raspberrypios/90-wrolpi /etc/sudoers.d/90-wrolpi @@ -98,6 +101,8 @@ systemctl enable NetworkManager # Enable iperf3 for speed testing. systemctl enable iperf3 +# Enable first startup script to generate certificates and start nginx. +systemctl enable wrolpi-first-startup.service # Enable Controller service to start on boot. systemctl enable wrolpi-controller.service diff --git a/pi-gen/stage2/04-wrolpi/03-run-chroot.sh b/pi-gen/stage2/04-wrolpi/03-run-chroot.sh index a0248fbc..6ddb615b 100755 --- a/pi-gen/stage2/04-wrolpi/03-run-chroot.sh +++ b/pi-gen/stage2/04-wrolpi/03-run-chroot.sh @@ -62,6 +62,9 @@ chown -R wrolpi:wrolpi /opt/wrolpi # Configure nginx. cp /opt/wrolpi/etc/raspberrypios/nginx.conf /etc/nginx/nginx.conf cp /opt/wrolpi/etc/raspberrypios/50x.html /var/www/50x.html +# Copy wrolpi.conf which defines the location blocks for reverse proxy. +[ -f /etc/nginx/conf.d/default.conf ] && rm /etc/nginx/conf.d/default.conf +cp /opt/wrolpi/etc/raspberrypios/wrolpi.conf /etc/nginx/conf.d/wrolpi.conf # WROLPi needs a few privileged commands. cp /opt/wrolpi/etc/raspberrypios/90-wrolpi /etc/sudoers.d/90-wrolpi diff --git a/repair.sh b/repair.sh index 1142ebab..eb2cb962 100755 --- a/repair.sh +++ b/repair.sh @@ -46,12 +46,7 @@ cp /opt/wrolpi/etc/raspberrypios/wrolpi.conf /etc/nginx/conf.d/wrolpi.conf cp /opt/wrolpi/etc/raspberrypios/50x.html /var/www/50x.html # Generate nginx certificate for HTTPS. -if [[ ! -f /etc/nginx/cert.crt || ! -f /etc/nginx/cert.key ]]; then - openssl genrsa -out /etc/nginx/cert.key 2048 - openssl req -new -x509 -nodes -key /etc/nginx/cert.key -out /etc/nginx/cert.crt -days 3650 \ - -subj "/C=US/ST=State/L=City/O=Org/OU=WROLPi/CN=$(hostname).local" - chmod 640 /etc/nginx/cert.key /etc/nginx/cert.crt -fi +/opt/wrolpi/scripts/generate_certificates.sh # Start nginx quickly so user can access Controller from React UI systemctl start nginx || : diff --git a/scripts/generate_certificates.sh b/scripts/generate_certificates.sh new file mode 100755 index 00000000..e205bf60 --- /dev/null +++ b/scripts/generate_certificates.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Generate self-signed SSL certificates for nginx if they don't exist. + +set -e + +CERT_FILE="/etc/nginx/cert.crt" +KEY_FILE="/etc/nginx/cert.key" + +if [[ -f "${CERT_FILE}" && -f "${KEY_FILE}" ]]; then + echo "Certificates already exist at ${CERT_FILE} and ${KEY_FILE}" + exit 0 +fi + +echo "Generating nginx certificates..." +openssl genrsa -out "${KEY_FILE}" 2048 +openssl req -new -x509 -nodes -key "${KEY_FILE}" -out "${CERT_FILE}" -days 3650 \ + -subj "/C=US/ST=State/L=City/O=Org/OU=WROLPi/CN=$(hostname).local" +chmod 640 "${KEY_FILE}" "${CERT_FILE}" +echo "Certificates generated successfully." diff --git a/wrolpi/scripts/first_startup.sh b/wrolpi/scripts/first_startup.sh index 3e24f138..f432a488 100755 --- a/wrolpi/scripts/first_startup.sh +++ b/wrolpi/scripts/first_startup.sh @@ -1,5 +1,14 @@ #!/usr/bin/env bash -# This script is run once on the first startup of a Raspberry Pi. +# This script is run once on the first startup of a Raspberry Pi or Debian Live. + +set -x + +# Generate nginx certificate for HTTPS if it doesn't exist. +/opt/wrolpi/scripts/generate_certificates.sh + +# Enable and start nginx now that certificates exist. +systemctl enable nginx +systemctl start nginx || echo "Warning: nginx failed to start" # Copy skeleton files to first users home directory. USER_HOME=$(getent passwd 1000 | cut -d: -f6)