forked from LibrePhotos/librephotos-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-librephotos.sh
executable file
·232 lines (202 loc) · 10.6 KB
/
install-librephotos.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
#!/bin/bash
######################### HERE EDIT VARIABLES ################################################
# the location of photos. If changed here, also must change the path in thenginx virtual host.
# /etc/nginx
export PHOTOS=/var/lib/librephotos/photos
export BASE_DATA=/var/lib/librephotos/data
# If your hardware without AVX and SSE instructions, seach in this file by keyword
# 'dlib' and read instructions :) Modern system have these
######## Below change ONLY if you know what are you doing. #####################
# Front-end’s IPs from which allowed to handle set secure headers. (comma separate).
# Ref: https://docs.gunicorn.org/en/stable/settings.html#forwarded-allow-ips
# Ref2: https://github.com/benoitc/gunicorn/issues/1857
# '*' to disable
export FORWARDED_ALLOW_IPS=
# Postgresql connection settings
DB_HOST=localhost
DB_PORT=5432
# REDIS connection settings. Unnecessary settings comment with TWO SYMBOLS "\#" or delete.
# If connection to REDIS using sock, set REDIS sock permissions to 770 and restart it.
# Script adds librephotos to redis group.
# for sock connection leave uncommented only REDIS_PATH, all other settings comment out.
REDIS=( \#REDIS_PASS= \#REDIS_DB= REDIS_HOST=localhost REDIS_PORT=6379 \#REDIS_PATH=/run/redis/redis-server.sock )
# If postgresql server is NOT local, after installation remove from these files
# /etc/systemd/system/librephotos-backend.service
# /etc/systemd/system/librephotos-worker.service
# these settings:
# postgresql.service
# Requires=postgresql.service
# If CPU supports SSE2, AVX, find in the script FFTW and uncomment configure settings
######################### END OF EDITABLE SECTION #########################################
set -exa
# PRE INSTALL
# if exist, removes old user
check_user=$(awk -F: -v user=librephotos '$1 == user {print $1}' /etc/passwd)
[[ $check_user ]] && userdel -rf librephotos
[[ -d /usr/lib/librephotos ]] && rm -rf /usr/lib/librephotos
id -g librephotos > /dev/null || groupadd -r librephotos
id -u librephotos > /dev/null || useradd --home-dir /usr/lib/librephotos --comment "librephotos user" -g librephotos -mr -s /usr/sbin/nologin librephotos
export BASE_LOGS=/var/log/librephotos
mkdir -p $BASE_LOGS
mkdir -p $PHOTOS
mkdir -p $BASE_DATA/data_models/{places365,im2txt,clip-embeddings}
mkdir -p $BASE_DATA/protected_media/{thumbnails_big,square_thumbnails,square_thumbnails_small,faces}
mkdir -p $BASE_DATA/nextcloud_media
chown -R librephotos:librephotos $BASE_LOGS
chown -R librephotos:librephotos $PHOTOS
chown -R librephotos:librephotos $BASE_DATA
# CREATING DATABASE
REQUIRED_PKG=(postgresql)
for i in "${REQUIRED_PKG[@]}"; do
[ $(dpkg-query -W -f='${Status}' $i 2>/dev/null | grep -c "ok installed") -eq 0 ] && apt install --no-install-recommends -y $i
done
if [[ -z "${DOCKERDEPLOY}" ]];
then
systemctl start postgresql.service
systemctl enable postgresql.service
su - postgres << EOF
psql -c 'DROP DATABASE IF EXISTS librephotos;'
psql -c 'DROP USER IF EXISTS librephotos;'
psql -c 'CREATE USER librephotos;'
psql -c 'CREATE DATABASE "librephotos" WITH OWNER "librephotos" TEMPLATE = template0 ENCODING = "UTF8";'
psql -c 'GRANT ALL privileges ON DATABASE librephotos TO librephotos;'
exit
EOF
echo 'su - postgres -c "psql -U postgres -d postgres -c \"alter user librephotos with password tmp_password;\""' > /tmp/database_pass
pass=$( < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-12};)
sed -i "s|tmp_password|'${pass}'|g" /tmp/database_pass
chmod +x /tmp/database_pass
/tmp/database_pass
else
echo "skipping db init"
fi
# LIBREPHOTOS : BACKEND
REQUIRED_PKG=( wget swig ffmpeg libimage-exiftool-perl libpq-dev curl libopenblas-dev libmagic1 libboost-all-dev libxrender-dev \
liblapack-dev git bzip2 cmake build-essential libsm6 libglib2.0-0 libgl1-mesa-glx gfortran gunicorn \
libheif-dev libssl-dev rustc liblzma-dev python3 python3-pip imagemagick redis-server )
for i in "${REQUIRED_PKG[@]}"; do
[ $(dpkg-query -W -f='${Status}' $i 2>/dev/null | grep -c "ok installed") -eq 0 ] && apt install --no-install-recommends -y $i
done
redis-server --daemonize yes
# This part compiles libvips. More info https://libvips.github.io/libvips/install.html
REQUIRED_PKG=( build-essential pkg-config libglib2.0-dev libexpat1-dev libgsf-1-dev liborc-dev libexif-dev libtiff-dev \
librsvg2-dev libpng-dev libwebp-dev )
for i in "${REQUIRED_PKG[@]}"; do
[ $(dpkg-query -W -f='${Status}' $i 2>/dev/null | grep -c "ok installed") -eq 0 ] && apt install --no-install-recommends -y $i
done
# Some packages differs in Ubuntu and Debian. Here script checks distro and installs correct packages
distro=$(lsb_release -i | awk '{print($3)}')
# here Ubuntu packages
[[ $distro == Ubuntu ]] && REQUIRED_PKG=( libjpeg-turbo8-dev )
# here Debian packages
[[ $distro == Debian ]] && REQUIRED_PKG=( libjpeg62-turbo-dev )
for i in "${REQUIRED_PKG[@]}"; do
[ $(dpkg-query -W -f='${Status}' $i 2>/dev/null | grep -c "ok installed") -eq 0 ] && apt install --no-install-recommends -y $i
done
#Optimal. FFTW library. Fourier Transform can be used in the image edtion. Filters, effects, etc. Librephotos have not (yet) these features.
#wget http://fftw.org/fftw-3.3.10.tar.gz
#tar xf fftw-3.3.10.tar.gz
#cd fftw-3.3.10
# FFTW. if CPU support sse2, avx, uncomment below.
# More info about optimization: http://fftw.org/fftw3_doc/Installation-on-Unix.html
#./configure --enable-threads --with-pic #--enable-sse2 --enable-avx
#make -j$(nproc --all)
#make install
#ldconfig
#cd ..
# Compiling libvips from source. Installed libvips42 from repositories not working.
if ! which vips; then
wget https://github.com/libvips/libvips/releases/download/v8.12.1/vips-8.12.1.tar.gz
tar xf vips-8.12.1.tar.gz
cd vips-8.12.1
[ $(dpkg-query -W -f='${Status}' libmagick++-dev 2>/dev/null | grep -c "ok installed") -eq 0 ] && apt install --no-install-recommends -y libmagick++-dev
./configure --with-magickpackage=ImageMagick
make -j$(nproc --all)
make install
ldconfig
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib' >> /usr/lib/librephotos/.bashrc
cd ..
else
echo "VIPS allready found on the system"
echo "Sometimes older vips can cause problems."
read -t 5 -p "If librephotos-backend not starting, try to uninstall vips and recompile again"
fi
su - -s $(which bash) librephotos << EOF
curl -SL https://github.com/LibrePhotos/librephotos-docker/releases/download/0.1/places365.tar.gz | tar -zxC $BASE_DATA/data_models/
curl -SL https://github.com/LibrePhotos/librephotos-docker/releases/download/0.1/im2txt.tar.gz | tar -zxC $BASE_DATA/data_models/
curl -SL https://github.com/LibrePhotos/librephotos-docker/releases/download/0.1/clip-embeddings.tar.gz | tar -zxC $BASE_DATA/data_models/
mkdir -p ~/.cache/torch/hub/checkpoints/
curl -SL https://download.pytorch.org/models/resnet152-b121ed2d.pth -o ~/.cache/torch/hub/checkpoints/resnet152-b121ed2d.pth
pip3 install torch==1.7.1+cpu torchvision==0.8.2+cpu -f https://download.pytorch.org/whl/torch_stable.html
#################################################################################################
# Here seting up AVX and SSE support.
# Comment out first line 'pip3 install...' and uncoment second. Must leave only one.
##################################################################################################
pip3 install -v --install-option="--no" --install-option="DLIB_USE_CUDA" dlib
#pip3 install -v --install-option="--no" --install-option="DLIB_USE_CUDA" --install-option="--no" --install-option="USE_AVX_INSTRUCTIONS" --install-option="--no" --install-option="USE_SSE4_INSTRUCTIONS" dlib
#This does only support x64 and not ARM. To install for ARM you have to build it from source
pip3 install faiss-cpu
pip3 install pyvips
git clone https://github.com/LibrePhotos/librephotos.git backend
cd backend
pip3 install -r requirements.txt
EOF
# POST INSTALL
usermod -aG redis librephotos
[ -d /usr/lib/librephotos/bin ] || mkdir -p /usr/lib/librephotos/bin
cp ressources/bin/* /usr/lib/librephotos/bin/
ln -fs /usr/lib/librephotos/bin/librephotos-cli /usr/sbin/librephotos-cli
chown -R librephotos:librephotos /usr/lib/librephotos/bin/
cp -r ressources/etc/librephotos/ /etc/
cp ressources/systemd/* /etc/systemd/system/
sed -i "s|DB_HOST=|DB_HOST=${DB_HOST}|g" /etc/librephotos/librephotos-backend.env
sed -i "s|DB_PORT=|DB_PORT=${DB_PORT}|g" /etc/librephotos/librephotos-backend.env
sed -i "s|DB_PASS=password|DB_PASS=${pass}|g" /etc/librephotos/librephotos-backend.env
sed -i "s|FORWARDED_ALLOW_IPS=|FORWARDED_ALLOW_IPS=${FORWARDED_ALLOW_IPS}|g" /etc/librephotos/librephotos-backend.env
sed -i "s|BASE_DATA=|BASE_DATA=${BASE_DATA}|g" /etc/librephotos/librephotos-backend.env
sed -i "s|PHOTOS=|PHOTOS=${PHOTOS}|g" /etc/librephotos/librephotos-backend.env
for i in "${REDIS[@]}"; do
echo $i >> /etc/librephotos/librephotos-backend.env
done
if [[ -z "${DOCKERDEPLOY}" ]]; then
rm /tmp/database_pass
else
echo "skipping temp database pass removal"
fi
systemctl start librephotos-worker.service && systemctl start librephotos-backend && systemctl start librephotos-image-similarity.service && echo
systemctl enable librephotos-backend
systemctl enable librephotos-worker.service
systemctl enable librephotos-image-similarity.service
# LIBREPHOTOS : FRONTEND
REQUIRED_PKG=( curl git xsel git )
for i in "${REQUIRED_PKG[@]}"; do
[ $(dpkg-query -W -f='${Status}' $i 2>/dev/null | grep -c "ok installed") -eq 0 ] && apt install --no-install-recommends -y $i
done
[ $(dpkg-query -W -f='${Status}' nodejs 2>/dev/null | grep -c "ok installed") -eq 0 ] && \
curl -sL https://deb.nodesource.com/setup_14.x | bash - && apt install nodejs -y --no-install-recommends
npm install -g yarn
export NODE_OPTIONS=--max-old-space-size=2048
su - -s $(which bash) librephotos << EOF
git clone https://github.com/LibrePhotos/librephotos-frontend.git frontend
cd frontend
npm install
npm run postinstall
npm run build
npm install serve
EOF
systemctl start librephotos-frontend
systemctl enable librephotos-frontend
# NGINX REVERSE PROXY
if [ $(dpkg-query -W -f='${Status}' nginx* 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
apt install -y nginx
cp ressources/etc/nginx/nginx.conf /etc/nginx/nginx.conf
systemctl restart nginx
else
cp ressources/etc/nginx/librephotos /etc/nginx/sites-available/librephotos
ln -s /etc/nginx/sites-available/librephotos /etc/nginx/sites-enabled/
fi
echo "If system has the nginx server before installing librephotos, edit virtual host /etc/nginx/librephotos and restart nginx."
echo "Optimal: check other settings in the file /etc/librephotos/librephotos-backend.env"
echo "After changing BASE_DATA, must edit file /etc/nginx/nginx.conf or /etc/nginx/sites-available/librephotos and change accordinaly"
echo "alias. There 4 lines"-