Ubuntu18.04 Server安裝Nginx+Git服務和獨立的svn服務

安裝Nginx+Git

須要安裝的包有 nginx, fcgiwrap, git. 其中git在Ubuntu18.04 Server安裝時已經默認安裝了. 須要安裝的是前兩個nginx

而fcgiwrap是在 universe 區域裏面(找一個包時若是不肯定是在那個區域, 能夠在 https://packages.ubuntu.com/ 上面先查一下git

默認的Ubuntu18.04 Server的 /etc/apt/source.list 內容是這樣的ubuntu

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 會找不到 fcgiwrapsocket

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安裝了.ionic

建立Git工做目錄

這裏將git工做目錄放置到 /var/www/git , 將目錄權限設置爲 www-data (和nginx的worker一致)svn

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默認的配置文件spa

# 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 / {} 後面, 增長下面的內容unix

    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 .rest

若是想將訪問限制在一個二級目錄下, 假設爲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項目了

相關文章
相關標籤/搜索