gbbirkisson / spis

Simple private image server 🖼️
GNU General Public License v3.0
128 stars 8 forks source link

make service file for spis systemctl #100

Closed id4vip closed 1 year ago

id4vip commented 1 year ago

I am very happy using spis on my machine with docker format. Now I want to install spis on debian machine directly. I have downloaded spis binary & install niginx & sqlite on my machine. Now I want that spis server run automatically after each time system reboot with systemctl command please guide me & help me to make service file for spis “ sudo nano /etc/systemd/system/spis.service” . I remembered I used for zigbee2mqtt this service file ` [Unit] Description=zigbee2mqtt After=network.target

[Service] Environment=NODE_ENV=production ExecStart=/usr/bin/npm start WorkingDirectory=/opt/zigbee2mqtt StandardOutput=inherit StandardError=inherit Restart=always RestartSec=10s User=pi

[Install] WantedBy=multi-user.target

` Please modify this service file for spis software binary. I save spis binary in this location “/mnt/mydata/spis/spis-server-x86_64-unknown-linux-gnu” I want spis dedicated data folder “/mnt/mydata/spis_data/” My images are at “/mnt/mydata/my_photos” want photos are read only permission for spis Please tell me how I can pass these location values to spis & binary software control by ststemctl. thanks

gbbirkisson commented 1 year ago

There are plenty of resources on the internet on how to write unit files for systemd. Search and you will find the information you need. That being said, the file would look something like this:

[Unit]
Requires=multi-user.target
After=multi-user.target

[Service]
Environment="SPIS_MEDIA_DIR=/mnt/mydata/my_photos"
Environment="SPIS_DATA_DIR=/mnt/mydata/spis_data"
Environment="SPIS_SERVER_SOCKET=/path/to/spis.sock"
Environment="RUST_LOG=error,spis_server=info"
User=someuser
Group=somegroup
ExecStart=/mnt/mydata/spis/spis-server-x86_64-unknown-linux-gnu
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target

Note that this only runs SPIS, not nginx! You will have to configure nginx seperatly.

id4vip commented 1 year ago

Thanks for helping, I have made my spis.service file ` [Unit]
Requires=multi-user.target
After=multi-user.target

[Service]
Environment="SPIS_MEDIA_DIR=/mnt/dietpi_userdata/plex_data"
Environment="SPIS_DATA_DIR=/mnt/dietpi_userdata/spis/data"
Environment="SPIS_SERVER_SOCKET=/var/run/spis.sock"
Environment="SPIS_API_MEDIA_PATH=/mnt/dietpi_userdata/spis/media"
Environment="SPIS_API_THUMBNAIL_PATH=/mnt/dietpi_userdata/spis/data/thumbnails"
Environment="RUST_LOG=error,spis_server=info"
User=spis
Group=spis
ExecStart=/mnt/dietpi_userdata/spis/spis_active
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
` but system fail to start program. the logs are like this

https://dpaste.org/M9auU

gbbirkisson commented 1 year ago

TL;DR: The user spis is not allowed to create the socket /var/run/spis.sock on your system.

I guess the error handling could be better here, but the logs state that the error originates from spis-server/src/server/mod.rs:171. So looking at that snippet ...

https://github.com/gbbirkisson/spis/blob/50ff25bc6a95ed3dcf098f35dfbcc0618da0cff4/spis-server/src/server/mod.rs#L165-L174

... we can see that it fails to create the socket.

id4vip commented 1 year ago

Thanks, for your tip, after adding sock file with permission. software start working. I configured nginx also so now able to see spis default page with time line on top. when I have added few photo to test. system not able to shows those on web page. in logs it is complaining exif data not found. logs are attached with link. just for clarify the photo files need to be spis user permission/domination or i can give photos to read only access to spis software by giving photos under plex user with read only access to spis. i do not want anybody delete photos through spis webpage. https://dpaste.org/28fEC also the photo without exif data like scanned etc. will not able to shown through spis system. I want all photos must be shown even time line screwed.

id4vip commented 1 year ago

