From b26309551d62e360f1e2b5186a57d1f1e3bde1be Mon Sep 17 00:00:00 2001 From: allan Date: Thu, 24 Oct 2024 12:55:58 +0200 Subject: [PATCH] initial commit --- LICENSE | 21 +++++ README.md | 76 +++++++++++++++++ giteainstall | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++ postinstall | 90 ++++++++++++++++++++ version | 1 + 5 files changed, 417 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 giteainstall create mode 100755 postinstall create mode 100644 version diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..57f408b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Allan Christensen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..49194ed --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +## Gitea install script for muliple instances of Gitea for Ubuntu 22.04 and 24.04 server. + +### Prerequisites +Ubuntu 22.04 or higher with Nginx and MariaDB. Nginx can be installed from +[\[Here\]](https://git.x-files.dk/ubuntu-web-server/nginx-install) +and MariaDB can be install from +[\[Here\]](https://git.x-files.dk/ubuntu-database-server/mariadb-install) + +> **Important:** +> +> There are rules to this one. Normally I don't make rules, but I've been pondering how to do this. So here goes: +> +> For each Gitea instance (everytime you run this script) the following rules apply: +> +> Each intance will have it's own user. i.e gitea1, gitea2 etc.\ +> Each intance will have it's own local port. i.e 3001, 3002 etc.\ +> Each instance will have it's own configuration file i.e giteainstance.app.ini\ +> Each instance will have it's own service unit i.e you will end up with several systemctl restart gitea1 and systemctl restart gitea2 restart etc.\ +> Each instance will share the same **Gitea Execuatable**. So if you update please make sure all owners of the other instances are notified. + +### Logic +The first instance will be called:\ +**gitea1** and the local port will be **3001** the database name will be **gitea1db** and the service to restart **gitea1.service**\ +The second instance will be called:\ +**gitea2** and the local port will be **3002** the database name will be **gitea2db** and the service to restart **gitea2.service** + +I think you get the idea. I can only do so much checking regarding your current setup, but the script should be able to do the logic. Meaning if gitea1 allready exists it will choose gitea2 as the next instance. And since you ventured this far you should know how to fix things if they mess up. + +### Download the script +``` +git clone https://git.x-files.dk/ubuntu-web-application/gitea-multi-install.git +``` + +### Usage + cd gitea-multi-install + sudo ./giteainstall [-n] [-p] + +### Example + sudo ./giteainstall -n git.something.xyz -p giteadatabasepwd + +This will create a Gitea site running on port 80 with the domain name you chose under the installation. +Don't worry though this can easily be fixed by modifying the Nginx configuration later. +If you need an example on how to configure Nginx to run this on port 443 I an example +[\[Here\]](https://git.x-files.dk/ubuntu-web-server/nginx-install/src/branch/main/cfg-apps/gitea.443.conf). Also make sure that you correct the /etc/gitea-instance/app.ini to reflect the changes as well i.e change http to https in the app.ini file and then restart Nginx and the Gitea instance. + +### Configuration +Once the script is done browse to http://git.something.xyz and fill out the mandatory fields marked with green. +

+Gitea Configuration +

