Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selfhosting #8

Merged
merged 49 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
3e894c7
create local db if not exists
3036662 Oct 26, 2023
9877c13
choose mySql if self_hosted
3036662 Oct 26, 2023
98c58be
include nuget Pamelo Mysql module dependencie
3036662 Oct 26, 2023
dab91b3
return message if now OpenAi token exists
3036662 Oct 26, 2023
baee4d9
use SMTPMailFrom variable if self-hosted
3036662 Oct 26, 2023
fd98596
add httpGet method to show change-password page
3036662 Oct 26, 2023
7ddd41c
added a class UserLocalStorageManager
3036662 Oct 26, 2023
91f8842
dependency injection for local storages
3036662 Oct 26, 2023
456907e
changed a method of parsing string to double
3036662 Oct 26, 2023
05d5239
fix constructors for localStorage managers
3036662 Oct 26, 2023
1b2514e
fix emailSender reset link
3036662 Oct 26, 2023
0d336ae
add reset password methods for self-hosted
3036662 Oct 26, 2023
9845810
fix accidentally doubled injection
3036662 Oct 26, 2023
5a09c73
fix a typo
3036662 Oct 30, 2023
b6a79fb
move dataDir to constructor local variable
3036662 Oct 30, 2023
8108de6
overwrite book cover file if exists
3036662 Oct 30, 2023
8682438
fix a typo
3036662 Oct 30, 2023
15d8659
move dataDir and baseDir to constructor local variables
3036662 Oct 30, 2023
4b46cee
move baseDir and dataDir to constructor local variables
3036662 Oct 30, 2023
4f93c07
overwrite profilePicture if exists
3036662 Oct 30, 2023
684354c
Throw exception if no openAi token exists
3036662 Oct 30, 2023
b3e923c
no return needed after throwing exception
3036662 Oct 30, 2023
c5da3bc
add accidantly deleted filename variable
3036662 Oct 30, 2023
c317980
add accidantly deleted filename variable
3036662 Oct 30, 2023
5ba9469
fix file attributes check when trying to overwrite
3036662 Oct 30, 2023
7438615
fix unreachable code in overwrite error handling
3036662 Oct 30, 2023
3701aa5
log to console when throwing exception (error overwriting file)
3036662 Oct 30, 2023
40516f3
Fix formatting and comments
DavidLazarescu Nov 1, 2023
e0943d1
add install and run instructions for self-hosted linux server
3036662 Nov 1, 2023
ddb270d
del dublicates in apt install command
3036662 Nov 2, 2023
03d355a
fix install.md filename to self-hosted-install.md
3036662 Nov 2, 2023
761683f
add ubuntu install-and-run instruction
3036662 Nov 2, 2023
0b849cc
fixed .md file name for ubuntu
3036662 Nov 2, 2023
a72b8b1
fixed path for configuring kesrel
3036662 Nov 2, 2023
78b4d3a
Merge branch 'Librum-Reader:main' into PR_techSelection
3036662 Nov 2, 2023
13dbd80
fixed a typo
3036662 Nov 2, 2023
be3388c
add sudo to chown command
3036662 Nov 2, 2023
41faf3f
replace apt-get with apt
3036662 Nov 2, 2023
dbd9ef2
Fixed updating highlights
DavidLazarescu Nov 4, 2023
9e683c2
Fixed librum-server.7
DavidLazarescu Nov 4, 2023
09cb68d
Improved language in librum-server.conf
DavidLazarescu Nov 4, 2023
01a1473
Update librum-server.service
DavidLazarescu Nov 4, 2023
bd87485
Delete self-hosting/self-hosted-install.md
DavidLazarescu Nov 4, 2023
5c08229
Improved the installation guide
DavidLazarescu Nov 4, 2023
a5254fb
Fixed dotnet installation on ubuntu
DavidLazarescu Nov 4, 2023
db8eb67
Added missing apt update after adding PPA
DavidLazarescu Nov 4, 2023
32b9e08
Link to dotnet installation page due to problems with the installatio…
DavidLazarescu Nov 4, 2023
52fc183
Added missing groupadd instruction
DavidLazarescu Nov 4, 2023
d06ae7f
Removed unnecessary instruction
DavidLazarescu Nov 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions self-hosting/librum-server.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.TH librum-server

.SH NAME
.B librum-server
- the server for the Librum application


.SH DESCRIPTION
.B librum-server
is a server running as a service

.SH INSTALLATION
.TP
After installing the librum-server package
.RS
.B 1.
Install and configure the MariaDb or MySql service
.RS
.LP
.B a)
Edit /etc/mysql/mariadb.conf.d/50-server.cnf to set bind-address=127.0.0.1 and comment out the skip-networking option
.LP
.B b)
Restart MySql server - systemctl restart mysqld
.LP
.B c)
Run mysql and create a user for the mysql database. For example:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'strongPassword123';
.RE

