Ubuntu 16.04使用Nginx安装HTTP Git服务器

现在使用ISPProtect扫描Web服务器的恶意软件。 免费试用

Git是一个免费的开源版本控制系统,可用于跟踪代码的更改。 Git允许您为同一应用程序创建许多存储库,并在多个人员之间协调这些文件的工作。 它主要用于软件开发中的源代码管理。

在本文中,我们将学习如何在Ubuntu 16.04上安装带有Nginx的HTTP Git服务器。

要求

  • 新的Ubuntu 16.04服务器安装在您的系统上。
  • 具有root权限的Sudo用户。
  • 在您的服务器上配置静态IP地址192.168.15.189

1. 入门指南

开始之前,您将需要使用最新的稳定版本来更新系统。

您可以通过运行以下命令来执行此操作:

sudo apt-get update -y
sudo apt-get upgrade -y

更新系统后,重新启动系统并使用sudo用户登录。

2. 安装所需的软件包

首先,您将需要安装一些所需的软件包,包括nginx,git,nano和fcgiwrap到您的系统。 您可以通过运行以下命令来安装它们:

sudo apt-get install nginx git nano fcgiwrap apache2-utils -y

一旦安装了所有必需的软件包,您将需要为Git存储库创建一个目录。 您可以通过运行以下命令来执行此操作:

sudo mkdir /var/www/html/git

接下来,给予Git目录的正确许可:

sudo chown -R www-data:www-data /var/www/html/git

完成后,您可以继续配置Nginx Web服务器。

3. 配置Nginx

首先,您需要配置Nginx将Git流量传递给Git。 您可以通过编辑Nginx默认配置文件来执行此操作:

sudo nano /etc/nginx/sites-available/default

更改文件如下所示:

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;


        root /var/www/html/git;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        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, just to make sure nginx doesn't suddenly cut the connection add this.
    auth_basic "Git Login"; # Whatever text will do.
    auth_basic_user_file "/var/www/html/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/html/git; # /var/www/git is 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
}

}

完成后保存并关闭文件。 然后使用以下命令测试Nginx的任何配置错误:

sudo nginx -t

如果一切正常,您应该看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,您将需要创建一个用户帐户,您需要使用它来浏览提交到存储库。 您可以使用htpasswd实用程序创建名称为hitesh的用户:

sudo htpasswd -c /var/www/html/git/htpasswd hitesh

最后,重新启动Nginx以使用以下命令应用所有更改:

sudo systemctl restart nginx

您可以使用以下命令检查Nginx服务器的状态:

sudo systemctl status nginx

您应该看到以下输出:

?? nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-06-20 23:00:11 IST; 51min ago
  Process: 12415 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 7616 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
  Process: 12423 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 12419 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 12427 (nginx)
   CGroup: /system.slice/nginx.service
           ??????12427 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ??????12431 nginx: worker process                           

Jun 20 23:00:11 localhost systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jun 20 23:00:11 localhost systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 20 23:00:11 localhost systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Jun 20 23:00:11 localhost systemd[1]: Started A high performance web server and a reverse proxy server.

4. 创建Git存储库

一旦配置正确,就可以建立Git仓库了。

您可以使用以下命令创建名称为repo.git的存储库:

cd /var/www/html/git
sudo mkdir hitesh.gitt
sudo git –bare initt
sudo git update-server-info
sudo chown -R www-data.www-data .
sudo chmod -R 777 .

接下来,您将需要通过UFW防火墙允许http服务。 默认情况下,UFW在系统中被禁用,因此您需要先启用它。 您可以使用以下命令启用它:

sudo ufw enable

一旦UFW防火墙启用,您可以通过运行以下命令来允许HTTP服务:

sudo ufw allow http

您现在可以通过运行以下命令检查UFW防火墙的状态:

sudo ufw status

好的,这是服务器端配置。 您现在可以转到客户端来测试Git。

5. 客户机上的测试Git

在启动之前,您将需要在客户端系统上安装git。 您可以使用以下命令安装它:

sudo apt-get install git -y

首先,使用以下命令创建本地存储库:

sudo mkdir ~/testproject

接下来,将目录更改为testproject并使用以下命令启动新的远程存储库:

cd ~/testproject
git init
git remote add origin http://[email protected]/hitesh.git

接下来,使用以下命令创建一些文件和目录:

mkdir test1 test2 test3
echo “This is my first repository” > test1/repo1
echo “This is my second repository” > test2/repo2
echo “This is my third repository” > test3/repo3

接下来,运行以下命令将所有文件和目录添加到存储库中:

git add .
git commit -a -m “Add files and directoires”

您应该看到以下输出:

[master 002fac9] Add files and directoires
 3 files changed, 3 insertions(+)
 create mode 100644 repo1
 create mode 100644 repo2
 create mode 100644 repo3

接下来,使用以下命令将所有文件和目录推送到Git服务器:

git push origin master

您应该看到以下输出:

Password for 'http://[email protected]': 
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 422 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To http://[email protected]/hitesh.git
   68f1270..002fac9  master -> master

