diff options
author | Ivan Davydov <lotigara@lotigara.ru> | 2025-03-23 16:07:46 +0300 |
---|---|---|
committer | Ivan Davydov <lotigara@lotigara.ru> | 2025-03-28 10:41:24 +0300 |
commit | a0f5891f7ebaef61c8ea362b4540d027f633849d (patch) | |
tree | 97571e7e46bf06af8d2f005ffbc54ca7a4aeb79d | |
parent | 553e884a45de6f67fb842dd281c662db1000cce3 (diff) |
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.
-rw-r--r-- | .env | 0 | ||||
-rw-r--r-- | .env.example | 4 | ||||
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | README.md | 51 | ||||
-rwxr-xr-x | get-cert.sh | 24 | ||||
-rw-r--r-- | libretranslate.service | 18 | ||||
-rw-r--r-- | libretranslate.service.example | 18 | ||||
-rw-r--r-- | nginx | 9 | ||||
-rw-r--r-- | nginx.example | 9 | ||||
-rwxr-xr-x | run.sh | 11 | ||||
-rwxr-xr-x | setup.sh | 73 | ||||
-rwxr-xr-x[-rw-r--r--] | update.sh | 45 |
12 files changed, 171 insertions, 94 deletions
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 @@ -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 <your hostname or IP address> - -# 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 <<EOF +# The directory where LibreTranslate will be located +LT_DIR="/var/lib/libretranslate" +# The user LibreTranslate will run under +LT_USER="libretranslate" +# The domain name LibreTranslate will be accessible on +LT_DOMAIN="lt.example.com" +EOF + +# Use the automated install +./setup.sh + +# Ensure that LibreTranslate runs sudo systemctl status libretranslate -# Enable https -~/LibreTranslate-init/get-cert.sh - +# Enable SSL after testing (powered by Certbot and Let's Encrypt) +./get-cert.sh ``` #### Tutorials diff --git a/get-cert.sh b/get-cert.sh index a5b9f81..ef9d3ed 100755 --- a/get-cert.sh +++ b/get-cert.sh @@ -1,7 +1,21 @@ -# Setup TLS with certbot and Let's Encrypt -# https://certbot.eff.org/ +#!/bin/sh -sudo apt install certbot python3-certbot-nginx -sudo certbot --nginx -sudo certbot renew --dry-run +if [ -f .env ]; then + . ./.env +else + . ./.env.example +fi +# --- + +echo 'Installing Certbot...' +apt-get install -y certbot python3-certbot-nginx + +if ![$LT_DOMAIN = "_" ]; then + certbot --nginx -d $LT_DOMAIN +else + echo 'Please set the $LT_DOMAIN variable in the .env file to your domain name.' + exit 1 +fi + +echo 'Done! Now you have SSL enabled on your LibreTranslate instance.' diff --git a/libretranslate.service b/libretranslate.service deleted file mode 100644 index 1e28adf..0000000 --- a/libretranslate.service +++ /dev/null @@ -1,18 +0,0 @@ -[Unit] -Description=LibreTranslate WSGI with Gunicorn and Nginx -After=network.target - -[Service] -User=libretranslate -Group=www-data -WorkingDirectory=/home/libretranslate/LibreTranslate -Environment="PATH=/home/libretranslate/LibreTranslate/env/bin" -ExecStart=/home/libretranslate/LibreTranslate/env/bin/gunicorn --workers 3 --bind unix:libretranslate.sock -m 007 wsgi:app -EnvironmentFile=/home/libretranslate/LibreTranslate-init/.env -Restart=always -ExecReload=/bin/kill -s HUP $MAINPID -KillMode=mixed -TimeoutStopSec=1 - -[Install] -WantedBy=multi-user.target diff --git a/libretranslate.service.example b/libretranslate.service.example new file mode 100644 index 0000000..7ae5203 --- /dev/null +++ b/libretranslate.service.example @@ -0,0 +1,18 @@ +[Unit] +Description=LibreTranslate WSGI with Gunicorn and Nginx +After=network.target + +[Service] +User=$LT_USER +Group=www-data +WorkingDirectory=$LT_DIR +Environment="PATH=$LT_DIR/venv/bin" +ExecStart=$LT_DIR/venv/bin/gunicorn --workers 3 --bind unix:libretranslate.sock -m 007 wsgi:app +EnvironmentFile=$LT_DIR/.env +Restart=always +ExecReload=/bin/kill -s HUP $MAINPID +KillMode=mixed +TimeoutStopSec=1 + +[Install] +WantedBy=multi-user.target @@ -1,9 +0,0 @@ -server { - listen 80; - server_name _; - - location / { - include proxy_params; - proxy_pass http://unix:/home/libretranslate/LibreTranslate/libretranslate.sock; - } -} diff --git a/nginx.example b/nginx.example new file mode 100644 index 0000000..69e5d83 --- /dev/null +++ b/nginx.example @@ -0,0 +1,9 @@ +server { + listen 80; + server_name $LT_DOMAIN; + + location / { + include proxy_params; + proxy_pass http://unix:$LT_DIR/libretranslate.sock; + } +} @@ -1,11 +0,0 @@ -# Setup systemd -sudo cp ~/LibreTranslate-init/libretranslate.service /etc/systemd/system/ -sudo systemctl start libretranslate -sudo systemctl enable libretranslate -# sudo systemctl status libretranslate - -# Configure Nginx -sudo cp ~/LibreTranslate-init/nginx /etc/nginx/sites-available/default -sudo nginx -t -sudo systemctl restart nginx - @@ -1,26 +1,63 @@ -sudo apt-get update -sudo apt-get upgrade -y -sudo apt-get install -y vim git +#!/bin/sh -# Install Python -sudo apt-get install -y python3 python3-virtualenv python-is-python3 python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools +if [ -f .env ]; then + set -a + . ./.env + set +a +else + . ./.env.example +fi -# Install PyICU dependencies -# https://community.libretranslate.com/t/pyicu-fails-to-install-on-ubuntu-20-04/23 -sudo apt-get install -y libicu-dev python3-icu pkg-config +# --- -# Install Nginx -sudo apt-get install -y nginx +echo 'Updating package index...' +apt-get update -# Download LibreTranslate source -git clone https://github.com/LibreTranslate/LibreTranslate.git ~/LibreTranslate +echo 'Installing dependencies...' +# Install Python, PyICU dependencies (https://community.libretranslate.com/t/pyicu-fails-to-install-on-ubuntu-20-04/23), Nginx and optionally Certbot with Nginx plugin +apt-get install -y python3 python3-virtualenv python-is-python3 python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools \ +libicu-dev python3-icu pkg-config \ +nginx -# Setup virtualenv -virtualenv ~/LibreTranslate/env +# --- -# Install gunicorn -~/LibreTranslate/env/bin/pip install gunicorn +echo 'Creating LibreTranslate user...' +useradd --system -m --home-dir $LT_DIR $LT_USER +cp $PWD/.env $LT_DIR/.env && chown -R $LT_USER $LT_DIR -# Install and run LibreTranslate on port 5000 -~/LibreTranslate/env/bin/pip install ~/LibreTranslate/ --no-cache-dir +echo 'Downloading and installing LibreTranslate...' +su -ls /bin/sh $LT_USER <<EOF + # Download LibreTranslate source code + git clone https://github.com/LibreTranslate/LibreTranslate.git $LT_DIR/lt + mv $LT_DIR/lt/* $LT_DIR/ + mv $LT_DIR/lt/.* $LT_DIR/ + rmdir $LT_DIR/lt + # Setup virtualenv + python -m venv $LT_DIR/venv + + # Install gunicorn + $LT_DIR/venv/bin/pip install gunicorn + + # Install and run LibreTranslate on port 5000 + $LT_DIR/venv/bin/pip install $LT_DIR --no-cache-dir + $LT_DIR/scripts/install_models.py +EOF + +# --- + +echo 'Configuring system services...' +echo 'Setting up systemd service...' +sed -e "{s|\$LT_DIR|$LT_DIR|;s|\$LT_USER|$LT_USER|}" libretranslate.service.example > 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 index 779c393..9afde02 100644..100755 --- 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 <<EOF + cd $LT_DIR + git pull + . venv/bin/activate + pip install -U gunicorn + pip install $LT_DIR --no-cache-dir -U + if [ "$1" = 'update-models' ]; then + scripts/install_models.py + fi +EOF + +echo 'Re-loading nginx configuration...' +systemctl restart nginx +echo 'Re-loading systemd configuration...' +systemctl daemon-reload +echo 'Starting LibreTranslate...' +systemctl start libretranslate |