Ubuntu18.04 Server安装Nginx+Git服务和独立的svn服务

安装Nginx+Git

需要安装的包有 nginx, fcgiwrap, git. 其中git在Ubuntu18.04 Server安装时已经默认安装了. 需要安装的是前两个

而fcgiwrap是在 universe 区域里面(找一个包时如果不确定是在那个区域, 可以在 https://packages.ubuntu.com/ 上面先查一下

默认的Ubuntu18.04 Server的 /etc/apt/source.list 内容是这样的

deb http://cn.archive.ubuntu.com/ubuntu bionic main 
deb http://cn.archive.ubuntu.com/ubuntu bionic-security main 
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates main 

需要在main后面加上universe, 否则apt install 会找不到 fcgiwrap

deb http://cn.archive.ubuntu.com/ubuntu bionic main universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-security main universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates main universe

然后执行 sudo apt update 后, 就可以通过 sudo apt install fcgiwrap安装了.

创建Git工作目录

这里将git工作目录放置到 /var/www/git , 将目录权限设置为 www-data (和nginx的worker一致)

cd /var/www/
sudo mkdir git
sudo chown -R www-data:www-data git/
cd git/
sudo mkdir sandbox.git
cd sandbox.git/
sudo git --bare init
sudo git update-server-info
sudo chown -R www-data:www-data .

在sandbox.git目录下, 设置目录和文件权限

# 设置目录为755
sudo find . -type d -exec chmod 755 {} +
# 设置文件为644
sudo find . -type f -exec chmod 644 {} +

配置Nginx

修改nginx默认的配置文件

# backup the default config
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default-bak
# Open the file for editing with the command:
sudo vi /etc/nginx/sites-available/default

在默认的 location / {} 后面, 增加下面的内容

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
#增加的内容
    location ~ (/.*) {
        client_max_body_size 0; # Git pushes can be massive, prevent suddenly cut the connection
        auth_basic "Git Login"; # For displaying
        auth_basic_user_file "/var/www/git/htpasswd";
        include /etc/nginx/fastcgi_params; # Include the default fastcgi configs
        fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; # Tells fastcgi to pass the request to the git http backend executable
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT /var/www/git; # The location of all of your git repositories.
        fastcgi_param REMOTE_USER $remote_user;
        fastcgi_param PATH_INFO $1; # Takes the capture group from our location directive and gives git that.
        fastcgi_pass  unix:/var/run/fcgiwrap.socket; # Pass the request to fastcgi
    }

以上的配置, location ~ (/.*) 这个正则匹配会命中所有的访问, 并将括号内的值赋为 PATH_INFO, 这时候访问git的URL为 http://server_ip/sandbox.git .

如果想将访问限制在一个二级目录下, 假设为git目录, 那么需要修改上面的location为

location ~ /git(/.*)

这时候访问的URL就是 http://server_ip/git/sandbox.git

创建密码文件

可以通过 htpasswd -c /var/www/git/htpasswd milton 来创建, 也可以通过 openssl passwd -apr1 生成口令来手动创建

然后重启nginx

sudo systemctl restart/reload nginx

这时候就可以通过git客户端连接测试了.

添加新Git仓库

sudo mkdir sandbox.git
cd sandbox.git/

sudo git --bare init
sudo git update-server-info
sudo chown -R www-data:www-data .
# 设置目录为755
sudo find . -type d -exec chmod 755 {} +
# 设置文件为644
sudo find . -type f -exec chmod 644 {} +

安装SVN服务

安装subversion

sudo apt install subversion

创建svn的仓库目录

cd /var/www/
sudo mkdir svn

在这个目录下创建两个文件 passwd 和 auth, 内容分别如下, 作为共用的用户管理, 将在各个svn仓库的配置中引用这两个文件

/var/www/svn$ more passwd[users]
harry = 111111
sally = 123123


/var/www/svn$ more authz 
[aliases]

[groups]
admin = harry,sally

[/]
@admin = rw

创建一个svn仓库

cd svn/
sudo svnadmin create sandbox

编辑 sandbox/conf/svnserve.conf, 需要修改三处

[general]
anon-access = none
password-db = ../../passwd
authz-db = ../../authz

启动svn服务

# 去掉 --foreground就是后台运行
svnserve -d --foreground -r /var/www/svn/

在客户端就可以通过 svn co svn://server_ip/sandbox/ 来checkout项目了

原文地址:https://www.cnblogs.com/milton/p/11047115.html