现在,您的所有文件和目录已经提交到您的Git服务器。

您的Git存储库创建过程现已完成。 您将来可以轻松克隆您的存储库。 您可以使用远程系统上的以下命令克隆您的存储库:

git clone [email protected]:/var/www/html/git/hitesh.git

您应该看到以下输出:

Cloning into 'hitesh'...
[email protected]'s password: 
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (3/3), done.
Receiving objects: 100% (8/8), 598 bytes | 0 bytes/s, done.
remote: Total 8 (delta 0), reused 0 (delta 0)
Checking connectivity... done.

现在,使用以下命令将目录更改为克隆的存储库:

cd hitesh
tree

您应该看到以下输出:

.
|-- test1
|   `-- repo1
|-- test2
|   `-- repo2
`-- test3
    `-- repo3

3 directories, 3 files

centos7安装配置gitlab(使用外部nginx)

1、安装依赖:

sudo yum install curl policycoreutils openssh-server openssh-clients
sudo systemctl enable sshd
sudo systemctl start sshd
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld

2、添加gitlab源:

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

3、安装gitlab-ce

sudo yum install gitlab-ce

如果您不喜欢通过管道脚本安装存储库,您可以在这里找到整个脚本并手动选择并下载包并使用:

curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-XXX.rpm/download
rpm -i gitlab-ce-XXX.rpm

4、配置gitlab:

sudo gitlab-ctl reconfigure
至此gitlab安装成功,默认用户名为root。

5、停止gitlab自带的nginx

打开文件$sudo vi /etc/gitlab/gitlab.rb。
将nginx['enable'] = ture改为nginx['enable'] = false
重启gitlab:sudo gitlab-ctl reconfigure。

6、修改gitlab域名:

打开/etc/gitlab/gitlab.rb文件,将external_url参数修改为自己的域名。

7、添加外部nginx的gitlab配置文件:

vim /etc/nginx/conf.d/gitlab.conf

添加以下内容:

upstream gitlab {
# 7.x 版本在此位置
# server unix:/var/opt/gitlab/gitlab-rails/tmp/sockets/gitlab.socket;
# 8.0 位置
server unix://var/opt/gitlab/gitlab-rails/sockets/gitlab.socket;
}

server {
 listen *:80;

 server_name gitlab.xuwanqiu.com; # 请修改为你的域名

 server_tokens off; # don't show the version number, a security best practice
 root /opt/gitlab/embedded/service/gitlab-rails/public;

 # Increase this if you want to upload large attachments
 # Or if you want to accept large git objects over http
 client_max_body_size 250m;

 # individual nginx logs for this gitlab vhost
 access_log /var/log/gitlab/nginx/gitlab_access.log;
 error_log /var/log/gitlab/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/index.html $uri.html @gitlab;
 }

 # if a file, which is not found in the root folder is requested,
 # then the proxy pass the request to the upsteam (gitlab unicorn)
 location @gitlab {
 # If you use https make sure you disable gzip compression
 # to be safe against BREACH attack

 proxy_read_timeout 300; # Some requests take more than 30 seconds.
 proxy_connect_timeout 300; # Some requests take more than 30 seconds.
 proxy_redirect off;

 proxy_set_header X-Forwarded-Proto $scheme;
 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-Frame-Options SAMEORIGIN;

 proxy_pass http://gitlab;
 }

 # Enable gzip compression as per rails guide: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
 # WARNING: If you are using relative urls do remove the block below
 # See config/application.rb under "Relative url support" for the list of
 # other files that need to be changed for relative url support
 location ~ ^/(assets)/ {
 root /opt/gitlab/embedded/service/gitlab-rails/public;
 # gzip_static on; # to serve pre-gzipped version
 expires max;
 add_header Cache-Control public;
 }

 error_page 502 /502.html;
}

参考资料:

http://blog.csdn.net/peterxiaoq/article/details/73330302
http://www.cnblogs.com/lixiuran/p/6761299.html
https://segmentfault.com/q/1010000003695935?_ea=337139
https://laravel-china.org/topics/2829/centos-7-install-gitlab-ce-community-edition-and-modify-the-default-nginx
https://about.gitlab.com/installation/#centos-7
https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/installation.md
http://jiankg.github.io/2015/06/12/%E5%9C%A8centos7%E4%B8%8A%E6%90%AD%E5%BB%BAgitlab%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF/

awk查看统计Nginx访问日志

nginx日志最好实现每天定时切割下,特别是在访问量比较大的时候,方便查看与处理,如果没切割,可以用sed直接切割,

切割日志

查找7月17日访问log导出到17.log文件中:

cat gelin_web_access.log | egrep "17/Jul/2017" | sed  -n '/00:00:00/,/23:59:59/p' > /tmp/17.log

查看访问量前10的IP

awk '{print $1}' 17.log | sort | uniq -c | sort -nr | head -n 10 

查看访问前10的URL

awk '{print $11}' gelin_web_access.log | sort | uniq -c | sort -nr | head -n 10

查询访问最频繁的URL

awk '{print $7}' gelin_web_access.log | sort | uniq -c | sort -n -k 1 -r | more

