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

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

至此升级完成。

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

Centos7安装配置mariaDB + nginx + php-fpm环境

Centos7 搭建mariaDB + nginx + php环境

系统更新

  • 1.安装开启安装EPEL YUM源
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum makecache
  • 2.系统更新
[root@localhost ~]# yum update
  • 3.解决The remote SSH server rejected X11 forwarding request提示
[root@localhost ~]# yum install xorg-x11-xauth
# 编辑/etc/ssh/sshd_config文件中的X11Forwarding参数为yes
[root@localhost ~]# vim /etc/ssh/sshd_config
  • 4.安装编译组件和依赖
[root@localhost ~]# yum -y install gcc gcc-c++ make unixODBC wxBase wxGTK wxGTK-gl ncurses-devel zlib zlib-devel openssl openssl-devel kernel-devel m4 xmlto net-tools lksctp-tools socat cmake perl perl-JSON libtool pcre pcre-devel yasm yasm-devel libmcrypt libmcrypt-devel libvpx libvpx-devel tiff tiff-devel libpng libpng-devel freetype freetype-devel jpeg jpeg-devel libgd libgd-devel t1lib t1lib-devel gd gd-devel

修改主机名

  • 1.将/etc/hostname中的值修改为localhost或者其他主机名
[root@localhost ~]# vim /etc/hostname
  • 2.在/etc/sysconfig/network 中添加或者修改为HOSTNAME=localhost或者其他主机名
[root@localhost ~]# vim /etc/sysconfig/network
  • 3.将/etc/hosts 中各项值修改为localhost或者其他主机名
[root@localhost ~]# vim /etc/hosts
  • 4.临时修改主机名
[root@localhost ~]# hostname localhost

添加用户

添加新的用户账号使用useradd命令,其语法如下

useradd 选项 用户名

其中各选项含义如下:
代码:

  • -c comment 指定一段注释性描述。
  • -d 目录 指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建主目录。
  • -g 用户组 指定用户所属的用户组。
  • -G 用户组,用户组 指定用户所属的附加组。
  • -s Shell文件 指定用户的登录Shell。
  • -u 用户号 指定用户的用户号,如果同时有-o选项,则可以重复使用其他用户的标识号。
  • 用户名 指定新账号的登录名

  • 1.添加nginx用户

