Skip to content

Instantly share code, notes, and snippets.

@makkinga
Last active August 31, 2020 13:23
Show Gist options
  • Save makkinga/8df95261f8f3ac729e47426aa1949dc3 to your computer and use it in GitHub Desktop.
Save makkinga/8df95261f8f3ac729e47426aa1949dc3 to your computer and use it in GitHub Desktop.
Preparing SD card for headless pi

Setup for a new headless Raspberry Pi

Download Raspbian

Download full raspbian from https://www.raspberrypi.org/downloads/raspbian/.

BalenaEtcher

  1. On OSX >= Catalina open BalenaEtcher with
sudo /Applications/balenaEtcher.app/Contents/MacOS/balenaEtcher
  1. Select the Raspbian image and the SD card and click Flash
  2. Remove and palce back the SD card for the next steps

SSH

Enable ssh with the following command:

touch /Volumes/boot/ssh

WiFi

  1. Create a file in the root of boot
nano /Volumes/boot/wpa_supplicant.conf

and add the following content

country=NL
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="Makkinga"
    psk="password"
    key_mgmt=WPA-PSK
    id_str="home"
}

network={
    ssid="Rosegaar.nl WiFi"
    psk="0622516682"
    key_mgmt=WPA-PSK
    id_str="rosegaar"
}

Boot the RPI

Remove the SD card, plug it into the RPI and turn it on

Login over WiFi

  1. Clear out previous references
ssh-keygen -R raspberrypi.local
  1. Login over SSH
ssh pi@raspberrypi.local
  1. Password is raspberry

Install NGINX + PHP

  1. Update RPI
sudo apt-get update
sudo apt-get upgrade

NGINX

  1. Remove Apache
sudo apt-get remove apache2
  1. Install NGINX
sudo apt-get install nginx
  1. Start NGINX
sudo systemctl start nginx
  1. Get the RPI's local IP
hostname -I
  1. Test the server by visiting http://IPADDRESS in a browser

PHP

  1. Install PHP
sudo apt-get install php7.3-fpm php7.3-mbstring php7.3-mysql php7.3-curl php7.3-gd php7.3-curl php7.3-zip php7.3-xml -y
  1. Edit the default NGINX configuration file

2.1 Open the file

sudo nano /etc/nginx/sites-enabled/default

2.2 Add index.php to index

index index.html index.htm;
index index.php index.html index.htm;

2.3 Find and replace

#location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #}
location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        }

Reload and test

Restart NGINX

sudo systemctl reload nginx

Set permissions for SFTP

sudo chown -R pi:www-data /var/www
sudo chmod u+rxw,g+rx-w,o-rwx /var/www
sudo chmod g+s /var/www

Make a test endpoint

Create a new file
sudo nano /var/www/html/index.php
Add some content
<?php phpinfo(); ?>

NPM

  1. Detect What Version of Node.js You Need

The processor on Raspberry Pi is ARM, but depends on the model there can be ARMv6, ARMv7 or ARMv8. This 3 versions of ARM is supported by Node.js. So first of all type this in your terminal on raspberry pi to detect the version that you need:

uname -m
  1. Download the tar.gz
wget https://nodejs.org/dist/v8.9.0/node-v8.9.0-linux-armv6l.tar.gz
  1. Extract the Archive
tar -xzf node-v8.9.0-linux-armv6l.tar.gz
  1. Copy Node to /usr/local
cd node-v8.9.0-linux-armv6l/
sudo cp -R * /usr/local/
  1. Check If Everything Is Installed Ok
node -v
npm -v

Composer

  1. Download and install Ccomposer
curl -sS https://getcomposer.org/installer | php
  1. Move to /usr/local/
sudo mv composer.phar /usr/local/bin/composer
  1. Check If Everything Is Installed Ok
composer

ZSH

sudo apt install zsh

Oh-My-Zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Moving to different WiFi network