查询访问最频繁的IP

awk '{print $1}' gelin_web_access.log | sort | uniq -c | sort -n -k 1 -r | more

根据访问IP统计UV

awk '{print $1}' gelin_web_access.log | sort | uniq -c | wc -l

统计访问URL统计PV

awk '{print $7}' gelin_web_access.log | wc -l

根据时间段统计查看日志

cat gelin_web_access.log | sed -n '/17/Jul/2017:12/,/17/Jul/2017:13/p' | more

Nginx配置basic_auth密码验证

为Nginx添加basic_auth,意思就是访问页面的时候需要弹出来一个用户和密码验证的东西,本文基于CentOS 6

1. 安装密码生成工具htpasswd并生成用户密码文件

yum install httpd-tools               #适用centos
sudo apt-get install apache2-utils    #适用ubuntu

生成用户密码文件

$ htpasswd -c /var/www/html/.htpasswd user1  #回车会要求输入两遍密码,会清除所有用户!
$ htpasswd -bc /var/www/html/.htpasswd user1 password  #不用回车,直接指定user1的密码为password
$ htpasswd -b /var/www/html/.htpasswd user2 password   #添加一个用户,如果用户已存在,则是修改密码
$ htpasswd -D /var/www/html/.htpasswd user2  #删除用户

2. 为Nginx添加basic_auth配置

server {
    listen        80;
#    root        /tmp;
#    index        index.html index.htm;
    server_name    zhukun.net www.zhukun.net;

    location / {
        auth_basic        "input you user name and password";
        auth_basic_user_file    /export/servers/.htpasswd;
        proxy_pass http://127.0.0.1:9000;
    }
}

然后再次访问zhukun.net时便会弹出验证框要求输入用户名和密码。

3. 可能遇到的问题

访问zhukun.net没有弹出验证框怎么办?
首先修改nginx.conf,将日志级别调为info,如下

$ cat /export/servers/nginx-1.12.1/conf/nginx.conf
.......
user  admin;
worker_processes  8;

error_log  logs/error.log info;
......

然后再次访问让其产error_log
看到error_log时会发现有如下错误产生

*69 no user/password was provided for basic authentication, client: 10.12.138.126, server: www.zhukun.net, request: "GET /date_lateral HTTP/1.1", host: "www.zhukun.net"

原因在于

The HTTP Basic authentication works as following:
*) A browser requests a page without user/password.
*) A server response with 401 page, sending realm as well.
   At this stage the 401 code appears in access_log and the message
   “no user/password …” appears in error_log.
*) The browser shows a realm/login/password prompt.
*) If a user will press cancel, then the browser will show the received
   401 page.
*) If the user enters login/password, then the browser repeats the request
   with login/password.

Then until you will exit the browser, it will send these login/password
with all requests in protected hierarchy.

error_page配置的401页面不存在或者指向问题导致的,可以注释掉401配置或者保证401配置指向的文件可用,然后basic_auth便会生效。

ubuntu 16.04升级nginx版本

因为刚刚在 V2EX 看到的 nginx 爆出了中度危险漏洞,于是决定将正在用的nginx服务都升个级喵~

默认 Ubuntu 自带的 nginx 都比较 out, 正确的姿势是从官方源安装

  • 在 /etc/apt/sources.list.d/ 下添加一个 nginx.list 文件,内容如下:
deb http://nginx.org/packages/mainline/ubuntu/ xenial nginx  
deb-src http://nginx.org/packages/mainline/ubuntu/ xenial nginx  
  • 添加 nginx 的 key,并更新 apt
curl http://nginx.org/keys/nginx_signing.key | sudo apt-key add  
sudo apt update  
需要注意的是,Ubuntu 自带的 nginx 系列模组会干扰nginx本体安装,所以先备份配置文件,删除ubuntu的默认模组,再重装nginx
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak  
sudo apt remove nginx nginx-common nginx-full nginx-core  
sudo apt install nginx  
sudo rm /etc/nginx/nginx.conf  
sudo cp /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf  

另外一点是此时 nginx 被 mask 了……解除并重启它:

sudo systemctl unmask nginx  
sudo systemctl start nginx  

测试无误后,加上重启自启动

sudo systemctl enable nginx  

NGINX编译安装动态模块(不需重新编译nginx)

虽然我一贯会自行编译Nginx,而且我多会选用阿里的Tengine或者YiChun Zhang的OpenResty,可难免会遇到一些特殊情况。

例如:我最近接手的一个小项目。其官方运维偏偏选择的是RPM包。如果是Nginx 1.9.11版本之前,我只能选择跟他们的运维商量:“hi,哥们!我需要用到的一些第三方模块必须重新编译安装Nginx呢!” 所幸,他们用的Centos 7.0,而官方的RPM仓库自带的版本已经支持到Nginx 1.10.2啦。我深呼吸一下,考虑到他们的使用习惯,我作出了第二个选择:Nginx动态模块。

可是。。。我真的是第一次操作动态模块的编译和加载。所以,我才会写下这则手记。

