Skip to content

Instantly share code, notes, and snippets.

@jniltinho
Last active October 15, 2020 12:15
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jniltinho/280573e10206e31ab1c7 to your computer and use it in GitHub Desktop.
Save jniltinho/280573e10206e31ab1c7 to your computer and use it in GitHub Desktop.
Install Gogs on Debian or Ubuntu
#!/bin/bash
## Install Gogs v0.11.4 + Nginx Webserver + Mysql
## On Debian, Ubuntu 64Bits
## Author: Nilton OS -- www.linuxpro.com.br
## Version: 3.5
### Tested on Ubuntu 16.04 LTS 64Bits
### Tested on Debian 8/9 64Bits
echo 'install_gogs_ubuntu.sh'
echo 'Support Ubuntu/Debian'
echo 'Installs Gogs 0.11.4'
echo 'Requires Ubuntu 16.04+, Debian 8+'
# Check if user has root privileges
if [[ $EUID -ne 0 ]]; then
echo "You must run the script as root or using sudo"
exit 1
fi
MY_IP=$(ip a s|sed -ne '/127.0.0.1/!{s/^[ \t]*inet[ \t]*\([0-9.]\+\)\/.*$/\1/p}' | tr '\n' ' ')
echo -e "Set Server Name Ex: git.domain.com []: \c "
read SERVER_FQDN
echo -e "Set Server IP Ex: $MY_IP []: \c "
read SERVER_IP
echo "" >>/etc/hosts
echo "$SERVER_IP $SERVER_FQDN" >>/etc/hosts
hostnamectl set-hostname $SERVER_FQDN
echo "$SERVER_FQDN" > /proc/sys/kernel/hostname
apt-get update
apt-get install -y wget nginx git-core mysql-client mysql-server
adduser --disabled-login --gecos 'Gogs' git
cd /home/git
wget --no-check-certificate https://dl.gogs.io/0.11.4/linux_amd64.tar.gz
tar -xvf linux_amd64.tar.gz && rm -f linux_amd64.tar.gz
echo -e "Set Password for Gogs in Mysql Ex: gogs_password : \c "
read GOGS_PASS
echo "Enter Mysql root password"
echo "CREATE USER 'gogs'@'localhost' IDENTIFIED BY $GOGS_PASS;" >>/home/git/gogs/scripts/mysql.sql
echo "GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost';" >>/home/git/gogs/scripts/mysql.sql
echo "--------------------"
mysql -p < /home/git/gogs/scripts/mysql.sql
chmod +x /home/git/gogs/gogs
mkdir -p /home/git/gogs/log
chown -R git:git /home/git/gogs
chown -R git:git /home/git/gogs/*
echo ""
echo "Setup Webserver Nginx"
echo "--------------------"
cp /home/git/gogs/scripts/systemd/gogs.service /etc/systemd/system/
sed -i 's|mysqld.service|mysqld.service mysql.service|' /etc/systemd/system/gogs.service
systemctl daemon-reload
systemctl enable gogs.service
systemctl start gogs.service
echo 'server {
listen YOUR_SERVER_IP:80;
server_name YOUR_SERVER_FQDN;
proxy_set_header X-Real-IP $remote_addr; # pass on real client IP
location / {
proxy_pass http://localhost:3000;
}
}' > /etc/nginx/sites-available/gogs.conf
ln -s /etc/nginx/sites-available/gogs.conf /etc/nginx/sites-enabled/gogs.conf
sed -i "s/YOUR_SERVER_IP/$SERVER_IP/" /etc/nginx/sites-available/gogs.conf
sed -i "s/YOUR_SERVER_FQDN/$SERVER_FQDN/" /etc/nginx/sites-available/gogs.conf
service nginx restart
echo ""
echo "Gogs Server App run on port 3000, Nginx on port 80"
echo "Access http://$SERVER_FQDN to continue the installation"
echo ""
echo "Screencast Install: http://www.linuxpro.com.br/2017/04/instalando-gogs-no-ubuntu/"
## Links
## http://gogs.io/docs/installation/install_from_source.html
## http://gogs.io/
## https://github.com/gogits/gogs/
## https://github.com/gogits/gogs/blob/master/conf/app.ini
@filevich
Copy link

This is great! Thanks.

@jniltinho
Copy link
Author

@jpfilevich , Thanks !!! 👍 💯

@Gu1nness
Copy link

Gu1nness commented Jul 5, 2017

Hi!

Nice script!

Just a little note though:
On debian 9 MySQL has been replaced by MariaDB (see https://www.debian.org/releases/stretch/amd64/release-notes/ch-whats-new.en.html#mariadb-replaces-mysql)
Thus, this script will fail when trying to install mysql-client and mysql-server.
However, a transition system has been created (from the link above):

For example, installing the metapackage default-mysql-server will install mariadb-server-10.1. Users who had mysql-server-5.5 or mysql-server-5.6 will have it removed and replaced by the MariaDB equivalent. Similarly, installing default-mysql-client will install mariadb-client-10.1.

It is possible to check the release with

if [[ $(lsb_release -c | grep buster) ]] then
    apt install -y wget nginx git-core mariadb-client mariadb-server
else
    apt install -y wget nginx git-core mysql-client mysql-server
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment