gitlab服务器上搭建网站

搭建GitLab服务器

GitLab是当前比较流行的源代码管理工具,支持创建自己的GitLab服务器。本文将介绍如何在GitLab服务器上搭建网站。以下是具体步骤。

第一步:安装Nginx

在Ubuntu上安装Nginx非常简单,在终端中输入以下命令即可:

sudo apt-get update

sudo apt-get install nginx

安装完成后,Nginx将自动启动。我们可以输入服务器的IP地址来验证Nginx是否已安装成功。在Web浏览器中输入以下地址:

http://your_server_IP_address

如果您看到Nginx欢迎页面,则说明已成功安装。

第二步:安装GitLab

可以通过以下步骤安装GitLab:

在终端中输入以下命令以添加GitLab仓库:

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

    使用以下命令安装Gitlab:

    sudo apt-get install gitlab-ee

    安装过程可能会花费一些时间。安装完成后,GitLab会自动启动并监听8080端口。

    第三步:配置Nginx

    由于GitLab默认监听8080端口,我们可以使用Nginx对请求进行代理。

    在终端中输入以下命令以创建GitLab配置文件:

    sudo nano /etc/nginx/conf.d/gitlab.conf

    将以下内容添加到文件中:

    upstream gitlab {

    server 127.0.0.1:8080;

    # Download GitLab CE from https://about.gitlab.com/downloads/

    # and change the following line to the location of the downloaded

    # GitLab package file if it exists, if not, remove this line.

    server downloads-packages.s3.amazonaws.com:443;

    }

    server {

    listen 80;

    server_name example.com www.example.com;

    return 301 https://$server_name$request_uri;

    }

    server {

    listen 443 ssl;

    server_name example.com www.example.com;

    server_tokens off;

    root /opt/gitlab/embedded/service/gitlab-rails/public;

    client_max_body_size 100m;

    # individual nginx logs for this gitlab vhost

    access_log /var/log/nginx/gitlab_access.log;

    error_log /var/log/nginx/gitlab_error.log;

    location / {

    # serve static files from defined root folder;.

    # @gitlab is a named location for the upstream fallback; see below

    try_files $uri $uri.html $uri/index.html $uri/index.htm $uri/index.php $uri/index.php;

    }

    location /api/v3/ { # add this

    proxy_set_header Host $http_host; # add this

    proxy_set_header X-Real-IP $remote_addr; # add this

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # add thisproxy_pass http://gitlab/api/v3/; # add this

    }

    location ~ ^/[\w\.-]+/[\w\.-]+/gitlab-lfs/objects/ {

    client_max_body_size 0;

    error_page 494 = @gitlab-workhorse;

    log_not_found off;

    error_log /dev/null;

    access_log /dev/null;

    proxy_read_timeout 300;

    proxy_connect_timeout 300;

    proxy_redirect off;

    proxy_http_version 1.1;

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass http://gitlab;

    }

    location /ci/api/v1/ {

    # needed to have GitLab CI's nginx logic work correctly on 8.17+ (might be needed for >= 8.16 as well)

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass http://127.0.0.1:8080;

    }

    location /uploads/ {

    client_max_body_size 0;

    # for a given url (/uploads/blablabla) first checks whether the file exists in /public/uploads/

    # If it does, that file is served and the request is over

    # If it doesn't, the request is passed on to GitLab

    # This is done because certain upload URLs are actually artifact URLs

    # and we want to serve them from the local filesystem

    try_files /public$uri @gitlab-workhorse;

    }

    location /assets/ {

    # This allow to properly serve `/assets/` over HTTP for ci runners

    # Otherwise /assets/ will try to be served by gitlab-workhorse

    alias /opt/gitlab/embedded/service/gitlab-rails/public/assets/;

    gzip_static on;

    expires max;

    add_header Cache-Control public;

    add_header ETag "";

    # avoid unnecessary requests to gitlab-workhors once the assets are cached by the browser

    add_header "Access-Control-Allow-Origin" "*";

    add_header "Access-Control-Expose-Headers" "ETag";

    add_header "Access-Control-Allow-Methods" "GET";

    add_header "Access-Control-Allow-Headers" "Range";

    }

    location /-/assets {

    gzip_static on;

    expires max;

    add_header Cache-Control public;

    add_header ETag "";

    # avoid unnecessary requests to gitlab-workhors once the assets are cached by the browser

    add_header "Access-Control-Allow-Origin" "*";

    add_header "Access-Control-Expose-Headers" "ETag";

    add_header "Access-Control-Allow-Methods" "GET";

    add_header "Access-Control-Allow-Headers" "Range";

    }

    # GitLab Satellites

    location ~ /[\w\.-]+/[\w\.-]+/gitlab-satellites/ {

    client_max_body_size 0;

    error_page 494 = @gitlab-workhorse;

    log_not_found off;

    error_log /dev/null;

    access_log /dev/null;

    proxy_read_timeout 300;

    proxy_connect_timeout 300;

    proxy_redirect off;

    proxy_http_version 1.1;

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass http://gitlab;

    }

    # gitlab-ci

    location /ci {

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass http://gitlab/ci;

    }

    # Go HTTP Serve

    location /go {

    client_max_body_size 20M;

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass http://127.0.0.1:3000;

    proxy_read_timeout 2m;

    proxy_send_timeout 2m;

    } location @gitlab-workhorse {

    # If you use HTTPS make sure you disable gzip compression

    # to be safe against BREACH attack.

    gzip off;

    # https://github.com/gitlabhq/gitlabhq/issues/694

    # Some requests take more than 30 seconds.

    proxy_read_timeout 3600;

    proxy_connect_timeout 300;

    proxy_redirect off;

    proxy_http_version 1.1;

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_pass http://gitlab-workhorse;

    }

    error_page 404 /404.html;

    error_page 422 /422.html;

    error_page 500 /500.html;

    error_page 502 /502.html;

    error_page 503 /503.html;

    # individual nginx logs for this gitlab vhost

    access_log /var/log/nginx/gitlab_access.log;

    error_log /var/log/nginx/gitlab_error.log;

    }

    请确保将上述代码中的“example.com”替换为您服务器的域名或IP地址。在配置更改后,使用以下命令验证Nginx配置是否无误:

    sudo nginx -t

    如果没有任何错误,请重新加载Nginx:

    sudo service nginx reload

    第四步:创建网站

    现在您可以使用GitLab创建新网站。首先,创建一个新的 Git 存储库,并将网站文件推送到服务器。然后,您可以使用Nginx的“server”块配置新网站。例如,假设您的域名为example.com:

    sudo nano /etc/nginx/conf.d/example.com.conf

    将以下内容添加到文件中:

    server {

    listen 80;

    listen [::]:80;

    # SSL configuration

    #

    listen 443 ssl;

    listen [::]:443 ssl;

    server_name example.com;

    ssl_certificate /etc/ssl/certs/example.com/fullchain.pem;

    ssl_certificate_key /etc/ssl/private/example.com/privkey.pem;

    root /var/www/example.com/public;

    index index.html;

    location / {

    try_files $uri $uri/ =404;

    }

    }

    请确保将上述代码中的“example.com”替换为您的域名或IP地址以及正确的SSL证书路径。然后,创建一个目录,将网站文件复制到该目录中:

    sudo mkdir -p /var/www/example.com/public

    sudo cp -R /path/to/your/files/* /var/www/example.com/public/

    在共享的Git存储库中更新网站文件时,只需再次使用cp命令将它们复制到/var/www/example.com/public/文件夹中。

    现在重启Nginx服务器:

    sudo service nginx restart

    现在您的网站就可以从世界各地的Web浏览器访问了。