我主要参考的三篇文章如下:

  • https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/
  • https://www.nginx.com/resources/wiki/extending/new_config/
  • https://www.nginx.com/resources/wiki/extending/converting/

我这里记录一下ngx_cache_purge的动态编译过程。

cd /usr/local/src #养成源码统一放置的位置,方便你我他。这家在职运维是到处乱丢的,吐槽一下!
nginx -v #返回的是nginx version: nginx/1.10.2, 确保下一步源码版本一致哟
wget -c http://nginx.org/download/nginx-1.10.2.tar.gz
wget -c https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz -O ngx_cache_purge_2.3.tar.gz
tar zxf nginx-1.10.2.tar.gz
tar zxf ngx_cache_purge_2.3.tar.gz

转化动态模块的config文件

因为这个模块比较老,作者并没有对它做config配置的更新。换言之,源码下载后并不能支持编译为动态模块,我们参考官方文档(见上列表),试试看。

cd ./ngx_cache_purge-2.3/
cp config config.bk
vim config
###
if [ "$HTTP_PROXY" = "YES" ]; then
    have=NGX_HTTP_PROXY . auto/have
fi

if [ "$HTTP_FASTCGI" = "YES" ]; then
    have=NGX_HTTP_FASTCGI . auto/have
fi

if [ "$HTTP_SCGI" = "YES" 2]; then
    have=NGX_HTTP_SCGI . auto/have
fi

if [ "$HTTP_UWSGI" = "YES" ]; then
    have=NGX_HTTP_UWSGI . auto/have
fi

ngx_addon_name=ngx_http_cache_purge_module
CACHE_PURGE_SRCS="$ngx_addon_dir/ngx_cache_purge_module.c"

if [ -n "$ngx_module_link" ]; then
    ngx_module_type=HTTP
    ngx_module_name="$ngx_addon_name"
  /  ngx_module_srcs="$CACHE_PURGE_SRCS"

    . auto/module
else
    HTTP_MODULES="$HTTP_MODULES $ngx_addon_name"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $CACHE_PURGE_SRCS"
fi

have=NGX_CACHE_PURGE_MODULE . auto/have
###

现在,我们可以进行动态模块的编译啦。

cd ../nginx-1.10.2
nginx -V
./configure --add-dynamic-module=../ngx_cache_purge-2.3/ ##这一步要注意:必须将之前的配置指令都拷贝过来,否则会有binary不兼容错误。
make modules
ls objs/

一切正常的话,输出会提示将so文件输出到了objs目录下。 他们这台服务器也没有SUDO相关配置,所以我以上都是以root身份直接运行的。您可能需要su指令。

最后,我们加载动态模块测试。

cp objs/ngx_http_cache_purge_module.so /usr/lib64/nginx/modules/
vim /usr/share/nginx/modules/mod-http-cache-purge.conf #该服务器是CENTOS 7.0,我看到nginx.conf中已经存在载入动态模块的include语句,所以就按照约定执行
###
load_module "/usr/lib64/nginx/modules/ngx_http_cache_purge_module.so";
###
systemctl restart nginx

Nginx平滑升级到最新版本

(一)简述:

早上收到nginx最新漏洞的通知,Nginx官方发布最新的安全公告,在Nginx范围过滤器中发现了一个安全问题(CVE-2017-7529),通过精心构造的恶意请求可能会导致整数溢出并且不正确处理范围,从而导致敏感信息泄漏。
当使用Nginx标准模块时,如果文件头从缓存返回响应,允许攻击者获取缓存文件头。在某些配置中,缓存文件头可能包含后端服务器IP地址或其他敏感信息。此外,如果使用第三方模块有潜在的可能导致拒绝服务。

影响版本
Nginx 0.5.6-1.13.2
漏洞等级
中危
Nginx 在官方公告中称发现了一个范围过滤器中的安全问题。通过精心构造的恶意请
求能造成整数溢出,对范围的不当处理会导致敏感信息泄漏。
No. 漏洞名称 漏洞危害
CVE-2017-7529 Nginx range 过滤器整形溢出漏洞 高危

针对 CVE–2017–7529 修复建议
针对 Nginx range 过滤器整形溢出漏洞的修复建议

  1. 下面的配置可以作为暂时的解决办法:
    max_ranges 1;
  2. 建议受影响用户尽快升级至 1.13.3, 1.12.1
  3. 及时安装官方补丁。

虽然临时可以解决,不过还是建议升级到最新的版本,官方建议升级到1.12.1。

(二)具体的升级步骤:

(1)升级和安装nginx第三方模块一样,需要查看原来安装nginx的版本以及编译的参数:

[root@ittestserver1 opt]# /usr/local/nginx2/sbin/nginx -V
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 
built with OpenSSL 1.1.0e  16 Feb 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx2 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_stub_status_module --with-http_v2_module --with-openssl=/tmp/install/openssl-1.1.0e --with-http_v2_module

(2)下载要升级的nginx版本