my working spis.service file is ` GNU nano 5.4 spis.service [Unit] Requires=multi-user.target After=multi-user.target

[Service] Environment="SPIS_MEDIA_DIR=/mnt/dietpi_userdata/plex_data" Environment="SPIS_DATA_DIR=/mnt/dietpi_userdata/spis_data" Environment="SPIS_SERVER_SOCKET=/mnt/dietpi_userdata/spis/spis.sock" Environment="SPIS_API_MEDIA_PATH=/mnt/dietpi_userdata/spis_data/media" Environment="SPIS_API_THUMBNAIL_PATH=/mnt/dietpi_userdata/spis_data/thumbnails" Environment="RUST_LOG=error,spis_server=info" Environment="SPIS_SERVER_ADDRESS=0.0.0.0:8085" Environment="SPIS_PROCESSING_RUN_ON_START=true" Environment="SPIS_PROCESSING_SCHEDULE=0 0 22 *" User=spis Group=spis ExecStart=/mnt/dietpi_userdata/spis/spis_active Restart=always RestartSec=5s

[Install] WantedBy=multi-user.target

`

and in nginx my reverse proxy conf is this ` server {

server_name spis.XXX.ZZZ;

location / {
  proxy_http_version 1.1;
  proxy_set_header "Connection" "";

  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;

  proxy_pass http://192.168.1.150:8085;
}

} `

gbbirkisson commented 1 year ago

I want all photos must be shown even time line screwed.

Creating a separate issue for that: https://github.com/gbbirkisson/spis/issues/101

and in nginx my reverse proxy conf is this

Use the nginx config file from the dockerfile as a reference to create your new nginx config:

https://github.com/gbbirkisson/spis/blob/50ff25bc6a95ed3dcf098f35dfbcc0618da0cff4/docker/nginx.conf#L1-L35

id4vip commented 1 year ago

The reason for using 0.0.0.0:8085 port/web based ip address is that. I want to avoid nginx & want to use rust base reverse proxy https://github.com/junkurihara/rust-rpxy/tree/develop this proxy this time working at my home and dealing with home-assistant, vaultwarden, adguard-home & plex. is there any limitation using port based approach as i do not know how i can connect spis with rpxy through web socket etc.

gbbirkisson commented 1 year ago

is there any limitation using port based approach

Nope, its just a matter of preference

id4vip commented 1 year ago

so now i struck that system not showing any photo. windows look like this https://yourimageshare.com/ib/NhYiKVh2yf

gbbirkisson commented 1 year ago

You have told SPIS that the proxy will serve the images on these paths:

Environment="SPIS_API_MEDIA_PATH=/mnt/dietpi_userdata/spis_data/media"
Environment="SPIS_API_THUMBNAIL_PATH=/mnt/dietpi_userdata/spis_data/thumbnails"

That translates to:

http://spis.XXX.ZZZ/mnt/dietpi_userdata/spis_data/media/someimage.jpg
http://spis.XXX.ZZZ/mnt/dietpi_userdata/spis_data/thumbnails/somethumbnail.jpg

I suspect that this is incorrect. Use the browser network tab to see where its trying to get the images from. Make sure that your reverse proxy is serving the images at those locations, or fix the configuration and point SPIS to the correct paths.

id4vip commented 1 year ago