+ +### Post install +This is needed in order to change a few configurations in app.ini which is first created when you are done with the setup screen. + + cd gitea-install + sudo ./postinstall + +### Custom layout +Gitea takes the favicon in form of a svg file and a png file as fallback for browsers that does not support it. A custom logo can also be used. For all 3 files goes the following. They need to be placed in the folowing directory:\ +\/var\/lib\/gitea\/custom\/public\/assets\/img\/ + +Gitea can also serve a splash page instead of the build in one you will need to create a home.tmpl and place it in the following directory:\ +\/var\/lib\/gitea\/custom\/templates\/ + +### Troubleshooting +Most issues will probably be caused by the fact that we all set up and configure Nginx diffrently. If you encounter problems I would suggest you look at the Nginx configuration I use +[\[Here\]](https://git.x-files.dk/ubuntu-web-server/nginx-install/src/branch/main/cfg/nginx.conf) +and also the default "sites-enabled" Nginx configurarion I use +[\[Here\]](https://git.x-files.dk/ubuntu-web-server/nginx-install/src/branch/main/cfg/default) +when you start troubleshooting. + +### More guides +More guides can be found on [\[wiki.x-files.dk\]](https://wiki.x-files.dk) + +### Last tested +October 10th 2024 on Ubuntu 24.04. diff --git a/giteainstall b/giteainstall new file mode 100755 index 0000000..5f00b6a --- /dev/null +++ b/giteainstall @@ -0,0 +1,229 @@ +#!/bin/bash + +################################################################################################### +# First Created: 25012024 Author: Allan Desc: Installs Gitea on Ubuntu requires Nginx and MariaDB # +################################################################################################### + +# +# Are we root +# +if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi +# +# Are we in the right directory +# +scriptdir="gitea-multi-install" && whereami=$(pwd |awk -F'/' '{print $NF}') +if [ $whereami != $scriptdir ]; then printf "\nWrong directory! Script must be run from $scriptdir\n\n" ; exit ; fi +# +# Define variables and functions +# +red='\033[0;31m' ; bred='\033[1;31m' ; green='\033[0;92m' ; blue='\033[0;36m' ; bold='\033[1m' ; normal='\033[0m' +line (){ for i in {1..50}; do echo -n "$1"; done && printf "\n"; } +giteauser=$(cat /etc/passwd |grep "gitea" |awk -F':' '{print $1}' | sort -V |sed 's/gitea//g' |tail -n 1) +giteadb="db" +giteaport="3000" +gitealocation="/usr/local/bin/gitea" +giteaversion=$(tail -1 version) +nginxfiles="/etc/nginx/nginxsnippets" +# +# Function title +# +title () { printf "\nGitea multi instance install script V1.0 \n\n"; } +# +# Function usage +# +usage () { clear ; printf -- "\n" +printf -- "${bold}giteamultiinstall${normal} \n\n" +printf -- "${bold}Usage:${normal} \n" +printf -- "./giteainstall [-n] \n" +printf -- " [-p] \n" +printf -- " [-help] \n\n" +printf -- "${bold}Examples:${normal} \n" +printf -- "./giteamultiinstall -n git.something.xyz -p giteadatabsepwd\n\n"; } +# +# Function to check if a service is already running or not +# +serviceno () { printf "\n$service is not running cannot continue...\n\n"; } +servicedead () { status=$(systemctl is-active $service); if [[ "$status" != "active" ]]; then serviceno ; exit; fi; } +# +# Function input check +# +inputcheck () { +if [[ -z "$hostname" ]]; then usage ; printf "${bold}Error! ${normal}Hostname Empty...\n\n" ; exit; fi +if [[ -z "$giteapwd" ]]; then usage ; printf "${bold}Error! ${normal}Database Password Empty...\n\n" ; exit; fi; } +# +# If Nginx and MariaDB is not running then die +# +service="mariadb" ; servicedead ; service="nginx" ; servicedead +# +# Display title +# +clear ; title +# +# Configure command line options +# +if [[ ! $@ =~ ^\-.+ ]]; then usage; fi + +while getopts "n:p:h:" option; do + case $option in + + n) # hostname + hostname=$(echo ${OPTARG} | tr '[:upper:]' '[:lower:]');; + + p) # database password + giteapwd=$OPTARG;; + + h) # display help + usage ; exit;; + + \?) # invalid option + printf "Type $0 -help for help\n\n" ; exit;; + esac +done +# +# Check if input conditions are met +# +inputcheck $hostname $giteapwd +# +# Check if a Nginx conf file with the hostname allready exists +# +hostcheck=$(grep -r --exclude="giteainstall" --exclude="postinstall" --exclude="README.md" $hostname /etc/nginx/ *) +if [ ! -z "$hostcheck" ]; then printf "Found a configuration file already containing $hostname in /ect/nginx/* going to abort...\n" ; exit ; fi +# +# Download gitea only if gitea does not allready exists in /etc/gitea +# +if [ ! -f $gitealocation ] +then wget --no-verbose https://dl.gitea.com/gitea/$giteaversion/gitea-$giteaversion-linux-amd64 +mv gitea-$giteaversion-linux-amd64 /usr/local/bin/gitea ; chmod +x /usr/local/bin/gitea +else : ; fi +# +# Check if nginxsnippets exist if not download them +# +if [[ ! -d "$nginxfiles" ]]; then +git clone --quiet https://git.x-files.dk/ubuntu-web-server/nginx-install.git ; cp -R nginx-install/nginxsnippets /etc/nginx +fi +# +# Find the next Gitea user number and port +# +if [ -z "$giteauser" ] +then giteauser="gitea1" ; giteaport="3001" +else nextuser=$(($giteauser + 1)) ; giteaport=$(($giteaport + $nextuser)) ; giteauser="gitea$nextuser" +fi +# +# Create Gitea database +# +giteasql=" +connect mysql +create database $giteauser$giteadb; +GRANT ALL PRIVILEGES ON $giteauser$giteadb.* TO '$giteauser'@'127.0.0.1' IDENTIFIED BY '$giteapwd'; +FLUSH PRIVILEGES; +" +printf '%s\n' "${giteasql[@]}" |sed '1d; $d' > giteasql.sql +mysql < giteasql.sql +# +# Create a Gitea Nginx configuration file +# +curl --silent curl https://git.x-files.dk/ubuntu-web-server/nginx-install/raw/branch/main/cfg-apps/gitea.80.conf > /etc/nginx/conf.d/$hostname.conf +sed -s -i "s/DOMAIN/$hostname/g" /etc/nginx/conf.d/$hostname.conf +# +# Restarting Nginx for changes to take effect +systemctl restart nginx +# +# Create Gitea user +# +adduser --system --group --disabled-password --shell /bin/bash --home /home/$giteauser --gecos 'Git Version Control' $giteauser +# +# Create Gitea standard folders +# +mkdir -p /var/lib/$giteauser/{custom,data,indexers,public,log} +chown $giteauser:$giteauser /var/lib/$giteauser/{data,indexers,log} +chmod 750 /var/lib/$giteauser/{data,indexers,log} +mkdir /etc/$giteauser +chown root:$giteauser /etc/$giteauser +chmod 770 /etc/$giteauser +# +# Create Gitea systemd script +# +giteasystemd=" +[Unit] +Description=Gitea (Git with a cup of tea) +After=syslog.target +After=network.target +Requires=mysql.service + +[Service] +LimitMEMLOCK=infinity +LimitNOFILE=65535 +RestartSec=2s +Type=simple +User=$giteauser +Group=$giteauser +WorkingDirectory=/var/lib/$giteauser/ +ExecStart=/usr/local/bin/gitea web -c /etc/$giteauser/app.ini +Restart=always +Environment=USER=$giteauser HOME=/home/$giteauser GITEA_WORK_DIR=/var/lib/$giteauser +#CapabilityBoundingSet=CAP_NET_BIND_SERVICE +#AmbientCapabilities=CAP_NET_BIND_SERVICE + +[Install] +WantedBy=multi-user.target +" +printf '%s\n' "${giteasystemd[@]}" |sed '1d; $d' > giteasystemd +cat giteasystemd > /etc/systemd/system/$giteauser.service +# +# Gitea customization uncomment if you need a personal touch to your gitea installation +# +mkdir -p /var/lib/$giteauser/custom/templates +mkdir -p /var/lib/$giteauser/custom/public/assets/img +# +# Start Gitea services +# +systemctl daemon-reload +systemctl enable $giteauser +systemctl start $giteauser +# +# Gitea postinstall notice +# +giteanotice=" +RIGHT : Now Go to http://$hostname and modify the following settings: +IMPORTANT: The postinstall script will take care of port configuration of Nginx and Gitea. + +DATABASE SETTINGS --------------------------------------------------------------------------------------- +DATABASE USERNAME = $giteauser +DATABASE PASSWORD = $giteapwd +DATABASE NAME = $giteauser$giteadb + +GENERAL SETTINGS ---------------------------------------------------------------------------------------- +SITE TITLE = YOUR SITE TITLE HERE + +SERVER AND THIRD-PARTY SERVICE SETTINGS ----------------------------------------------------------------- +HIDE EMAIL ADDRESSES BY DEFAULT = CHECKMARK + +ADMINISTRATOR ACCOUNT SETTINGS -------------------------------------------------------------------------- +ADMINISTRATOR USERNAME = ADMINUSERNAME +PASSWORD = ADMINPASSWORD +EMAIL ADDRESS = ADMINEMAIL +--------------------------------------------------------------------------------------------------------- + +IMPORTANT: Once done go back to \"gitea-install\" and run the following as root or using sudo. + + ./postinstall + +NOTE: SSH will be disabled after running postinsall. You can modify this in /etc/$giteauser/app.ini + All other options in /etc/gitea/app.ini are optional and can be modified as you see fit. +--------------------------------------------------------------------------------------------------------- +" +# +# Display post install note +# +clear ; printf '%s\n' "${giteanotice[@]}" +# +# Adding gitea user number to cfg for use with postinstall +# +echo $giteauser > giteainfo ; echo $giteaport >> giteainfo ; echo $hostname.conf >> giteainfo +# +# Cleaning up +# +rm -Rf giteasql.sql version giteasystemd nginx-install +# +# End of script +# diff --git a/postinstall b/postinstall new file mode 100755 index 0000000..afe543f --- /dev/null +++ b/postinstall @@ -0,0 +1,90 @@ +#!/bin/bash + +####################################################################################### +# First Created: 25012024 Author: Allan Desc: Gitea multi instance postinstall script # +####################################################################################### + +# +# Are we root +# +if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo ""; exit 1; fi +# +# Are we in the right directory +# +scriptdir="gitea-multi-install" && whereami=$(pwd |awk -F'/' '{print $NF}') +if [ $whereami != $scriptdir ]; then printf "\nWrong directory! Script must be run from $scriptdir\n\n"; exit 1; fi +# +# Define variables and functions +# +line (){ for i in {1..50}; do echo -n "$1"; done && printf "\n"; } +giteauser=$(head -1 giteainfo) +giteaport=$(head -2 giteainfo |sed '1d') +hostname=$(tail -1 giteainfo) +# +# Function to check if a service is already running or not +# +serviceno () { printf "\n$service is not running cannot continue...\n\n"; } +servicedead () { status=$(systemctl is-active $service); if [[ "$status" != "active" ]]; then serviceno ; exit; fi; } +# +# Function title +# +title () { printf "\nGitea postinstall script V1.0${normal}\n\n"; } +# +# If Gitea is not running then die +# +service="$giteauser" ; servicedead +# +# Change the port Gitea is using in the Nginx configuration and minor tweaks +# +sed -s -i "s/3000/$giteaport/" /etc/nginx/conf.d/$hostname +sed -i 's/# Optional //' /etc/nginx/conf.d/$hostname +# +# Adjusting Gitea configuration parameters +# +cp -Rp /etc/$giteauser/app.ini /etc/$giteauser/app.ini.orig +sed -s -i "s/HTTP_PORT = 3000/HTTP_PORT = $giteaport/" /etc/$giteauser/app.ini +sed -i '/gitea-repositories/a MAX_FILES = 500' /etc/$giteauser/app.ini +sed -i '/gitea-repositories/a FILE_MAX_SIZE = 200' /etc/$giteauser/app.ini +sed -i 's/LEVEL = info/LEVEL = warn/' /etc/$giteauser/app.ini +sed -i 's/MODE = console/MODE = file/' /etc/$giteauser/app.ini +# +# Gitea post configuration values +# +giteaconfig=" +[ui.admin] +USER_PAGING_NUM = 50 +REPO_PAGING_NUM = 50 +NOTICE_PAGING_NUM = 25 +ORG_PAGING_NUM = 25 + +[ui.user] +USER_PAGING_NUM = 50 +REPO_PAGING_NUM = 50 +NOTICE_PAGING_NUM = 25 +ORG_PAGING_NUM = 25 + +[ui] +THEMES = gitea,arc-green + +[other] +SHOW_FOOTER_BRANDING = false +SHOW_FOOTER_VERSION = false +SHOW_FOOTER_TEMPLATE_LOAD_TIME = false +ENABLE_FEED = false +" +printf '%s\n' "${giteaconfig[@]}" |sed '$d' >> /etc/$giteauser/app.ini +# +# Restart Nginx and Gitea in order for changes to take effect +# +systemctl restart nginx ; systemctl restart $giteauser +# +# Cleaning up +# +rm -Rf giteainfo +# +# All done +# +clear ; printf "\n" ; line '-' ; printf "All Done...\n" ; line '-' ; printf "\n" +# +# End of script +# diff --git a/version b/version new file mode 100644 index 0000000..89144db --- /dev/null +++ b/version @@ -0,0 +1 @@ +1.22.3