[root@ittestserver1 soft]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
--2017-07-17 15:41:24--  http://nginx.org/download/nginx-1.12.1.tar.gz
正在解析主机 nginx.org... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ...
正在连接 nginx.org|206.251.255.63|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:981093 (958K) [application/octet-stream]
正在保存至: “nginx-1.12.1.tar.gz”
90% [=================================================>     ] 892,302      265K/s eta(英国中部时100%[======================================================>] 981,093      291K/s   in 3.3s    
2017-07-17 15:41:28 (291 KB/s) - 已保存 “nginx-1.12.1.tar.gz” [981093/981093])

(3)解压ningx下载的压缩包编译make,切记不要make install。

[root@ittestserver1 soft]# tar xf nginx-1.12.1.tar.gz 
[root@ittestserver1 soft]# cd nginx-1.12.1
[root@ittestserver1 nginx-1.12.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@ittestserver1 nginx-1.12.1]# ./configure  --prefix=/usr/local/nginx2 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_realip_module 
--with-http_gzip_static_module 
--with-http_stub_status_module 
--with-http_stub_status_module 
--with-http_v2_module 
--with-openssl=/tmp/install/openssl-1.1.0e 
--with-http_v2_module
checking for OS
 + Linux 2.6.32-358.el6.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
  + using system PCRE library
  + using OpenSSL library: /tmp/install/openssl-1.1.0e
  + using system zlib library
  nginx path prefix: "/usr/local/nginx2"
  nginx binary file: "/usr/local/nginx2/sbin/nginx"
  nginx modules path: "/usr/local/nginx2/modules"
  nginx configuration prefix: "/usr/local/nginx2/conf"
  nginx configuration file: "/usr/local/nginx2/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx2/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx2/logs/error.log"
  nginx http access log file: "/usr/local/nginx2/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@ittestserver1 nginx-1.12.1]# make

由于make的时间比较长,需要稍等下。

(4)make编译完后会在安装目录下生成一个objs目录且在该目录下有一个nginx执行文件。

[root@ittestserver1 nginx-1.12.1]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@ittestserver1 nginx-1.12.1]# ll objs/
总用量 7124
-rw-r--r-- 1 root root   17459 7月  17 15:48 autoconf.err
-rw-r--r-- 1 root root   43530 7月  17 15:48 Makefile
-rwxr-xr-x 1 root root 7152312 7月  17 15:51 nginx
-rw-r--r-- 1 root root    5345 7月  17 15:51 nginx.8
-rw-r--r-- 1 root root    7066 7月  17 15:48 ngx_auto_config.h
-rw-r--r-- 1 root root     657 7月  17 15:48 ngx_auto_headers.h
-rw-r--r-- 1 root root    6242 7月  17 15:48 ngx_modules.c
-rw-r--r-- 1 root root   38232 7月  17 15:51 ngx_modules.o
drwxr-xr-x 9 root root    4096 7月  17 15:48 src

(5)备份原来老的nginx文件

[root@ittestserver1 nginx-1.12.1]# mv /usr/local/nginx2/sbin/nginx /usr/local/nginx2/sbin/nginx.bak
[root@ittestserver1 nginx-1.12.1]# cp objs/nginx
nginx    nginx.8  
[root@ittestserver1 nginx-1.12.1]# cp objs/nginx  /usr/local/nginx2/sbin/

[root@ittestserver1 nginx-1.12.1]# /usr/local/nginx2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx2/conf/nginx.conf test is successful

(6)使用make upgrade替换老的nginx进程