Thanks, now i am understand the trouble. your project is perfect but my bad luck i am not able to bridge two rust projects. in other projects only one page is divert by reverse proxy & all other functioning carried by 3rd party software. in spis we need to give default page & thumbnail address & photo address to revers proxy as your nginx.conf. it will be work with nginx but i do not know how to mould in rpxy (https://github.com/junkurihara/rust-rpxy) . i am getting this error in rpxy logs with not finding thumbnails. I am not much expert in linux so not understanding what is the remedy to bridge rpxy with spis (both awesome project in rust) thanks

id4vip commented 1 year ago

my log file in rpxy side is like ` Aug 02 17:34:28 lxc501 rpxy_active[9419]: 2023-08-02T16:34:28.287643Z INFO rpxy rpxy_lib::log: spis.XXX.ZZZ:8080 <- 192.168.1.93:64300 -- GET /mnt/dietpi_userdata/spis_data/thumbnails/7d660794-6e1a-3c26-b5b8-87dc65f966b0.webp HTTP/1.1 -- 404 Not Found -- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36", "192.168.1.93" "http://localhost:8085/mnt/dietpi_userdata/spis_data/thumbnails/7d660794-6e1a-3c26-b5b8-87dc65f966b0.webp"

` I have checked in given folder spis making thumbnail. but I am not manipulate that in rpxy revers proxy side. My reverse proxy works on port 8080 (meaning spis.XXX.ZZZ:8080) for opening spis default page. spis itself run on port 8085 (meaning 192.168.1.150:8085). both services rpxy & spis on same computer as dedicated services. rpxy works under rpxy:rpxy & spis works under spis:spis user:group.

But one strange thing is may be my reverse proxy itself not configured relation to spis. even then spis default web page not shown images (http://192.168.1.150:8085). Spis default black screen shows with broken image logos & time ribbon at top. indirectly it mean spis itself not able to find its created thumbnails. so i suspected something in my spis.service file something confusing software itself.

gbbirkisson commented 1 year ago

Opening the port SPIS is running on (http://192.168.1.150:8085/) will serve you the GUI, but not the images. This is stated in the README!

Note that the spis-server binary does not serve images. For that you can use something like nginx. See nginx config for an example.

You have to serve both SPIS and static images through your proxy:

flowchart LR

A[Browser] -->|Request on 8080| B[Proxy]
B -->|Request on 8085| C[SPIS]
B -->|Serve Images| D[Files on disk]

Again, read the nginx config provided in this repo, understand how it works, and configure your own proxy accordingly:

https://github.com/gbbirkisson/spis/blob/50ff25bc6a95ed3dcf098f35dfbcc0618da0cff4/docker/nginx.conf#L1-L35

id4vip commented 1 year ago

thanks for helping me. rust-rpxy is not compatible with SPIS software by it self , it need nginx backend support from nginx for web file sharing.

id4vip commented 1 year ago

my exact spis.service file is ` [Unit] Requires=multi-user.target After=multi-user.target

[Service] Environment="SPIS_MEDIA_DIR=/mnt/dietpi_userdata/plex_data" Environment="SPIS_DATA_DIR=/mnt/dietpi_userdata/spis_data" Environment="SPIS_SERVER_SOCKET=/mnt/dietpi_userdata/spis/spis.sock" Environment="SPIS_API_MEDIA_PATH=/mnt/dietpi_userdata/spis_data/media" Environment="SPIS_API_THUMBNAIL_PATH=/mnt/dietpi_userdata/spis_data/thumbnails" Environment="RUST_LOG=error,spis_server=info" Environment="SPIS_SERVER_ADDRESS=0.0.0.0:8085" Environment="SPIS_PROCESSING_RUN_ON_START=true" Environment="SPIS_PROCESSING_SCHEDULE=0 0 22 *" User=spis Group=spis ExecStart=/mnt/dietpi_userdata/spis/spis_active Restart=always RestartSec=5s

[Install] WantedBy=multi-user.target

`

& for nginx my spis.conf is ` server {

listen       80;
listen  [::]:80;
server_name spis.zzz.xxx;

access_log /dev/stdout;

gzip on;
gzip_proxied any;
gzip_types
    text/html
    application/wasm
    application/javascript
    application/json
    text/plain
    image/jpeg;

location /mnt/dietpi_userdata/spis_data/media {
    gzip_static on;
    expires 1y;
    add_header Cache-Control "public";
    alias /mnt/dietpi_userdata/plex_data;
}

location /mnt/dietpi_userdata/spis_data/thumbnails {
    gzip_static on;
    expires 1y;
    add_header Cache-Control "public";
    alias /mnt/dietpi_userdata/spis_data/thumbnails;
}

location / {
    proxy_pass http://unix:/mnt/dietpi_userdata/spis/spis.sock;
    proxy_http_version 1.1;
}

}

`

my spis software is working. but nginx is failing to start , the log of nginx is as. https://dpaste.org/Oxn7h Please guide me how can access spis whole system from my network through nginx revers proxy access my this address "http://spis.zzz.xxx" . my adgurdhome diverting all *.zzz.xxx to my local access 192.168.1.150 . if i disable spis.conf file in nginx then nginx works and able to access homeassistant, adguardhome plex etc. only there is trouble in spis.conf.

the nginx command "nginx -t" give this result ` root@lxc501:/etc/nginx/sites-enabled# nginx -t 2023/08/07 22:22:02 [warn] 3574#3574: duplicate MIME type "text/html" in /etc/nginx/sites-enabled/spis.conf:19 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

`

id4vip commented 1 year ago

one more clarification question, what is the relation ship between "SPIS_API_MEDIA_PATH" variable & "SPIS_MEDIA_DIR" . as i declare "SPIS_MEDIA_DIR" is my plex_data. i want spis just read that data but for all its work/functionality i gave different folder which software owned itself. is this approach is correct or these values must be same. this question come into my mind because i am able to see few files in thumbnails folder. but my created folder(media) for spis empty "SPIS_API_MEDIA_PATH". is at the proper functioning time this location "SPIS_API_MEDIA_PATH" having anything or it is just dummy location.

id4vip commented 1 year ago

I confirm after hash out "# access_log /dev/stdout;" nginx & spis both service working. i am able to open thumbnail directly via browser to known file name (made by spis) http://spis.zzz.xxx/mnt/dietpi_userdata/spis_data/thumbnails/2adb0b13-964f-3130-acc3-1d80945203ec.webp
so confirm webserver is working. but spis default page is not opening and giving me nginix 502 error. so something wrong in websocket adding format location / { proxy_pass http://unix:/mnt/dietpi_userdata/spis/spis.sock; proxy_http_version 1.1; } please guide me for correct formatting for adding socket connection for nginx. overall both services working.

also is spis software create any file name spis.sock or not, as my spis service is working & not able to find any sock file in given folder location.

id4vip commented 1 year ago

just want to confirm ipaddress & port method works in nginx & websocket is not working. please guide me toward the correct formatting of websocket approach. I tried to remold author nginx file into my required folder & websocket format is not working. so something in websocket formatting is wrong. overall with ip:port approach spis functionality works ` location / {

proxy_pass http://unix:/mnt/dietpi_userdata/spis/spis.sock;

proxy_pass http://192.168.1.150:8085;
proxy_http_version 1.1;

} `

gbbirkisson commented 1 year ago

Please guide me how can access spis whole system from my network ...

I'm afraid you will have to figure that out on your own.

what is the relation ship between "SPIS_API_MEDIA_PATH" variable & "SPIS_MEDIA_DIR" ...

So a valid configuration could be:

# spis.service
...
Environment="SPIS_MEDIA_DIR=/mnt/dietpi_userdata/plex_data"
Environment="SPIS_API_MEDIA_PATH=/api/media"
...
# nginx.conf
...
     location /api/media { 
         gzip_static on; 
         expires 1y; 
         add_header Cache-Control "public"; 
         alias /mnt/dietpi_userdata/plex_data; 
     }
...

as my spis service is working & not able to find any sock file in given folder location

You are specifically telling SPIS to listen to a port not a socket. Change it so SPIS listens to a socket:

# spis.service
...
-Environment="SPIS_SERVER_ADDRESS=0.0.0.0:8085"
+Environment="SPIS_SERVER_SOCKET=/mnt/dietpi_userdata/spis/spis.sock"
...
id4vip commented 1 year ago

I removed SPIS_SERVER_ADDRESS & reboot system & after reboot spis software generate spis.sock file at given location. in nginx spis conf file i put directy file path even then system not opening the spis default page (error 502). something wrong in formatting for websocket " proxy_pass http://unix:/mnt/dietpi_userdata/spis/spis.sock" please in your side remove all variable in nginx side & put websocket direct address. or you just share your working exact nginx spis.conf file with websocket . your spis software working ok & generating spis.sock file but nginx not displaying your page. even nginx working for my other apps fine only spis websocket having trouble. The template file is incomplete as it shows the variable in bottom & not declared in start up. so this file need to be adjusted. https://github.com/gbbirkisson/spis/blob/main/docker/nginx.conf

id4vip commented 1 year ago

this thing correct & works proxy_pass http://unix:/mnt/my_userdata/myapp/myapp.sock:/; find the solution sock file need to be chmod 777 /mnt/my_userdata/myapp/myapp.sock only then nginx open the webpage. now question arise how i can maintain myapp.sock file to 777 permission. as this file generate after each reboot & system having tendency make this file 755 permission. please guide me how i can permanently maintain this file to 777 permission.

gbbirkisson commented 1 year ago

Run SPIS and nginx with the same user/group and you should not have that problem

id4vip commented 1 year ago

Thanks for help, confirming sips software works as intended with binary download, make service file, configure nginx reverse proxy. everything works seamless. thanks for spis software.