-
Notifications
You must be signed in to change notification settings - Fork 40
/
installation.sh
271 lines (252 loc) · 13.7 KB
/
installation.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
########################################################################################
# Installation procedure for the Raspberry Pi - IP Camera.
########################################################################################
# This procedure was designed on top of a foundation Raspbian Stretch lite image with release date 11-13-2018
# Download the latest Raspbian Stretch Lite image from https://downloads.raspberrypi.org/raspbian_lite_latest
# Unzip your downloaded image, and write it to SD card with win32 disk imager.
# Since we will be needing the ssh server we need to activate it: access your sd card and create an empty file
# with the name 'ssh' in the root (where u can find cmdlines.txt) folder. (this should be done before your first boot).
# Boot up your SD card in your Raspberry Pi, and Log into the Raspbian Stretch OS, with pi as username and raspberry as password.
# Start executing below commands in sequence.
########################################################################################
# Connect the Raspberry Pi with a screen over HDMI, also connect a keyboard.
# Alternatively, scan your network for the Raspberry Pi's IP address and ssh into it.
# Login with username 'pi' and password 'raspberry'.
# We will enable the camera on the device
########################################################################################
sudo raspi-config
# go to menu option 5 - Interfacing Options
# press enter on P1 Camera
# select yes to enable the camera and press enter
# press enter again to confirm
# press the tab key and press enter on finish
# accept to reboot the raspberry pi
# log back in to your raspberry pi
########################################################################################
# Bootstrap - Preparing the Raspbian OS.
########################################################################################
# Regen our security keys, it's a best practice
sudo /bin/rm -v /etc/ssh/ssh_host_*
sudo ssh-keygen -t dsa -N "" -f /etc/ssh/ssh_host_dsa_key
sudo ssh-keygen -t rsa -N "" -f /etc/ssh/ssh_host_rsa_key
sudo ssh-keygen -t ecdsa -N "" -f /etc/ssh/ssh_host_ecdsa_key
sudo ssh-keygen -t ed25519 -N "" -f /etc/ssh/ssh_host_ed25519_key
sudo systemctl restart ssh.service
########################################################################################
# Update Firmware - Making sure that your Raspbian firmware is the latest version.
########################################################################################
# update raspbian
sudo apt-get update && sudo apt-get -y dist-upgrade
########################################################################################
# Download a copy of our git repository and extract it.
########################################################################################
wget -O /home/pi/RaspberryIPCamera.zip https://github.com/ronnyvdbr/RaspberryIPCamera/archive/v1.8-beta.zip
unzip /home/pi/RaspberryIPCamera.zip -d /home/pi
rm /home/pi/RaspberryIPCamera.zip
mv /home/pi/RaspberryIPCamera* /home/pi/RaspberryIPCamera
########################################################################################
# Set-up nginx with php support and enable our Raspberry IP Camera website.
########################################################################################
# Install nginx with php support.
sudo nano /etc/apt/sources.list
# PASTE INTO /etc/apt/sources.list below first line, then save and exit
: '
deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
'
# Update the APT sources for the new source we just added
sudo apt-get update
sudo apt-get -y install nginx php5-fpm
# By default, APT will install PHP7.0; we have just installed php5, so we want to make sure to set the php version system-wide, just in case
sudo update-alternatives --set php /usr/bin/php5
# Disable the default nginx website.
sudo rm /etc/nginx/sites-enabled/default
# Copy our siteconf into place
sudo cp /home/pi/RaspberryIPCamera/DefaultConfigFiles/RaspberryIPCamera.Nginx.Siteconf /etc/nginx/sites-available/RaspberryIPCamera.Nginx.Siteconf
# Lets enable our website
sudo ln -s /etc/nginx/sites-available/RaspberryIPCamera.Nginx.Siteconf /etc/nginx/sites-enabled/RaspberryIPCamera.Nginx.Siteconf
# Disable output buffering in php.
sudo sed -i 's/output_buffering = 4096/;output_buffering = 4096/g' /etc/php5/fpm/php.ini
# Set permissions for the config files
sudo chgrp www-data /home/pi/RaspberryIPCamera/www/RaspberryIPCameraSettings.ini
chmod 664 /home/pi/RaspberryIPCamera/www/RaspberryIPCameraSettings.ini
sudo chgrp www-data /home/pi/RaspberryIPCamera/secret/RaspberryIPCamera.secret
chmod 664 /home/pi/RaspberryIPCamera/secret/RaspberryIPCamera.secret
########################################################################################
# Install all UV4L components
########################################################################################
# Add the supplier's repository key to our key database
curl http://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add -
# Open /etc/apt/sources.list in Nano
sudo nano /etc/apt/sources.list
# PASTE INTO /etc/apt/sources.list below second line, then save and exit
: '
deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/stretch stretch main
'
sudo apt-get update
# Now fetch and install the required modules.
sudo apt-get -y install uv4l uv4l-raspicam
sudo apt-get -y install uv4l-raspicam-extras
sudo apt-get -y install uv4l-server
# Let's copy our own config files in place.
sudo cp /home/pi/RaspberryIPCamera/DefaultConfigFiles/uv4l-raspicam.conf /etc/uv4l/uv4l-raspicam.conf
sudo cp /home/pi/RaspberryIPCamera/DefaultConfigFiles/uv4l-server.conf /etc/uv4l/uv4l-server.conf
sudo sed -i "s/--editable-config-file=\$CONFIGFILE/--server-config-file=\/etc\/uv4l\/uv4l-server.conf/g" /etc/systemd/system/uv4l_raspicam.service
# Notify systemd of service changes.
sudo systemctl daemon-reload
# Set some permissions so our web gui can modify the config files.
sudo chgrp www-data /etc/uv4l/uv4l-raspicam.conf
sudo chmod 664 /etc/uv4l/uv4l-raspicam.conf
########################################################################################
# Install the RTSP server
########################################################################################
# we will be compiling software, so install some prerequisite
sudo apt-get -y install cmake
# Make sure to be in home directory
cd ~
# first compile the live555 library as a prerequisite
wget http://www.live555.com/liveMedia/public/live555-latest.tar.gz -O - | tar xvzf -
cd live
./genMakefiles linux
sudo make CPPFLAGS=-DALLOW_RTSP_SERVER_PORT_REUSE=1 install
cd ..
# clone the rtsp server's git repository, compile and install
sudo apt-get -y install git
git clone https://github.com/mpromonet/v4l2rtspserver.git
sudo apt-get install -y libasound2-dev liblog4cpp5-dev
cd v4l2rtspserver
cmake . && make
sudo make install
# Put system service file for RTSP server into place
sudo cp /home/pi/RaspberryIPCamera/DefaultConfigFiles/RTSP-Server.service /etc/systemd/system/RTSP-Server.service
# Notify systemd of a service installation.
sudo systemctl daemon-reload
# Set the startup for the service to disabled for our default config.
sudo systemctl disable RTSP-Server.service
########################################################################################
# Set some additional rights and config files
########################################################################################
# put a sudoers file in the correct location for php shell commands integration
sudo cp /home/pi/RaspberryIPCamera/DefaultConfigFiles/sudoers_commands /etc/sudoers.d/sudoers_commands
# There's a systemd-timesync service running on a default install of Stretch
# Install NTP
sudo apt-get install -y ntp
# Stop & Disable systemd-timesync service
sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd
# Put correct security rights on configuration files
sudo chgrp www-data /etc/timezone
sudo chmod 664 /etc/timezone
sudo chgrp www-data /etc/ntp.conf
sudo chmod 664 /etc/ntp.conf
########################################################################################
# Make our SD card read only, to preserve it and contribute to system stability
########################################################################################
# First get rid of some unnecessary pagkages.
sudo apt-get -y remove --purge cron logrotate triggerhappy dphys-swapfile fake-hwclock samba-common
sudo apt-get -y autoremove --purge
# remove rsyslog and install a memory resident variant
sudo apt-get -y remove --purge rsyslog
sudo apt-get -y install busybox-syslogd
# now remap some folders to memory temp space
sudo rm -rf /var/lib/dhcp/ /var/spool /var/lock
sudo rm /etc/resolv.conf
sudo ln -s /tmp /var/lib/dhcp
sudo ln -s /tmp /var/spool
sudo ln -s /tmp /var/lock
sudo ln -s /tmp/resolv.conf /etc/resolv.conf
sudo rm -rf /var/lib/php5/sessions
sudo ln -s /tmp/phpsessions /var/lib/php5/sessions
# configure the boot options to be read-only on next boot
sudo mount -o remount rw /boot
echo "dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait fastboot noswap" | sudo tee /boot/cmdline.txt
# we will now edit the fstab file using nano and add 3 more lines like describe below
sudo nano /etc/fstab
# Our /etc/fstab should look like the one below, copy and paste it
proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat ro,defaults 0 2
/dev/mmcblk0p2 / ext4 ro,defaults,noatime 0 1
tmpfs /var/log tmpfs nodev,nosuid 0 0
tmpfs /var/tmp tmpfs nodev,nosuid 0 0
tmpfs /tmp tmpfs nodev,nosuid 0 0
# Modify service unit of nginx service to create log folder before starting, otherwise error
sudo sed -i '20i\ExecStartPre=/bin/mkdir /var/log/nginx' /lib/systemd/system/nginx.service
# Modify service unit of php5-fpm service to create a tmp folder to store sessions in, otherwise error
sudo sed -i '8i\ExecStartPre=/bin/mkdir /tmp/phpsessions' /lib/systemd/system/php5-fpm.service
sudo sed -i '9i\ExecStartPre=/bin/chgrp www-data /tmp/phpsessions' /lib/systemd/system/php5-fpm.service
sudo sed -i '10i\ExecStartPre=/bin/chmod 775 /tmp/phpsessions' /lib/systemd/system/php5-fpm.service
# reboot your raspberry pi here, check if you are read only
sudo reboot
# log in again and check the mounts
mount | grep /dev/mmcblk0p2
# your / filesystem should be ro
########################################################################################
# Create easy to use alias to swap between readonly and readwrite
########################################################################################
# Open /etc/bash.bashrc
sudo nano /etc/bash.bashrc
# Paste the following lines to the bottom of the file, then save and exit
: '
# set variable identifying the filesystem you work in (used in the prompt below)
set_bash_prompt(){
fs_mode=$(mount | sed -n -e "s/^\/dev\/.* on \/ .*(\(r[w|o]\).*/\1/p")
PS1='\[\033[01;32m\]\u@\h${fs_mode:+($fs_mode)}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
}
alias ro='sudo mount -o remount,ro / ; sudo mount -o remount,ro /boot'
alias rw='sudo mount -o remount,rw / ; sudo mount -o remount,rw /boot'
# setup fancy prompt"
PROMPT_COMMAND=set_bash_prompt
'
# Open ~/.bashrc
sudo nano ~/.bashrc
# Paste the following lines to the bottom of the file, then save and exit
: '
# set variable identifying the filesystem you work in (used in the prompt below)
set_bash_prompt(){
fs_mode=$(mount | sed -n -e "s/^\/dev\/.* on \/ .*(\(r[w|o]\).*/\1/p")
PS1='\[\033[01;32m\]\u@\h${fs_mode:+($fs_mode)}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
}
alias ro='sudo mount -o remount,ro / ; sudo mount -o remount,ro /boot'
alias rw='sudo mount -o remount,rw / ; sudo mount -o remount,rw /boot'
# setup fancy prompt"
PROMPT_COMMAND=set_bash_prompt
'
# Open /etc/bash.bash_logout (You will likely create this file by doing this)
sudo nano /etc/bash.bash_logout
#Paste the following lines to the bottom of the file, then save and exit
: '
mount -o remount,ro /
mount -o remount,ro /boot
'
# Open ~/.bash_logout
sudo nano ~/.bash_logout
#Paste the following lines to the bottom of the file, then save and exit
: '
mount -o remount,ro /
mount -o remount,ro /boot
'
########################################################################################
# Clean unneeded packages from our design to make the image size smaller for redistribution
########################################################################################
# Let's clean as much rubbish from our image so we can repack this for internet distribution in a normal size.
sudo mount -o remount rw /
sudo apt-get -y install localepurge
sudo localepurge
sudo apt-get -y remove --purge localepurge
sudo apt-get -y remove --purge avahi-daemon build-essential nfs-common console-setup curl dosfstools lua5.1 luajit manpages-dev parted python-rpi.gpio python
sudo apt-get -y autoremove --purge
sudo apt-get clean
sudo rm -rf /var/swap
########################################################################################
# Final reboot before we can start using our IP Camera.
# Issue below reboot command to restart your Raspberry Pi.
# The Raspberry Pi is configured for DHCP and will therefore retrieve an IP address
# from the network automatically.
# Use a ping sweep tool to find the IP address of your Raspberry Pi or check out
# your DHCP server lease table. (or connect your Raspberry Pi with HDMI, login and check
# the IP address with 'ifconfig')
# Lastly, open your browser and enter the IP address in the address bar.
########################################################################################
sudo reboot
# to check
# add our pi user to www-data group.
# sudo usermod -a -G www-data pi