From a0f5891f7ebaef61c8ea362b4540d027f633849d Mon Sep 17 00:00:00 2001 From: Ivan Davydov Date: Sun, 23 Mar 2025 16:07:46 +0300 Subject: Big refactor TL;DR: this commit makes README.md more simple and makes scripts more complex. This commit makes scripts look (and behave) like scripts. That is, they now contain shebangs, `echo` lines to allow user to watch the installation process. The latter is now more configurable: user can customize the directory, where LibreTranslate will be installed, username, which will be used to run LibreTranslate and the domain name configuration is now contained in the .env file, which used for all other settings. --- .env | 0 .env.example | 4 +++ .gitignore | 3 ++ README.md | 51 ++++++++++++++--------------- get-cert.sh | 24 +++++++++++--- libretranslate.service | 18 ----------- libretranslate.service.example | 18 +++++++++++ nginx | 9 ------ nginx.example | 9 ++++++ run.sh | 11 ------- setup.sh | 73 +++++++++++++++++++++++++++++++----------- update.sh | 45 ++++++++++++++++++++++---- 12 files changed, 171 insertions(+), 94 deletions(-) delete mode 100644 .env create mode 100644 .env.example create mode 100644 .gitignore delete mode 100644 libretranslate.service create mode 100644 libretranslate.service.example delete mode 100644 nginx create mode 100644 nginx.example delete mode 100755 run.sh mode change 100644 => 100755 update.sh diff --git a/.env b/.env deleted file mode 100644 index e69de29..0000000 diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..56f006a --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +LT_DIR="/var/lib/libretranslate" +LT_USER="libretranslate" +# Change the value below! +LT_DOMAIN="lt.example.com" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d2019a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.env +/nginx +/libretranslate.service diff --git a/README.md b/README.md index 3535967..e0802a1 100644 --- a/README.md +++ b/README.md @@ -4,42 +4,39 @@ Shell scripts to install [LibreTranslate](https://libretranslate.com) Uses WSGI with [Gunicorn and Nginx](https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04). -Tested on Ubuntu 20.04. +Tested on Ubuntu 20.04 and Debian 12. [Known Python version bug with Ubuntu 24.04](https://github.com/LibreTranslate/LibreTranslate/issues/611#issuecomment-2415239429) and Python 3.12. -## Install +## Installation +Please note that you should run these commands as root by adding `sudo` before every command or by entering the root shell using the `sudo su -` command. ``` -# Create libretranslate user -useradd libretranslate -mkdir /home/libretranslate -chown libretranslate:libretranslate /home/libretranslate -usermod -aG sudo libretranslate -passwd -d libretranslate -su libretranslate +# First of all, update the package cache & upgrade the system +apt update +apt dist-upgrade # Download LibreTranslate-init -git clone https://github.com/argosopentech/LibreTranslate-init.git ~/LibreTranslate-init - -# Download dependencies and run LibreTranslate on port 5000 -~/LibreTranslate-init/setup.sh - -# Add your hostname or IP address to this command -# When you run LibreTranslate for the first time it will download all of the language model packages -~/LibreTranslate/env/bin/libretranslate --host - -# Set server_name to your domain name in ~/LibreTranslate-init/nginx - -# Run LibreTranslate WSGI with nginx and systemd -~/LibreTranslate-init/run.sh - -# Check LibreTranslate status +git clone https://github.com/argosopentech/LibreTranslate-init.git ~/LibreTranslate-init && cd LibreTranslate-init + +# Fill up the .env file with settings +cat > .env < libretranslate.service +cp -v libretranslate.service /etc/systemd/system/ +systemctl enable --now libretranslate + +echo 'Configuring Nginx site...' +sed -e "{s|\$LT_DIR|$LT_DIR|;s|\$LT_DOMAIN|$LT_DOMAIN|}" nginx.example > nginx +cp -v nginx /etc/nginx/sites-available/libretranslate +ln -sv /etc/nginx/sites-available/libretranslate /etc/nginx/sites-enabled/libretranslate +nginx -t && systemctl restart nginx + +# --- + +echo "Done! Now you can visit the http://$LT_DOMAIN website. You can use the ./get-cert.sh script to get an SSL certificate." diff --git a/update.sh b/update.sh old mode 100644 new mode 100755 index 779c393..9afde02 --- a/update.sh +++ b/update.sh @@ -1,6 +1,39 @@ -cd ~/LibreTranslate-init -git pull -rm -rf ~/LibreTranslate/env -virtualenv ~/LibreTranslate/env -~/LibreTranslate/env/bin/pip install ~/LibreTranslate/ --no-cache-dir -~/LibreTranslate/env/bin/libretranslate --update-models +#!/bin/sh + +if [ -f .env ]; then + . ./.env +else + . ./.env.example +fi + +echo 'Stopping LibreTranslate...' +systemctl stop libretranslate + +echo 'Updating the LibreTranslate systemd service according to the .env file...' +sed -e "{s|\$LT_DIR|$LT_DIR|;s|\$LT_USER|$LT_USER|}" libretranslate.service.example > libretranslate.service +cp -v libretranslate.service /etc/systemd/system/ + +echo 'Updating the nginx site according to the .env file...' +sed -e "{s|\$LT_DIR|$LT_DIR|;s|\$LT_DOMAIN|$LT_DOMAIN|}" nginx.example > nginx +cp -v nginx /etc/nginx/sites-available/libretranslate + +cp $PWD/.env $LT_DIR/.env && chown -R $LT_USER $LT_DIR + +echo 'Updating LibreTranslate...' +su -ls /bin/sh $LT_USER <