.LP
.B 2.
Edit the configuration file at /etc/librum-server/librum-server.conf
You must provide:
.RS
.LP
.B JWTValidIssuer
- Any string for key provider for example "myhomeKeyProvider"
.LP
.B JWTKey
- The secret key for JWT token generation (at least 20 symbols)
.LP
.B AdminEmail
- An admin email for seeding the database with an admin account on the first run
.LP
.B AdminPassword
- A password for the admin account (5 symbols minimum)
.LP
.B DBConnectionString
- The connection string for Mysql (or MariaDB)
for example "Server=127.0.0.1;port=3306;Database=my_database_name;Uid=mysql_user;Pwd=mysql_password;"
.LP
.B SMTPEndpoint
- The smtp server endpoint used for sending emails to confirm your account
.LP
.B SMTPUsername and SMTPPassword
- The username and password for your smtp server
.LP
.B SMTPMailFrom
- It is recommended to set this variable to be exactly the same email that you are using on your mail server
.LP
.B CleanUrl
- A clean url without ports, it will be used to build the "reset password link".
As an example, a server running on 127.0.0.1:5000 can be exposed to the web as https://myserver.com, so the CleanUrl would be https://myserver.com
.RE
.LP
.B 3.
Refresh the systemd services by running: systemctl daemon-reload
.LP
.B 4.
Run the server: systemctl start librum-server
.LP
.B 5.
Check status with: systemctl status librum-server
.RE
.LP
.B 6.
Configure your librum-reader app to launch using your server.
In ~/.config/librum-server/librum-server.conf set selfHosted to true and set serverHost to the servers url (e.g. https://127.0.0.1:5001)

.SH UNINSTALL
.TP
Delte the package and in ~/.config/librum-server/librum-server.conf, change selfHosted to false and serverHost to api.librumreader.com to switch back to the official servers.

.SH DIAGNOSTICS
.PP
The activity of server is logged to /var/lib/librum-server/srv/Data/Logs and journalctl.


31 changes: 31 additions & 0 deletions self-hosting/librum-server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Valid issuer for JWT Key - string
JWTValidIssuer="exampleIssuer"

# Secret key for JWT token generation (at least 20 symbols)
JWTKey="exampleOfALongSecretToken"

# An admin email for seeding the database with an admin account on the first run
AdminEmail="admin@example.com"

# A password for the admin account (5 symbols minimum)
AdminPassword="strongPassword123"

# The connection string for Mysql (or MariaDB)
DBConnectionString="Server=127.0.0.1;port=3306;Database=my_database_name;Uid=mysql_user;Pwd=mysql_password;"

# The smtp server endpoint used for sending emails to confirm your account
SMTPEndpoint="smtp.example.com"

# The username and password for your smtp server
SMTPUsername="mailuser123"
SMTPPassword="smtpUserPassword123"

# It is recommended to set this variable to be exactly the same email that you are using on your mail server
SMTPMailFrom="mailuser123@example.com"

# A clean url without ports, it will be used to build the "reset password link".
# As an example, a server running on 127.0.0.1:5000 can be exposed to the web as https://myserver.com, so the CleanUrl would be https://myserver.com
CleanUrl="https://127.0.0.1"

# Your OpenAI api token - If left empty, all Ai services will simply be disabled
OpenAIToken=""
19 changes: 19 additions & 0 deletions self-hosting/librum-server.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Unit]
Description=Server for the Librum application

[Service]
WorkingDirectory=/var/lib/librum-server/srv
ExecStart=/var/lib/librum-server/srv/run.sh
User=librum-server
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=librum-server
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=LIBRUM_SELFHOSTED=true
EnvironmentFile=/etc/librum-server/librum-server.conf

[Install]
WantedBy=multi-user.target
4 changes: 4 additions & 0 deletions self-hosting/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

cd /var/lib/librum-server/srv
dotnet Presentation.dll
203 changes: 203 additions & 0 deletions self-hosting/self-host-installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# Librum-Server
The build and deploy process was tested on Ubuntu 22.04. It should work on any other linux distribution, but the commands might need to be adjusted.

<br>

## Dependencies

You will need `dotnet`, `openssl` and `mariadb-server`.
<br>
<br>
To download dotnet7 follow: https://learn.microsoft.com/en-us/dotnet/core/install/linux (if you run into problems with the dotnet7 installation on ubuntu, this: https://stackoverflow.com/a/77059342 might help).
<br>
<br>
download the other packages via:
```
sudo apt install openssl mariadb-server
```
to install all dependencies.

