Using Git and Github proficiently has become an essential skill for every programmer. Git enables us to manage and maintain our own code better, and enables team members to work in a more efficient way. Github, as a free and easy to use code hosting platform, has also played a very positive role in the development of the open source community to a certain extent.
However, for a variety of reasons, such as github’s unfriendly Internet speed to Chinese users, the team’s private projects do not want to be placed on a third-party server, or the internal environment of the public network is not accessible, some developers choose to build their own private code platform. For this demand, there are also solutions like gitWeb, Gerrit and GitLab. Each of these solutions has its own advantages and disadvantages. Here we introduce the best of them, GitLab, which is also a code management platform being used in our company. If you’re familiar with github usage, this will be familiar.
Buying a server
In order to facilitate the use in the Internet environment, we choose Tencent Cloud lightweight server to build the GitLab server.
Why use tencent lightweight servers of cloud, first, because the price is very high, for new users, 1 nuclear 2 g configuration only need 99 yuan a year, it is a host to the community of desire, second, is also very friendly to the user, provides a variety of images, including docker various preset can be used very quickly.
First of all, we need to buy a Tencent Cloud lightweight server, click here to open the purchase page, select the required configuration, region and duration, select Docker CE 19.03.9 for image, click Buy now, and after payment, we will have our first cloud server.
For ease of use, you also need to register a domain name to access our GitLab servers. At present, due to the policy of the mainland, the use of domain name pointing to the server in Hong Kong needs ICP record (Tencent Cloud will assist to record), and the record time is usually about 10 days. If you can’t wait, you can buy the Hong Kong nodes of lightweight cloud for use, and the use of Hong Kong nodes does not need to be recorded. Please refer here to purchase Hong Kong server, purchase domain name and set resolution. I have registered a domain name lixf.ink and pointed gitlab.lixf.ink to the IP of the server.
After the purchase, go to the console, and on the Lightweight Application Server page, you can see that we have a lightweight server running
It’s best to reset your password for the first time. Click on the image above to enter the summary screen and see the corresponding options. Remember your password after you reset it. I won’t go into details here for lack of space.
Click the “Login” button in the figure above, a black dialog box like the one below will pop up, and the commands we will follow will be input directly in this dialog box, without the need to install a third-party client.
Install gitlab
Now we have our own lightweight server, and we chose Docker CE as the image when we bought it. Therefore, there is a complete Docker environment in the server at this time. Now we can quickly install the GitLab server directly in the way of container.
First, you need to create a directory for gitLab data to store the data generated by GitLab during its run.
sudo mkdir -p /data/gitlab #/data/gitlab can be modified to the appropriate directory
Copy the code
Then, you can start GitLab by running the following command. Be sure to change gitlab.lixf.ink to your own domain name.
sudo docker run --detach \
--hostname gitlab.lixf.ink \
--publish 443:443 --publish 80:80 --publish 23:22 \
--name gitlab \
--restart always \
--volume /data/gitlab/config:/etc/gitlab \
--volume /data/gitlab/logs:/var/log/gitlab \
--volume /data/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
Copy the code
When gitLab is started for the first time, it takes several minutes to complete the installation because some data needs to be initialized internally. In this case, you can enter the following command to view the logs during the installation process
sudo docker logs -f gitlab
Copy the code
Alternatively, you can use the watch command to monitor whether Gitlab is successfully started
sudo watch docker ps
Copy the code
When YOU see HEALTHY in the figure below, gITLab is running successfully
At this point, use a browser to access gitlab.lixf.ink to access our own GitLab.
Resetting the administrator password
It can be seen from the above figure that GitLab has been correctly installed. At this time, since we do not know the account and password of gitLab administrator, we need to return to the black window page (terminal) and then set the password of the administrator.
If the input box for setting the administrator password is displayed on the home page, we can directly set the administrator password on this interface without the following procedure.
Enter the following command to enter the terminal inside the Gitlab container
sudo docker exec -it gitlab /bin/bash
Copy the code
Then go to the Rails console in Gitlab with the following command
gitlab-rails console # This step is slow, about 1 minute, please wait patiently
Copy the code
Enter the following commands to reset the administrator account and password when opening the console.
User = user.find_by_username 'root' user.password="88888888"Copy the code
The setting process is shown in the following figure
After the password is set successfully, you can return to the GitLab login page to log in. The following is the successful login page.
Create and upload the first project
Click the + button in the middle of the navigation bar and choose [New Project] -> [Create Blank Project] in turn to enter the page of creating a Project. Then fill in the corresponding information and click Create Project to Create a New Project.
After successful creation, the project homepage is automatically redirected
Then you can upload the code. The front page of the newly created project already tells us how to initialize and upload our code. We just have to follow through.
The first step is to configure the local Git user and mailbox
git config --global user.name "Administrator" # change your username
git config --global user.email "[email protected]" # Change your email address
Copy the code
Second, pull the repository, add the file, and commit the code
git clone http://gitlab.lixf.ink/root/hellogitlab.git
cd hellogitlab
git switch -c master
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
Copy the code
Here are the results on my computer
Once the code has been submitted, refresh the project home page again to see our submitted code
Here I directly use the administrator user to create and upload the project, in the actual use of the process, it is better to create a separate ordinary user to carry out the corresponding operation.
Configure HTTPS
HTTP is an insecure protocol. Therefore, we configure HTTPS to make our code repository more secure.
Letsencrypt is integrated inside GitLab, so you only need to enable letsEncrypt here and do the necessary configuration
Open the/data/gitlab with vim/config/gitlab rb, modify the following content
-
Around line 32, delete the # in front of external_url and fill in single quotes with the HTTPS address of the Gitlab server, for example, gitlab.lixf.ink
-
At about line 2235, change the following items
Letsencrypt ['enable'] = true # Delete previous #, Letsencrypt ['contact_emails'] = ['[email protected]'] # Letsencrypt ['auto_renew'] = true # Letsencrypt ['auto_renew'] = trueCopy the code
Then, execute the following command to restart the Gitlab container
sudo docker restart gitlab
Copy the code
After the container is restarted, GitLab will automatically issue a free HTTPS certificate through LetsENCRYPT. After the certificate is successfully issued (about 3 minutes), you can access our code repository through Gitlab.lixf.ink. At this point, the address bar will also show a small lock, representing this is a secure URL.
Set SSH port 22
Previously, when starting Docker container, the -p 23:22 parameter was specified to map port 22 of GitLab container to the port of host computer. This is because port 22 of the host has been occupied by the SSHD service. If port 22 is not mapped to another port, the GitLab container will not start. However, in doing so, we will not be able to submit and pull the code through SSH protocol normally, which will bring unnecessary trouble to the friends who are used to SSH protocol.
To solve this problem, we need to modify the port of the SSHD service. Due to space limitations, this section does not specify how to modify the SSHD port. For details, please refer to this blog post to modify the SSHD port. Because the SSHD is our through the terminal (including light cloud bring the black window) to connect server relies on a service, revised its port, will cause light cloud own terminal can’t connect to the server normally, as a result, after modified the SSHD port, behind only use terminal software to connect to the server. Also note that after modifying the port, add the modified port to the lightweight cloud firewall.
If Windows 10 is used, you can use power shell or CMD to connect to Tencent cloud server.
Open a Powershell window, type the following command and then follow the prompts to enter the password to connect to the server
SSH -p 24 [email protected]# change port 24 to the SSHD service modified port, change 1.1.1.1 to the lightweight cloud IP address
Copy the code
After connecting to the server, you need to delete the original container and run the restart command again
sudo docker rm -f gitlab
sudo docker run --detach \
--hostname gitlab.lixf.ink \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume /data/gitlab/config:/etc/gitlab \
--volume /data/gitlab/logs:/var/log/gitlab \
--volume /data/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
Copy the code
Because when we started GitLab, our configuration files, data directories, etc., were mounted to the /data/gitlab directory of the host machine, so even if we deleted and restarted a new container, our previous configuration and uploaded projects would still be valid.
At this point, a GitLab supporting HTTPS and SSH protocols has been built.
summary
This tutorial uses Tencent Cloud lightweight server and The Docker image provided by GitLab to quickly build an available GitLab code warehouse, and through further configuration, make the gitLab correctly support HTTPS protocol and SSH protocol. In the process of building, Tencent Lightweight cloud has its own Docker environment, so that we do not need to build the Docker environment by ourselves. In addition, the Docker environment of lightweight cloud has set the Docker image source as the Docker image source of Tencent cloud, and the speed of pulling gitLab image is also very fast, which also saves the time of waiting for pulling image.