CentOS 8下利用Nginx搭建Git Http (Https) 服务器
Centos 8环境下Git Http (Https) 服务器的搭建
·
1、Git Http服务器的搭建
安装nginx
dnf install nginx
安装gitweb
dnf install gitweb
安装fcgiwrap
dnf install fcgiwrap
安装httpd
dnf install httpd
创建git用户登录验证文件
命令为htpasswd -c /etc/nginx/passwd.conf 用户名,这里的用户名为jiangfan。
htpasswd -c /etc/nginx/passwd.conf jiangfan
创建Git仓库目录
mkdir /var/www/git/repository
创建测试仓库
git --bare init /var/www/git/repository/test.git
配置nginx.conf
vim /etc/nginx/nginx.conf
在图示位置中加入下面的代码:
设置Nginx用户为root
user root root;
请求配置
location ~(/.*) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/passwd.conf; # git用户登录信息
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend; # git http服务目录
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /var/www/git/repository; # git仓库地址
fastcgi_param PATH_INFO $1;
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
client_max_body_size 100M; # 客户端请求服务器最大允许大小
}
此时nginx.conf的完整代码
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~(/.*) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/passwd.conf; # git用户登录信息
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend; # git http服务目录
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /var/www/git/repository; # git仓库地址
fastcgi_param PATH_INFO $1;
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
client_max_body_size 100M; # 客户端请求服务器最大允许大小
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
启动fcgiwrap
fcgiwrap -f -s unix:/var/run/fcgiwrap.socket &
启动Nginx
systemctl start nginx.service
测试Git仓库
设置git用户信息
设置邮箱,命令为git config --global user.email 邮箱,这里的邮箱为666@qq.com。
git config --global user.email 666@qq.com
设置名字,命令为git config --global user.email 名字,这里的名字为jiangfan。
git config --global user.name jiangfan
克隆
跳转到根目录
cd /root
执行克隆
git clone http://localhost/test.git
推送
进入测试仓库目录
cd /root/test
创建测试文件
vim test.txt
添加文件
git add .
添加提交的描述信息
git commit -m "first commit"
推送到远程仓库
git push -u origin master
远程克隆
命令为git clone http://公网ip/test.git,这里的xxx.xxx.xxx.xxx需替换为你的公网ip。
git clone http://xxx.xxx.xxx.xxx/test.git
至此,我们已经完成了Git Http服务器的搭建,接下来我们将http升级为https协议。
2、Git Https服务器的搭建
这里采用OpenSSL实现HTTPS协议。
安装openssl
前面可能已安装,为保险起见,执行一下安装命令
dnf install openssl
创建SSL证书
进入Nginx目录
cd /etc/nginx
创建不受信任的SSL Key
openssl genrsa -des3 -out server.key 2048
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
修改Nginx配置
vim /etc/nginx/nginx.conf
在图示位置中加入下面的代码:
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name _;
root /usr/share/nginx/html;
ssl_certificate "/etc/nginx/server.crt"; # ssl证书的pem文件路径
ssl_certificate_key "/etc/nginx/server.key"; # ssl证书的key文件路径
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers PROFILE=SYSTEM;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~(/.*) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/passwd.conf; # git用户登录信息
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend; # git http服务目录
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /var/www/git/repository; # git仓库地址
fastcgi_param PATH_INFO $1;
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
client_max_body_size 100M; # 客户端请求服务器最大允许大小
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
此时nginx.conf的完整代码
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~(/.*) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/passwd.conf; # git用户登录信息
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend; # git http服务目录
fastcgi_param PATH_INFO $1;
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
client_max_body_size 100M; # 客户端请求服务器最大允许大小
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name _;
root /usr/share/nginx/html;
ssl_certificate "/etc/nginx/server.crt"; # ssl证书的pem文件路径
ssl_certificate_key "/etc/nginx/server.key"; # ssl证书的key文件路径
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers PROFILE=SYSTEM;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~(/.*) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/passwd.conf; # git用户登录信息
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend; # git http服务目录
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /var/www/git/repository; # git仓库地址
fastcgi_param PATH_INFO $1;
fastcgi_param REMOTE_USER $remote_user;
include fastcgi_params;
client_max_body_size 100M; # 客户端请求服务器最大允许大小
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
关闭SSL检查
git config --global http.sslVerify false
测试Git仓库
克隆
重启Nginx服务
systemctl restart nginx.service
跳转到根目录
cd /root
删除旧的克隆目录
rm -rf test
重新执行克隆
git clone https://localhost/test.git
远程克隆
git clone https://xxx.xxx.xxx.xxx/test.git
至此,我们的Git Http和Git Https服务器都搭建完成了,既可以用http访问,也可以用https访问,根据实际需要进行选择。
更多推荐
已为社区贡献1条内容
所有评论(0)