2

Is there a way to cause noobs to install apache and php during setup, or perhaps leave a script which will do that upon first boot? And then copy info into /var/www? Basically what I want is a way to get a headless pi to setup a webserver with no input from me once I have put in the sd card.

1

I would recommend FooDeas/raspberrypi-ua-netinst as the base for making your installer. Download a released zip, configure a few files, put it on the SD Card, put the card in your Pi and it will boot up and install however you choose without needing any further intervention.

I have written a blog post here that explains how to turn it into an installer that can do anything you want. (By showing how to automatically install NodeJS). There are a number of ways raspberrypi-ua-netinst can be made to perform complex installations but I only explore one method fully (both here and in my blog).

The raspberrypi-ua-netinst installer, as it is, is slightly limited. As it is, you can easily do the following:

  • Supply a list of packages which it installs with apt-get
  • Supply a set of files which will be loaded onto the final image.
  • Provide (limited) scripts to run during the install, prior to first boot.

Both are explained at its homepage on github.

The biggest limitation is that the installer (and scripts you choose to run with it, such as post-install.txt) are done so on busybox which is a minimal shell with few tools.

There is a discussion here on how to take the installer further here. The user "netdesk" explains why the installer by itself is slightly limited, and solutions are explored. Please note the discussion is for raspbian-ua-netinst, not raspberrypi-ua-netinst. The latter is the one I prefer, as it has some useful extra features - the former is the original. But they are almost identical in how they work.

My blog article gives one possible solution: it explains how to bundle a .bashrc file with the installer, and some systemd service files that do the following:

  1. The systemd files ensure that when the installer is done and reboots that the system automatically logs in.
  2. The .bashrc file ensures that when it logs in that first time, it performs the remaining installation that you choose in the full Raspbian rather than busybox, which makes it a whole lot easier.

You can monitor the entire install process via the UART if you connect a serial-to-USB converter to the Pi's UART GPIO pins and the USB of your PC or similar (using Putty if you're on windows). The systemd files make it log in as root with input/output via the UART so you can easily interact with it without having to rely on a working SSH connection.

Just change the contents of on-first-login.sh to perform the remaining configuration of your system, eg install and configure Apache etc. You can bundle your files for /var/www with the installer files directly on the SD card.

In case my blog goes down or whatever, the key to my method is you create a file with this in it:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# Modified from /lib/systemd/system/serial-getty@.service

[Unit]
Description=Serial Getty on %I
Documentation=man:agetty(8) man:systemd-getty-generator(8)
Documentation=http://0pointer.de/blog/projects/serial-console.html
BindsTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

[Service]
# ExecStart=-/sbin/agetty --keep-baud 115200,38400,9600 %I $TERM
ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud 115200,38400,9600 %I $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes

[Install]
WantedBy=getty.target

And ensure this gets put in two places in the final installation:

/etc/systemd/system/serial-getty@ttyAMA0.service 
/etc/systemd/system/getty.target.wants/serial-getty@ttyAMA0.service 

Then just create some script, and in /root/.bashrc call your script, and ensure the script will not get called in future.

There are other possible solutions; in the issue discussion, one person explains how he made his own repository and package which he configured the installer to use. He also posted a link to the source. And there are one or two other suggestions as well.

My method isn't perfect, but for the sake of keeping things short I will leave it as it is - it will get you off the ground. Mainly I suggest reviewing secirity since everything is done as root. But please do let me know if you have any issues or suggestions.

0

Yes, there is.

Script

The location /etc/init.d is where you want to put your script in and also you should make it executable, otherwise it wouldn't work. Write the script in /etc/init.d/yourscript. Make it executable by chmod +x /etc/init.d/yourscript or chmod 775 /etc/init.d/yourscript. You might also need to type sudo before the codes I've written above. Then check this tutorial for writing your script: http://linuxconfig.org/bash-scripting-tutorial . I suggest you to put some sleep between codes (its trivial) so no problem would occur.

Autologin

Since you are using Pi headless and you don't want to touch it you'll need it to loging automatically for things to happen, http://www.opentechguides.com/how-to/article/raspberry-pi/5/raspberry-pi-auto-start.html (there is also a method for running a script after login in this guide and it can take part of Script part as well)

The things you want in your script are including sudo apt-get install what_ever_you_want,cd /var/www | cat > info.txt for quickly creating a text file and also some sleep n. But, if you choose to use the article I pasted in Autologin part, the script there works after login so you might not need to use sleep n's.

Hope this helps.

  • I don't think you quite understood what I was asking. I know well enough how to do everything once the system is running. However, that is not what I want to do. What I want is for noobs to copy stuff into the right dirs for me as it installs. – jamesraf18 Jul 25 '15 at 1:26
  • It would be wise to call script in rc.local instead of init.d since networking might not be up by then. Also, you might need to make sure that it runs once only (and not everytime it boots). You might want to put a lock by keeping a setup variable in file or something. – dhruvvyas90 Jul 25 '15 at 6:15
  • and why trying to install the same package on every boot? – aastefanov Jul 25 '15 at 8:53
  • That's why I told to add some sleep but you're right, rc.local would be better. Yes, a condition branch in the script before the installing lines would get rid of that @dastaan – Tolga Varol Jul 25 '15 at 14:57
  • A condition line in the script can easily get rid of that I didn't write the exact answer @alb3rtano0012 – Tolga Varol Jul 25 '15 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.