Ubuntu18.04 Server安装Nginx+Git服务

时间:2019-06-18 19:47:57   收藏:0   阅读:218

安装

需要安装的包有 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 ~ (/.*) {
        root /var/www/git;
        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
    }

创建密码文件

可以通过 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 {} +

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!