[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -c nginx -g nginx nginx -s /sbin/nologin
  • 2.添加网站专用wwwroot用户
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -c www -g www www -s /sbin/nologin

安装MARIADB

  • 1.使用yum命令安装mariaDB
[root@localhost]# yum -y install mariadb mariadb-server
  • 2.设置开机启动
[root@localhost ~]# systemctl enable mariadb
  • 3.启动MySQL服务
[root@localhost ~]# systemctl start mariadb
  • 4.MariaDB的相关简单配置
[root@localhost ~]# mysql_secure_installation
# 首先是设置密码,会提示先输入密码,初次运行直接回车
Enter current password for root (enter for none):
# 设置密码,是否设置root用户密码,输入y并回车或直接回车
Set root password? [Y/n]
# 设置root用户的密码
New password:
# 再输入一次你设置的密码
Re-enter new password:
# 其他配置
# 是否删除匿名用户,回车
Remove anonymous users? [Y/n]
# 是否禁止root远程登录,回车
Disallow root login remotely? [Y/n]
# 是否删除test数据库,回车
Remove test database and access to it? [Y/n]
# 是否重新加载权限表,回车
Reload privilege tables now? [Y/n]
# 初始化MariaDB完成,接下来测试登录
# mysql -uroot -proot密码
  • 5.授权远程用户登录
# 允许的IP地址,%为任意地址
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'IP地址' IDENTIFIED BY 'youpassword' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

安装PHP

  • 1.安装php
[root@localhost ~]# yum -y install php php-devel
  • 2.安装php扩展
[root@localhost ~]# yum -y install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc
  • 3.安装php-fpm
# 安装 php-fpm
[root@localhost ~]# yum -y install php-fpm
# 设置fpm开机自启动
[root@localhost ~]# systemctl enable php-fpm
# 启动fpm
[root@localhost ~]# systemctl start php-fpm
# php-fpm配置文件路径/etc/php-fpm.conf

安装NGINX

  • 1.下载nginx
# 切换到src目录
[root@localhost ~]# cd /usr/local/src
# 下载nginx源码包
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
# 解压并切换到nginx目录
[root@localhost src]# tar zxvf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
# 配置编译选项
[root@localhost nginx-1.12.0]# ./configure --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-pcre
# 安装
[root@localhost nginx-1.12.0]# make & make install
  • 2.启动关闭nginx
# 常规启动、关闭和重启,不会改变启动时指定的配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
# 设置开机自启动
[root@localhost ~]# vim /lib/systemd/system/nginx.service
# 写入以下内容
# --------------------------------------------------
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
# --------------------------------------------------
[root@localhost ~]# systemctl enable nginx
  • 3.添加php支持
# 修改nginx.conf,去掉以下代码的注释
        location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
# 重启nginx
[root@localhost]# /usr/local/nginx/sbin/nginx -s reload
  • 4.解决访问php提示File not found.
# 修改nginx配置文件
# 源文件
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
# 修改后的文件,将 /scripts 修改为$document_root
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

Ubuntu apt-cache列出版本列表并apt-get安装指定版本

一、通过apt-get安装指定版本

apt-get install <<package name>>=<<version>>

二、查询指定软件有多少个版本

说明:在Linux用这个查询并不能完全的把所有版本都列举出来,因为每个版本都与系统版本和CPU架构有关,比如一个软件支持Ubuntu系统的16.04的CPU架构为amd64的版本只有1.0和1.2,其余都不支持,所以列举时就只有两款。

列举版本列表:

0、通过网站搜索:

https://packages.ubuntu.com/

1、

apt-cache madison <<package name>>

将列出所有来源的版本。如下输出所示:

apt-cache madison vim
vim | 2:7.3.547-1 | http://debian.mirrors.tds.net/debian/ unstable/main amd64 Packages
vim | 2:7.3.429-2 | http://debian.mirrors.tds.net/debian/ testing/main amd64 Packages
vim | 2:7.3.429-2 | http://http.us.debian.org/debian/ testing/main amd64 Packages
vim | 2:7.3.429-2 | http://debian.mirrors.tds.net/debian/ testing/main Sources
vim | 2:7.3.547-1 | http://debian.mirrors.tds.net/debian/ unstable/main Sources
madison是一个apt-cache子命令,可以通过man apt-cache查询更多用法。

2、

apt-cache policy <<package name>>

将列出所有来源的版本。信息会比上面详细一点,如下输出所示:

apt-cache policy gdb
gdb:
  Installed: 7.7.1-0ubuntu5~14.04.2
  Candidate: 7.7.1-0ubuntu5~14.04.2
  Version table:
 *** 7.7.1-0ubuntu5~14.04.2 0
        500 http://fr.archive.ubuntu.com/ubuntu/ trusty-updates/main amd64 Packages
        100 /var/lib/dpkg/status
     7.7-0ubuntu3 0
        500 http://fr.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages
        500 http://archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages

policy是一个apt-cache子命令,可以通过man apt-cache查询更多用法。

3、

apt-cache showpkg <<package name>>

4、

apt-get install -s <<package-name>>

说明:这个命令只是模拟安装时会安装哪些软件列表,但不会例举出每个软件有多少个版本

5、

aptitude versions <<package name>>

6、

apt-show-versions -a <<package name>>

说明:列举出所有版本,且能查看是否已经安装。还可以通过apt-show-versions -u <>来查询是否有升级版本。

7、

whohas -d Debian,Ubuntu <<package name>> | tr -s ' ' 't' | cut -f 1-3 | column -t

8、

rmadison -u debian,ubuntu,bpo <<package name>> | cut -d "|" -f 1-3

单个详情:

1、

apt-cache show <<package name>>

说明:查询指定包的详情,不管是否已经安装。

2、

dpkg -l <<package name>>

说明:效果和上面基本一致,但是结果是列表详情展示,会提示是否已经删除了之后还有依赖包没有删除等。

3、

dpkg -s <<package name>>

说明:必须是安装的包才能显示详情。

4、

dpkg-query -s <<package name>>

说明:同上,效果一致。

使用技巧:

1、可以在查询后面带上一些参数来实现筛选

apt-cache show package | grep Version
apt-show-versions | more

设置Apache 301跳转到https和www

一般我会较多的使用WORDPRESS程序,其在安装的时候我们如果直接用WWW打开,或者在后台设置WWW域名则默认会强制301指向WWW站点域名。而这里有使用ZBLOG或者TYPECHO等其他博客程序则不会默认301跳转。理论上从用户体验,还是从搜索引擎,最好是统一要么WWW,要么不带WWW格式的网址。

所以,我准备在Apache中用301跳转强制WWW格式,这里我是用的Apache虚拟主机环境所以直接在根目录的.htaccess文件设置就可以。

第一、强制WWW跳转

RewriteEngine on
RewriteCond %{HTTP_HOST} ^cnbanwagong.com [NC]
RewriteRule ^(.*)$ http://www.cnbanwagong.com/$1 [L,R=301,NC]

添加到伪静态文件中,立即生效。

第二、强制HTTPS格式

因为HTTPS加密格式网址也在陆续的流行和必须,所以我也在考虑和调整添加HTTPS格式的网址,毕竟免费SSL证书也比较多,添加也不复杂,刚才测试后还是可以的,唯独也需要将HTTPS强制跳转,因为HTTP和HTTPS也最好唯一。

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.cnbanwagong.com/$1 [R,L]

同样的,可以在.htaccess文件中添加强制HTTPS跳转。

第三、补充301跳转

RewriteEngine On
RewriteCond %{HTTP_HOST} !^cnbanwagong.com$ [NC]
RewriteRule ^(.*)$ https://cnbanwagong.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://cnbanwagong.com/$1 [L,R=301]

如果我们有朋友喜欢用不带WWW的格式,所以我们也可以强制不带WWW跳转和HTTPS强制不带WWW。