[root@ittestserver1 nginx-1.12.1]# make upgrade
/usr/local/nginx2/sbin/nginx -t
nginx: the configuration file /usr/local/nginx2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx2/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx2/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx2/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx2/logs/nginx.pid.oldbin`

(7)执行/usr/local/nginx2/sbin/nginx -V查看nginx最新的版本及编译的参数

[root@ittestserver1 nginx-1.12.1]# /usr/local/nginx2/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.1.0e  16 Feb 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx2 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_stub_status_module --with-http_v2_module --with-openssl=/tmp/install/openssl-1.1.0e --with-http_v2_module

至此升级完成。

centos7.1安装配置NFS共享文件系统

环境:centos7.1
server:192.168.0.63
client:192.168.0.64 (centos7.1 客户端)
共享文件的目录用户为:web(uid:1000,gid:1000)

一、安装

yum -y install nfs-utils rpcbind

nfs 的配置文件 /etc/expots
共享目录赋予权限:chmod 755 /home/data
vim /etc/exports
/home/data 192.168.0.0/24(rw,async,insecure,anonuid=1000,anongid=1000,no_root_squash)

二、使配置生效

exportfs -rv

配置文件说明:

/opt/test 为共享目录

  • 192.168.1.0/24 可以为一个网段,一个IP,也可以是域名,域名支持通配符 如: *.com
  • rw:read-write,可读写;
  • ro:read-only,只读;
  • sync:文件同时写入硬盘和内存;
  • async:文件暂存于内存,而不是直接写入内存;
  • no_root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。显然开启这项是不安全的。
  • root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,拥有匿名用户权限,通常他将使用nobody或nfsnobody身份;
  • all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限;
  • anonuid:匿名用户的UID值
  • anongid:匿名用户的GID值。备注:其中anonuid=1000,anongid=1000,为此目录用户web的ID号,达到连接NFS用户权限一致。
  • defaults 使用默认的选项。默认选项为rw、suid、dev、exec、auto nouser与async。
  • atime 每次存取都更新inode的存取时间,默认设置,取消选项为noatime。
  • noatime 每次存取时不更新inode的存取时间。
  • dev 可读文件系统上的字符或块设备,取消选项为nodev。
  • nodev 不读文件系统上的字符或块设备。
  • exec 可执行二进制文件,取消选项为noexec。
  • noexec 无法执行二进制文件。
  • auto 必须在/etc/fstab文件中指定此选项。执行-a参数时,会加载设置为auto的设备,取消选取为noauto。
  • noauto 无法使用auto加载。
  • suid 启动set-user-identifier设置用户ID与set-group-identifer设置组ID设置位,取消选项为nosuid。
  • nosuid 关闭set-user-identifier设置用户ID与set-group-identifer设置组ID设置位。
  • user 普通用户可以执行加载操作。
  • nouser 普通用户无法执行加载操作,默认设置。
  • remount 重新加载设备。通常用于改变设备的设置状态。
  • rsize 读取数据缓冲大小,默认设置1024。–影响性能
  • wsize 写入数据缓冲大小,默认设置1024。
  • fg 以前台形式执行挂载操作,默认设置。在挂载失败时会影响正常操作响应。
  • bg 以后台形式执行挂载操作。
  • hard 硬式挂载,默认设置。如果与服务器通讯失败,让试图访问它的操作被阻塞,直到服务器恢复为止。
  • soft 软式挂载。服务器通讯失败,让试图访问它的操作失败,返回一条出错消息。这项功能对于避免进程挂在无关紧要的安装操作上来说非常有用。
  • retrans=n 指定在以软方式安装的文件系统上,在返回一条出错消息之前重复发出请求的次数。
  • nointr 不允许用户中断,默认设置。
  • intr 允许用户中断被阻塞的操作并且让它们返回一条出错消息。
  • timeo=n 设置请求的超时时间以十分之一秒为单位。
  • tcp 传输默认使用udp,可能出现不稳定,使用proto=tcp更改传输协议。客户端参考mountproto=netid

(以上内容:参考:man nfs)

三、启动nfs

systemctl enable rpcbind
systemctl start rpcbind
systemctl enable nfs-server
systemctl start nfs-server

确认NFS服务器启动成功:

rpcinfo -p
查看具体目录挂载权限
cat /var/lib/nfs/etab

四、客户端挂载:

1、linux客户端挂载:

在从机上安装NFS 客户端

首先是安裝nfs,然后启动rpcbind服务

systemctl enable rpcbind.service

systemctl start rpcbind.service

注意:客户端不需要启动nfs服务

检查 NFS 服务器端是否有目录共享:

showmount -e nfs服务器的IP
showmount -e 192.168.0.63     

客户端挂载#开机自动挂载

vim /etc/fstab  
192.168.0.63:/home/data    /home/data     nfs4 rw,hard,intr,proto=tcp,port=2049,noauto    0  0
手工挂载:
mount -t nfs 192.168.0.63:/home/data /home/data
#查看是否挂载成功。
df -h 
NFS默认是用UDP协议,换成TCP协议达到稳定传输目的:
mount -t nfs 192.168.0.63:/home/data /home/data -o proto=tcp -o nolock

2、windows客户端挂载:

  • Win7自带的NFS客户端可以在“控制面板”->“程序”->“WIndows 功能”找到->nfs-安装。

  • 由于自带的客户端功能少,缺少用户名映射,功能,所以必然会遇到权限的问题。所以需要自行配置权限问题

获取nfs server 用户web的gid和uid,并记录uid和gid,当前为:1000

打开注册表编辑器,找到HKEY_LOCAL_MACHINESOFTWAREMicrosoftClientForNFSCurrentVersionDefault,添加两个REG_DWORD值,填上uid和gid(10进制)完成后重启电脑

注册表导出是如下格式 :

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESOFTWAREMicrosoftClientForNFSCurrentVersionDefault]
"AnonymousGid"=dword:000003e8
"AnonymousUid"=dword:000003e8
  • 挂载到Z盘
mount -o anon mtype=soft lang=ansi  \192.168.0.63homedata  Z:

事项:一定要用软装载模式(mtype=soft),防止资源管理器停止响应,不能用utf-8

参考:http://nfs.sourceforge.net/nfs-howto/index.html

Nginx负载均衡NFS配置

Nginx配置

首先在两台服务器上部署同一个项目,例如下:
测试网站节点1: http://192.168.168.61/nfstest/
测试网站节点2: http://192.168.64.145/nfstest/

在主站进行nginx配置

upstream nfstest {
    server 192.168.64.145:9575 weight=5;
    server 192.168.168.61:80 weight=5; 
    fair;                           
}

现在负载均衡初步完成了。upstream按照轮询(默认)方式进行负载,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。虽然这种方式简便、成本低廉。但缺点是:可靠性低和负载分配不均衡。适用于图片服务器集群和纯静态页面服务器集群。

upstream还有其它的分配策略,分别如下:

weight(权重)

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。如下所示,10.0.0.88的访问比率要比10.0.0.77的访问比率高一倍。

upstream linuxidc{ 
      server 10.0.0.77 weight=5; 
      server 10.0.0.88 weight=10; 
}

ip_hash(访问ip)

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

upstream favresin{ 
      ip_hash; 
      server 10.0.0.10:8080; 
      server 10.0.0.11:8080; 
}

fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。与weight分配策略类似。

 upstream favresin{      
      server 10.0.0.10:8080; 
      server 10.0.0.11:8080; 
      fair; 
}

url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
注意:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。

 upstream resinserver{ 
      server 10.0.0.10:7777; 
      server 10.0.0.11:8888; 
      hash $request_uri; 
      hash_method crc32; 
}

upstream还可以为每个设备设置状态值,这些状态值的含义分别如下:

  • down 表示单前的server暂时不参与负载.
  • weight 默认为1.weight越大,负载的权重就越大。
  • max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.
  • fail_timeout : max_fails次失败后,暂停的时间。
  • backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
upstream bakend{ #定义负载均衡设备的Ip及设备状态 
      ip_hash; 
      server 10.0.0.11:9090 down; 
      server 10.0.0.11:8080 weight=2; 
      server 10.0.0.11:6060; 
      server 10.0.0.11:7070 backup; 
}

NFS配置

环境:
两台服务器之间能正常通信
192.168.64.145 A服务器(文件实际保存)
192.168.168.61 B服务器

一、A服务器配置

1、安装NFS

使用rpm -qa | grep nfs 与 rpm -qa | grep rpcbind 可以查看是否有安装。
在CentOS内可以使用『yum install nfs-utils 』来安装。

2、设置服务机上的共享目录

  [root@www ~]# vi /etc/exports 
  /chroot/www/nfstest/WebContent/source 192.168.168.61(rw)
  #为192.168.168.61读写操作source目录权限
  /chroot/www/nfstest/WebContent/source *(rw,no_root_squash) 
  #任何人都可以应用source目录

3、启动NFS

设定文档后,开始启动, NFS启动之前需要先启动rpcbind才行。

  #如果rpcbind本来就已经在执行了,那就不需要启动啊!
  [root@www ~]# /etc/init.d/rpcbind start 
  #启动nfs
  [root@www ~]# /etc/init.d/nfs start 
  #启动nfslock
  [root@www ~]# /etc/init.d/nfslock start 
  [root@www ~]# chkconfig rpcbind on 
  [root@www ~]# chkconfig nfs on 
  [root@www ~]# chkconfig nfslock on

4、NFS 的连线观察

[root@www ~]# showmount [-ae] [hostname|IP]
选项与参数:

-a :显示目前主机与用户端的NFS 连线分享的状态;
-e :显示某部主机的/etc/exports 所分享的目录资料。

显示出刚刚所设定好的相关exports分享目录资讯

  [root@iZuf6ixy03u72vzno4jsiuZ ~]# showmount -e localhost
  Export list for localhost:
  /chroot/www/nfstest/source (everyone)
  /tmp                       (everyone)

二、B服务器配置

1.启动必备的服务
若没有启动才启动,有启动则保持原样不动

[root@clientlinux ~]# /etc/init.d/rpcbind start 
[root@clientlinux ~]# /etc/init.d/nfslock start 

2.查询A服务器提供哪些资源供使用

[root@jstu565zbb65jg ~]# showmount -e 192.168.64.145
Export list for 192.168.64.145:
/chroot/www/nfstest/source (everyone)<==这是等一下要挂载的目录
/tmp
                       (everyone)

3.建立挂载点

[root@clientlinux ~]# mkdir -p /chroot/www/nfstest/source 
[root@clientlinux ~]# mount -t nfs 192.168.64.145:/chroot/www/nfstest/source  /chroot/www/nfstest/source

4.卸载挂载点

[root@clientlinux ~]# umount /chroot/www/nfstest/source

推荐文档:
http://linux.vbird.org/linux_server/0330nfs.php#nfsserver_need

Linux NFS共享文件系统安装配置

一、NFS服务简介

NFS 是Network File System的缩写,即网络文件系统。一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布。功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在类Unix系统间实现磁盘文件共享的一种方法。

NFS 的基本原则是“容许不同的客户端及服务端通过一组RPC分享相同的文件系统”,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。

NFS在文件传送或信息传送过程中依赖于RPC协议。RPC,远程过程调用 (Remote Procedure Call) 是能使客户端执行其他系统中程序的一种机制。NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络进行资料的分享,这是因为NFS使用了一些其它的传输协议。而这些传输协议用到这个RPC功能的。可以说NFS本身就是使用RPC的一个程序。或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS SERVER或者NFS CLIENT。这样SERVER和CLIENT才能通过RPC来实现PROGRAM PORT的对应。可以这么理解RPC和NFS的关系:NFS是一个文件系统,而RPC是负责负责信息的传输。

二、系统环境

系统平台:CentOS release 6.6
NFS Server IP:192.168.11.100

NFS Client IP:192.168.11.110
防火墙已关闭/iptables
SELINUX=disabled

三、安装NFS服务

NFS的安装是非常简单的,只需要两个软件包即可,而且在通常情况下,是作为系统的默认包安装的。
nfs-utils-* :包括基本的NFS命令与监控程序
rpcbind-* :支持安全NFS RPC服务的连接

1、查看系统是否已安装NFS

未分类

2、如果没有安装NFS软件包,则可以手动安装

# yum -y install nfs-utils rpcbind

3、安装完成后启动服务。

 注:启动时要先启动rpcbind在启动nfs

未分类

4、停止服务时也要先停止nfs在停止rpcbind

四、NFS配置文件

NFS的常用目录

  • /etc/exports NFS服务的主要配置文件

  • /usr/sbin/exportfs NFS服务的管理命令

  • /usr/sbin/showmount 客户端的查看命令

  • /var/lib/nfs/etab 记录NFS分享出来的目录的完整权限设定值

  • /var/lib/nfs/xtab 记录曾经登录过的客户端信息

4.1、NFS服务的配置文件为 /etc/exports,这个文件是NFS的主要配置文件,不过系统并没有默认值,所以这个文件不一定会存在,可能要使用vim手动建立,然后在文件里面写入配置内容。
/etc/exports文件内容格式:

[客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)]

例:

vim /etc/exports

/home/data 192.168.11.0/24(rw,sync,no_root_squash)

4.2、常用配置项说明

访问权限选项
设置输出目录只读:ro
设置输出目录读写:rw

用户映射选项

  • all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);

  • no_all_squash:与all_squash取反(默认设置);

  • root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置);

  • no_root_squash:与rootsquash取反;

  • anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);

  • anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

其它选项

  • secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);

  • insecure:允许客户端从大于1024的tcp/ip端口连接服务器;

  • sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;

  • async:将数据先保存在内存缓冲区中,必要时才写入磁盘;

  • wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);

  • no_wdelay:若有写操作则立即执行,应与sync配合使用;

  • subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);

  • no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

五、重启服务

 修改过配置文件后需要重启相关服务
#service rpcbind resatrt
#service nfs restart

六、客户端配置

6.1、客户端可通过showmount -e IP来查看NFS共享状态

未分类

6.2、客户端通过mount挂载

未分类

6.3、客户端取消挂载

umount /home/data
注:如遇到umount.nfs: /home/data: device is busy可使用-l参数来取消挂载
umount -l /home/data

6.4、挂载完成后,客户端和服务端做的修改都会及时同步

七、配置自动挂载nfs文件系统

 vim /etc/fstab

未分类

保存重启

八、有关权限的介绍

  1. 客户端连接时候,对普通用户的检查

    a. 如果明确设定了普通用户被压缩的身份,那么此时客户端用户的身份转换为指定用户;

    b. 如果NFS server上面有同名用户,那么此时客户端登录账户的身份转换为NFS server上面的同名用户;

    c. 如果没有明确指定,也没有同名用户,那么此时 用户身份被压缩成nfsnobody;

  2. 客户端连接的时候,对root的检查

    a. 如果设置no_root_squash,那么此时root用户的身份被压缩为NFS server上面的root;

    b. 如果设置了all_squash、anonuid、anongid,此时root 身份被压缩为指定用户;

    c. 如果没有明确指定,此时root用户被压缩为nfsnobody;

    d. 如果同时指定no_root_squash与all_squash 用户将被压缩为 nfsnobody,如果设置了anonuid、anongid将被压缩到所指定的用户与组;

九、相关命令

1、exportfs
如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?这个时候我们就可以用exportfs 命令来使改动立刻生效,该命令格式如下:

  • exportfs [-aruv]

  • -a 全部挂载或卸载 /etc/exports中的内容

  • -r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab

  • -u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)

  • -v 在export的时候,将详细的信息输出到屏幕上。

具体例子:

  • exportfs -au 卸载所有共享目录

  • exportfs -rv 重新共享所有目录并输出详细信息

2、nfsstat
查看NFS的运行状态,对于调整NFS的运行有很大帮助。

3、rpcinfo
查看rpc执行信息,可以用于检测rpc运行情况的工具,利用rpcinfo -p 可以查看出RPC开启的端口所提供的程序有哪些。

4、showmount

  • -a 显示已经于客户端连接上的目录信息

  • -e IP或者hostname 显示此IP地址分享出来的目录

5、netstat

  • 可以查看出nfs服务开启的端口,其中nfs 开启的是2049,portmap 开启的是111,其余则是rpc开启的。

  • 最后注意两点,虽然通过权限设置可以让普通用户访问,但是挂载的时候默认情况下只有root可以去挂载,普通用户可以执行sudo。

  • NFS server 关机的时候一点要确保NFS服务关闭,没有客户端处于连接状态!通过showmount -a 可以查看,如果有的话用kill killall pkill 来结束,(-9 强制结束)