<br>

## Build

To build the server, clone the repository and use `dotnet publish`

```
git clone https://github.com/Librum-Reader/Librum-Server.git
cd Librum-Server
dotnet restore
cd src/Presentation
dotnet publish -c Release -o build --no-restore --verbosity m

```

<br>

## Install
### Create a `librum-server` group and user

```
groupadd -r -f librum-server
sudo useradd -r -g librum-server -d /var/lib/librum-server --shell /usr/sbin/nologin librum-server
```

### Install the .service file for systemd

```
cd ../..
sudo install -d /etc/systemd/system/
sudo install self-hosting/librum-server.service -m660 /etc/systemd/system/
```

### Install the .conf file that contains the environment variables

```
sudo install -d /etc/librum-server/
sudo install -m660 self-hosting/librum-server.conf /etc/librum-server/
```

### Install the server

```
sudo mkdir -p /var/lib/librum-server/srv
sudo cp src/Presentation/build/* /var/lib/librum-server/srv --recursive
sudo chmod --recursive 660 /var/lib/librum-server/
sudo chmod 770 /var/lib/librum-server
sudo chmod 770 /var/lib/librum-server/srv
sudo install self-hosting/run.sh -m770 /var/lib/librum-server/srv
sudo chown --recursive librum-server /var/lib/librum-server/
```

### Install the manpage

```
mkdir -p /usr/share/man/man7
sudo install -m664 self-hosting/librum-server.7 /usr/share/man/man7
```

### Insall readme

```
sudo install -m664 self-hosting/self-host-installation.md /var/lib/librum-server/srv
```

### Create the SSL certificate for the server

```
KEYOUT=/var/lib/librum-server/srv/librum-server.key
CRTOUT=/var/lib/librum-server/srv/librum-server.crt
PFXOUT=/var/lib/librum-server/srv/librum-server.pfx
sudo /usr/bin/openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -nodes -keyout $KEYOUT -out $CRTOUT -subj "/CN=librum-server" -extensions v3_ca -extensions v3_req
sudo openssl pkcs12 -export -passout pass: -out $PFXOUT -inkey $KEYOUT -in $CRTOUT
sudo chown librum-server $PFXOUT
```

### Configure the server ports

Edit `/var/lib/librum-server/srv/appsettings.json` and change it to look like the following:

```
{
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://127.0.0.1:5000"
},
"Https": {
"Url": "https://127.0.0.1:5001",
"Certificate": {
"Path": "librum-server.pfx"
}
}
}
},
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"AzureKeyVaultUri": "https://librum-keyvault.vault.azure.net/",
"IpRateLimiting": {
"EnableEndpointRateLimiting": true,
"StackBlockedRequests": false,
"RealIpHeader": "X-Real-IP",
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
"GeneralRules": [
{
"Endpoint": "post:/api/register",
"Period": "15m",
"Limit": 6
}
]
}
}
```

<br>

## Run

### Install and configure MariaDB

Edit `/etc/mysql/mariadb.conf.d/50-server.cnf` (called differently on other linux distros e.g. `/etc/my.cnf.d/server.cnf` or `my.cnf`).

Set `bind-adress` to `127.0.0.1` and if a `skip-networking` section exists, comment it out by adding a `#` infront of it.

Then restart the mariaDB service:

```
systemctl restart mysqld
```

#### Create Mysql user and password
For example:

```
sudo mysql_secure_installation

Switch to unix_socket authentication [Y/n] n
Change the root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
```

### Run the librum-server
Firstly you must edit `/etc/librum-server/librum-server.conf` and change the variables following the comments above them.

Then you can run:

```
sudo systemctl daemon-reload
sudo systemctl start librum-server
```

to start the service.

<br>

## Note
- By default the server listens to 5000 (http) and (5001) https. You can chage it in the `/var/lib/librum-server/srv/appsettings.json` file.
- The server stores its files at `/var/librum-server/data_storage`
- Logs are written to `/var/librum-server/srv/Data`

<br>

## Configuration for the client application

By default the Librum client application is set up to use the official servers. To connect it with your self-hosted server, you will need to edit `~/.config/Librum-Reader/Librum.conf` and set `selfHosted=true` and `serverHost` to your server's url (e.g. `serverHost=https://127.0.0.1:5001`).<br>
If there is no file at `~/.config/Librum-Reader/Librum.conf`, make sure that you have ran the application at least once before for the settings files to be generated.
<br>
<br>
To switch back to the official servers, set `selfHosted=false` and `serverHost=api.librumreader.com`

<br>

## Questions

If you have any questions or run into problems which you can't solve, feel free to open an issue.
Loading
Loading