Gogs is a self-hosted Git service written in Go. It is easy to run and has low system usage. It aspires to be the easiest, fastest, and easiest way to build self-hosted Git services.
At the end of this tutorial, you will have a running instance of Gogs, which includes a web interface, a management dashboard, and access to operations such as Git pull and push.
To follow this tutorial, you need:
In this step, we will create the backend Gogs database.
After logging in, please make sure your system software package is up to date.
sudo apt-get update
We will use MySQL as the backend server, and then install it. The -y
flag here assumes that all prompts are.
sudo apt-get-y install mysql-server
During the installation process, the system will ask you to enter the password of the database root user. Make sure to use it safely, and remember it, because you will need it later in this tutorial.
Now create and open a file named gogs.sql
. Here, we use the nano
editor, but you can use your favorite text editor.
nano gogs.sql
Paste the following content into the file, then save and close it.
DROP DATABASE IF EXISTS gogs;
CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8 COLLATE utf8_general_ci;
Finally, use MySQL to execute gogs.sql
to create a Gogs database. Replace your_password
with the root password you chose earlier in this step.
Note: There is no space between the -p
sign and the password in this command.
mysql -u root -pyour_password < gogs.sql
To install Gogs from source code, you need to use version control tools such as Git and Mercurial, and then install them.
sudo apt-get-y install mercurial git
If you plan to clone the repository via SSH, you need a functioning SSH server, but fortunately, Ubuntu 14.04 provides a server out of the box.
Because Gogs is written in Go, we need to install it before compiling Gog.
First, we need to set some environment variables for Go. To do this, open the ~/.bashrc
file for editing.
nano ~/.bashrc
Add the following line to the end of the file, then close and save.
export GOPATH=/home/git/go
export GOROOT=/usr/local/src/go
export PATH=${PATH}:$GOROOT/bin
Next, apply your changes.
source ~/.bashrc
Then use wget
to download the latest compiled version of Go from its website. At the time of writing this article, the latest file is go1.4.2.linux-amd64.tar.gz
.
wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
Use tar
to release the archive.
tar zxf go1.4.2.linux-amd64.tar.gz
Change the directory to the $GOROOT
directory we defined in ~/.bashrc
.
sudo mv go $GOROOT
Now, if you enter go
in your terminal:
go
You should see something like this:
Go is a tool for managing Go source code.
Usage:
go command [arguments]
...
Use "go help [topic]"for more information about that topic.
Go has a built-in command get
to easily download the source code of the Go project and all its dependencies. We will use it to download Gogs.
go get-d github.com/gogits/gogs
The source code of Gogs will now go to $GOPATH/src/github.com/gogits/gogs
, so move there.
cd $GOPATH/src/github.com/gogits/gogs
Next, build and generate the binary file. This command may take some time to run.
go build
We will use Supervisor to manage the Gogs service.
First, let's install it.
sudo apt-get-y install supervisor
Let's create a Gogs daemon by creating a Supervisor configuration section. First, create a directory for log files.
sudo mkdir -p /var/log/gogs
Next, we will open the Supervisor configuration file for editing.
sudo nano /etc/supervisor/supervisord.conf
Append the following to the file to create the Gogs section.
[ program:gogs]
directory=/home/git/go/src/github.com/gogits/gogs/
command=/home/git/go/src/github.com/gogits/gogs/gogs web
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/var/log/gogs/stdout.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/var/log/gogs/stderr.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
environment = HOME="/home/git", USER="git"
user = git
This section defines the command we want to execute to start Gogs, use Supervisor to automatically start it, and specify the location of the log file and the corresponding environment variables.
Now restart Supervisor:
sudo service supervisor restart
We can use the following command to check if Gogs is running.
ps -ef | grep gogs
You should see output similar to this.
root 13441343008:55?00:00:00/home/git/go/src/github.com/gogits/gogs/gogs web
You can also verify that the server is running by viewing the stdout.log
file.
tail /var/log/gogs/stdout.log
You should see a line like this:
2015 /03/0914:24:42[I] Gogs: Go Git Service 0.5.16.0301 Beta
You should also be able to access web pages with the URL of http://your_server_ip:3000/
. This will redirect to the installation page, but it has not been filled out yet.
Let's continue to configure Nginx as a reverse proxy so that you can easily bind domain names to Gogs.
First, install Nginx.
sudo apt-get-y install nginx
Next, create an Nginx configuration file for gogs.
sudo nano /etc/nginx/sites-available/gogs
Add the following content and replace your_server_ip
with the IP address of your Tencent CVM. If you are using the domain name of Tencent CVM, you can use your domain name here.
server {
listen 80;
server_name your_server_ip;
proxy_set_header X-Real-IP $remote_addr; # pass on real client IP
location /{
proxy_pass http://localhost:3000;}}
And make a symbolic link to it so that Nginx can use it.
sudo ln -s /etc/nginx/sites-available/gogs /etc/nginx/sites-enabled/gogs
Finally, restart Nginx to activate the virtual host configuration.
sudo service nginx restart
You should now be able to access web pages using URLvhttp://your_server_ip/
without specifying the port.
There is also a simple step to initialize Gogs during the first run.
Visit http://your_server_ip/install
and fill in the following options. Many of these are already filled in for you, but make sure to replace the red variable with the server's value.
In the first part, Gogs requires MySQL, PostgreSQL or SQLite3, fill in:
MySQL
127.0.0.1:3306
root
your_database_password
gogs
In the second part, Gogs General Settings, fill in:
/home/git/gogs-repositories
git
your_server_ip
3000
http://your_server_ip/
Skip the optional email and notification settings, then under "Administrator Account Settings", select the administrator username and password, and include your email address. We will set the admin username to your_admin_username
in the next step.
Finally, click "Install Gogs" and log in.
You are all done! Let's do a simple pull/push test to ensure that Gogs is running properly.
First, go to http://your_server_ip/repo/create
and create a repository named my-test-repo, then click on the "Use README.md to initialize this repository" option .
Now you should be able to clone it. First, go to your home directory.
cd
Next, clone the repository.
git clone http://your_server_ip/your_admin_username/my-test-repo.git
Switch to the repository directory.
cd my-test-repo
Update README.md
.
echo 'I love Gogs!'>> README.md
Submit your changes and push them. This command will ask you for the username and password of Gogs.
git add --all && git commit -m "init commit"&& git push origin master
Now, if you go back to http://your_server_ip/your_admin_username/my-test-repo
, you will see "I like Gogs!" appended to the README line. It's that simple!
For more Ubuntu tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Set Up Gogs on Ubuntu 14.04"
Recommended Posts