If you put a file named wpa_supplicant.conf in the small FAT32 boot partition of a Raspbian SD card the system will use the information in that file to connect to your wireless network. The wpa_supplicant.conf file should contain the following:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=NL

network={
	ssid="WiFi SSID"
	psk="WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}

The wpa_supplicant.conf file normally resides at /etc/wpa_supplicant/wpa_supplicant.conf. When Raspbian finds one in the boot partition it will move it to its correct location and use it to connect to the network. This makes it easy to reconfigure WiFi, since the boot partition is accessible from any computer (Windows, Mac, Linux, Chrome OS).

Get device info

cat /proc/device-tree/model

returns something like

Raspberry Pi 4 Model B Rev 1.2

Assign a static private IP address to Raspberry Pi with DHCPCD

Raspbian Jessie, or Jessie Lite – the current Raspbian operating systems at the moment – have a DHCP client daemon (DHCPCD) that can communicate with the DHCP servers from routers. The configuration file of a DHCP client daemon allows you to change the private IP address of a computer and set it up in the long term. The following instructions will assign a static IPv4 address with 32 bits (not to be confused with an IPv6 address, which has 128 bits available) to the Raspberry Pi.

Before you begin with the assignment of a private IP address for Raspberry Pi, check whether DHCPCD is already activated using the following command:

sudo service dhcpcd status

In case it’s not, activate DHCPCD as follows:

sudo service dhcpcd start
sudo systemctl enable dhcpcd

Now make sure that the configuration of the file /etc/network/interfaceshas the original status. For this, the iface configuration needs to be set at manual for the interfaces.

For the editing of the activated DHCPCDs, start by opening the configuration file /etc/dhcpcd.confand running the following command:

sudo nano /etc/dhcpcd.conf

You’ll now carry out the configuration of the static IP address. If your Raspberry Pi is connected to the internet via an Ethernet or network cable, then enter the command

interface eth0

if it takes place over Wi-Fi, then use this command

interface wlan

To assign an IP address to Raspberry Pi, use the command

static ip_address=

followed by the desired IPv4 address and the suffix /24 (an abbreviation of the subnet mak 255.255.255.0). For example, if you want to link a computer with the IPv4 address 192.168.0.4, then you need to use the command

static ip_address=192.168.0.4/24

It goes without saying that the address used here is not yet used anywhere else. As such, it also can’t be located in the address pool of a DHCP server.

You still then need to specify the address of your gateway and domain name server (usually both are the router). Raspberry Pi turns to the gateway address if an IP address to which it wants to send something is outside of the subnet mask (in the example, this would mean outside of the range 192.168.0). In the following command, the IPv4 address 192.168.0.1 is used as an example as both the gateway and DNS server. The complete command looks like this in our example (where a network cable is used for the internet connection):

interface eth0
static ip_address=192.168.0.4/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

The command lines above match the IPv4 addresses that you want to use for your Raspberry Pi, or where your router is assigned. Save the changes with ‘Ctrl + O’ and then press the enter key. Close the configuration file with ‘Ctrl + X’. Restart to adopt the newly assigned static IP address in the network:

sudo reboot

Now use a ping command to check whether the Raspberry Pi is accessible in the network with its new IP address:

ping raspberrypi.local

If the connection of the IP address was successful, you’ll see that you can reach it under the new IP address with a ping.

Change hostname

  1. Change /etc/hostname
sudo nano /etc/hostname

Change raspberrypi to your desired hostname. Hit CTRL+O and then ENTER to Save. Hit CTRL+X to Exit.

  1. Change /etc/hosts
sudo nano /etc/hosts

Change raspberrypi to your desired hostname. Hit CTRL+O and then ENTER to Save. Hit CTRL+X to Exit.

  1. Reboot
sudo reboot

More info

https://desertbot.io/blog/headless-raspberry-pi-3-bplus-ssh-wifi-setup

TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment