使用Nginx geoip_country GeoIP模块为不同的国家显示不同的内容

如果想屏蔽某个地区的 IP 访问的话,用 iptables 把来自某个国家的 IP 重定向到预定页面不是特别灵活的办法,如果只有一个 IP 可用而有多个网站在同一 VPS 上怎么办?用 iptable 屏蔽某个网站的话也会屏蔽同一 VPS 上的其他网站的访问。所以正统的办法还是用 GeoIP 配合对应的 web 服务器模块,比如:apache + mod_geoip 或者 nginx + http_geoip_module 等。

安装 Nginx

因为要用到 http_geoip_module 模块,系统自带的 nginx 一般不带这个模块,所以要下载 nginx 源代码后自行编译:

# wget http://nginx.org/download/nginx-0.9.6.tar.gz
# tar zxvf nginx-0.9.6.tar.gz
# cd nginx-0.9.6
# ./configure --without-http_empty_gif_module --with-poll_module 
--with-http_stub_status_module --with-http_ssl_module 
--with-http_geoip_module
# make; make install

安装 MaxMind 的 GeoIP 库

MaxMind 提供了免费的 IP 地域数据库(GeoIP.dat),不过这个数据库文件是二进制的,需要用 GeoIP 库来读取,所以除了要下载 GeoIP.dat 文件外(见下一步),还需要安装能读取这个文件的库。

# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
# tar -zxvf GeoIP.tar.gz
# cd GeoIP-1.4.6
# ./configure
# make; make install

刚才安装的库自动安装到 /usr/local/lib 下,所以这个目录需要加到动态链接配置里面以便运行相关程序的时候能自动绑定到这个 GeoIP 库:

# echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
# ldconfig

下载 IP 数据库

MaxMind 提供了免费的 IP 地域数据库,这个数据库是二进制的,不能用文本编辑器打开,需要上面的 GeoIP 库来读取:

# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# gunzip GeoIP.dat.gz

配置 Nginx

最后是配置 nginx,在相关地方加上如下的配置就可以了:

# vi /etc/nginx/nginx.conf

http {
...
geoip_country /home/vpsee/GeoIP.dat;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
...
}

server {
...
        location / {
            root   /home/vpsee/www;
            if ($geoip_country_code = CN) {
                root /home/vpsee/cn;
            }
            ...
        }
...
}

这样,当来自中国的 IP 访问网站后就自动访问到预定的 /home/vpsee/cn 页面。关于 Nginx + GeoIP 还有很多有用的用法,比如做个简单的 CDN,来自中国的访问自动解析到国内服务器、来自美国的访问自动转向到美国服务器等。MaxMind 还提供了全球各个城市的 IP 信息,还可以下载城市 IP 数据库来针对不同城市做处理。

nginx geo根据客户端IP创建变量

geo指令使用ngx_http_geo_module模块提供的。默认情况下,nginx有加载这个模块,除非人为的 –without-http_geo_module。
ngx_http_geo_module模块可以用来创建变量,其值依赖于客户端IP地址。

geo指令

语法: geo [$address] $variable { … }
默认值: —
配置段: http
定义从指定的变量获取客户端的IP地址。默认情况下,nginx从$remote_addr变量取得客户端IP地址,但也可以从其他变量获得。如

geo $remote_addr $geo {
        default 0;
        127.0.0.1 1;
}
geo $arg_ttlsa_com $geo {
        default 0;
        127.0.0.1 1;
}

如果该变量的值不能代表一个合法的IP地址,那么nginx将使用地址“255.255.255.255”。
nginx通过CIDR或者地址段来描述地址,支持下面几个参数:
– delete:删除指定的网络
– default:如果客户端地址不能匹配任意一个定义的地址,nginx将使用此值。 如果使用CIDR,可以用“0.0.0.0/0”代替default。
– include: 包含一个定义地址和值的文件,可以包含多个。
– proxy:定义可信地址。 如果请求来自可信地址,nginx将使用其“X-Forwarded-For”头来获得地址。 相对于普通地址,可信地址是顺序检测的。
– proxy_recursive:开启递归查找地址。 如果关闭递归查找,在客户端地址与某个可信地址匹配时,nginx将使用“X-Forwarded-For”中的最后一个地址来代替原始客户端地址。如果开启递归查找,在客户端地址与某个可信地址匹配时,nginx将使用“X-Forwarded-For”中最后一个与所有可信地址都不匹配的地址来代替原始客户端地址。
– ranges:使用以地址段的形式定义地址,这个参数必须放在首位。为了加速装载地址库,地址应按升序定义。

geo $country {
    default        ZZ;
    include        conf/geo.conf;
    delete         127.0.0.0/16;
    proxy          192.168.100.0/24;
    proxy          2001:0db8::/32;

    127.0.0.0/24   US;
    127.0.0.1/32   RU;
    10.1.0.0/16    RU;
    192.168.1.0/24 UK;
}
vim conf/geo.conf
10.2.0.0/16    RU;
192.168.2.0/24 RU;

地址段例子:

geo $country {
    ranges;
    default                   ZZ;
    127.0.0.0-127.0.0.0       US;
    127.0.0.1-127.0.0.1       RU;
    127.0.0.1-127.0.0.255     US;
    10.1.0.0-10.1.255.255     RU;
    192.168.1.0-192.168.1.255 UK;
}

[warning]遵循最精确匹配原则,即nginx使用能最精确匹配客户端地址的值。[/warning]

适用实例

上面的例子几乎都是官网说明例子。下面举例说明便于理解该指令的用法。

1. 使用默认变量也就是$remote_addr

http {
  #geo $remote_addr $ttlsa_com {
  geo $ttlsa_com {
        default 0;
        127.0.0.1 1;
  }
  server {
        listen       8080;
        server_name  test.ttlsa.com;

        location /hello {
      default_type text/plain;
      echo $ttlsa_com;
      echo $arg_boy;
    }
  }
}
# curl 127.0.0.1:8080/hello?boy=默北
1
默北

2. 使用指定变量

http {
  geo $arg_boy $ttlsa_com {
        default 0;
        127.0.0.1 1;
        8.8.8.8 2;
}
  server {
        listen       8080;
        server_name  test.ttlsa.com;

        location /hello {
      default_type text/plain;
      echo $ttlsa_com;
      echo $arg_boy;
    }
  }
}
# curl 127.0.0.1:8080/hello?boy=8.8.8.8
2
8.8.8.8

3. 匹配原则

http {
  geo $arg_boy $ttlsa_com {
        default 0;
        127.0.0.1/24 24;
        127.0.0.1/32 32;
        8.8.8.8 2;
}
  server {
        listen       8080;
        server_name  test.ttlsa.com;

        location /hello {
      default_type text/plain;
      echo $ttlsa_com;
      echo $arg_boy;
    }
  }
}
# curl 127.0.0.1:8080/hello?boy=127.0.0.1
32
127.0.0.1
# curl 127.0.0.1:8080/hello?boy=127.0.0.12
24
127.0.0.12

geo指令主要是根据IP来对变量进行赋值的。因此geo块下只能定义IP或网络段,否则会报错“nginx: [emerg] invalid network”。

Ubuntu 16.04源码编译安装Nginx 1.10.3

在Ubuntu 16.04源码编译安装Nginx 1.10.3 过程记录。

一、下载相关的依赖库

  • pcre 下载地址 http://120.52.73.43/jaist.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.tar.gz
  • openssl 下载地址 https://www.openssl.org/source/openssl-1.0.2h.tar.gz
  • zlib 下载地址 http://zlib.net/zlib-1.2.8.tar.gz
  • nginx 下载地址 http://nginx.org/download/nginx-1.10.3.tar.gz

二、解压相关依赖库源码包

cd /tmp
tar -zxf nginx-1.10.3.tar.gz 
tar -zxf pcre-8.38.tar.gz
tar -zxf zlib-1.2.8.tar.gz
tar -zxf openssl-1.0.2h.tar.gz

三、编译安装Nginx

cd /tmp/nginx-1.10.3
./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/logs/nginx.pid --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --with-http_ssl_module --with-pcre=/tmp/pcre-8.38 --with-zlib=/tmp/zlib-1.2.8 --with-openssl=/tmp/openssl-1.0.2h

四、执行make和make install 安装

make && sudo make install

五、验证nginx是否安装成功

/usr/local/nginx/sbin/nginx -v

执行结果:nginx version: nginx/1.10.3

六、启动nginx

/usr/local/nginx/sbin/nginx

浏览器访问localhost:
Nginx

ubuntu 16.04安装docker-ce v17.03

本文介绍在ubuntu 16.04 LTS安装最新版本的docker-ce,docker v17.03。目前docker分为docker-ce(社区版本),docker-ee(企业版本)。这里介绍docker-ce的安装。

先决条件

OS要求

要安装docker,需要如下64位ubuntu版本的系统
– Yakkety 16.10
– Xenial 16.04 (LTS)
– Trusty 14.04 (LTS)

卸载旧版本

旧的docker版本称为docker或docker-engine,如果安装了这些版本,先卸载它们:

$ sudo apt-get remove docker docker-engine

/var/lib/docker目录存储着镜像,容器,数据卷和网络,这些都会保留。

安装docker

配置仓库

1.配置apt可以使用https安装软件包

$ sudo apt-get install 
    apt-transport-https 
    ca-certificates 
    curl 
    software-properties-common

2.添加docker官方GPG key:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

验证key批纹为9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88.

$ sudo apt-key fingerprint 0EBFCD88

pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <[email protected]>
sub   4096R/F273FCD8 2017-02-22

3.使用如下命令配置stable仓库。

$ sudo add-apt-repository 
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu 
   $(lsb_release -cs) 
   stable"

安装docker

1.更新apt软件包索引

$ sudo apt-get update

2.安装最新版本的docker,或者到下一步安装指定版本的docker

sudo apt-get install docker-ce

3.在生产系统中,应该安装一个指定的docker版本,而不是最新的。执行如下命令列出可用的版本。

$ apt-cache madison docker-ce

docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages

执行如下命令安装指定的版本:

sudo apt-get install docker-ce=<VERSION>

centos 7 yum安装mysql 5.7

1 查看Linux发行版本

[root@typecodes ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

2 下载MySQL官方的Yum Repository

根据Linux发行版本(CentOS、Fedora都属于红帽系),从mysql官方(http://dev.mysql.com/downloads/repo/yum/)获取Yum Repository。

[root@typecodes ~]#  wget -i http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
--2016-02-03 18:36:02--  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm [following]
--2016-02-03 18:36:04--  http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 104.102.164.25
Connecting to repo.mysql.com (repo.mysql.com)|104.102.164.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8984 (8.8K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-7.noarch.rpm’

100%[=============================================================================================================================>] 8,984       --.-K/s   in 0s

2016-02-03 18:36:07 (68.4 MB/s) - ‘mysql57-community-release-el7-7.noarch.rpm’ saved [8984/8984]

3 安装MySQL的Yum Repository

安装完MySQL的Yum Repository,每次执行yum update都会检查MySQL是否更新。

[root@typecodes ~]# yum -y install mysql57-community-release-el7-7.noarch.rpm
Loaded plugins: axelget, fastestmirror, langpacks
Examining mysql57-community-release-el7-7.noarch.rpm: mysql57-community-release-el7-7.noarch
Marking mysql57-community-release-el7-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                                       Arch                       Version                    Repository                                                   Size
=======================================================================================================================================================================
Installing:
 mysql57-community-release                     noarch                     el7-7                      /mysql57-community-release-el7-7.noarch                     7.8 k

Transaction Summary
=======================================================================================================================================================================
Install  1 Package

Total size: 7.8 k
Installed size: 7.8 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-7.noarch                                                                                                              1/1 
  Verifying  : mysql57-community-release-el7-7.noarch                                                                                                              1/1

Installed:
  mysql57-community-release.noarch 0:el7-7

Complete!

4 安装MySQL数据库的服务器版本

[root@typecodes ~]# yum -y install mysql-community-server
Loaded plugins: axelget, fastestmirror, langpacks
No metadata available for base
No metadata available for epel
No metadata available for extras
repomd.xml                                                                                                                                      | 2.5 kB  00:00:00     
update mysql-connectors-community metadata successfully
repomd.xml                                                                                                                                      | 2.5 kB  00:00:00     
update mysql-tools-community metadata successfully
repomd.xml                                                                                                                                      | 2.5 kB  00:00:00     
update mysql57-community metadata successfully
No metadata available for updates
mysql-connectors-community                                                                                                                      | 2.5 kB  00:00:00     
mysql-tools-community                                                                                                                           | 2.5 kB  00:00:00     
mysql57-community                                                                                                                               | 2.5 kB  00:00:00     
(1/3): mysql-tools-community/x86_64/primary_db                                                                                                  |  24 kB  00:00:01     
(2/3): mysql57-community/x86_64/primary_db                                                                                                      |  28 kB  00:00:01     
(3/3): mysql-connectors-community/x86_64/primary_db                                                                                             | 8.6 kB  00:00:02     
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.neusoft.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.10-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.10-1.el7 for package: mysql-community-server-5.7.10-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) = 5.7.10-1.el7 for package: mysql-community-server-5.7.10-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.10-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) = 5.7.10-1.el7 for package: mysql-community-client-5.7.10-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.10-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.44-2.el7.centos will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.10-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.10-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                                            Arch                          Version                               Repository                                Size
=======================================================================================================================================================================
Installing:
 mysql-community-libs                               x86_64                        5.7.10-1.el7                          mysql57-community                        2.2 M
     replacing  mariadb-libs.x86_64 1:5.5.44-2.el7.centos
 mysql-community-libs-compat                        x86_64                        5.7.10-1.el7                          mysql57-community                        2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.44-2.el7.centos
 mysql-community-server                             x86_64                        5.7.10-1.el7                          mysql57-community                        142 M
Installing for dependencies:
 mysql-community-client                             x86_64                        5.7.10-1.el7                          mysql57-community                         24 M
 mysql-community-common                             x86_64                        5.7.10-1.el7                          mysql57-community                        269 k

Transaction Summary
=======================================================================================================================================================================
Install  3 Packages (+2 Dependent packages)

Total download size: 171 M
Downloading packages:
mysql-community-server-5.7.10-1.el7.x86_64.rpm                                                                                                  | 142 MB  00:03:42     
mysql-community-libs-compat-5.7.10-1.el7.x86_64.rpm                                                                                             | 2.0 MB  00:00:03     
mysql-community-libs-5.7.10-1.el7.x86_64.rpm                                                                                                    | 2.2 MB  00:00:05     
mysql-community-client-5.7.10-1.el7.x86_64.rpm                                                                                                  |  24 MB  00:00:41     
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.10-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY00 ETA 
Public key for mysql-community-common-5.7.10-1.el7.x86_64.rpm is not installed
mysql-community-common-5.7.10-1.el7.x86_64.rpm                                                                                                  | 269 kB  00:00:01     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <[email protected]>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql57-community-release-el7-7.noarch (@/mysql57-community-release-el7-7.noarch)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.10-1.el7.x86_64                                                                                                          1/6 
  Installing : mysql-community-libs-5.7.10-1.el7.x86_64                                                                                                            2/6 
  Installing : mysql-community-client-5.7.10-1.el7.x86_64                                                                                                          3/6 
  Installing : mysql-community-server-5.7.10-1.el7.x86_64                                                                                                          4/6 
  Installing : mysql-community-libs-compat-5.7.10-1.el7.x86_64                                                                                                     5/6 
  Erasing    : 1:mariadb-libs-5.5.44-2.el7.centos.x86_64                                                                                                           6/6 
  Verifying  : mysql-community-server-5.7.10-1.el7.x86_64                                                                                                          1/6 
  Verifying  : mysql-community-libs-compat-5.7.10-1.el7.x86_64                                                                                                     2/6 
  Verifying  : mysql-community-common-5.7.10-1.el7.x86_64                                                                                                          3/6 
  Verifying  : mysql-community-libs-5.7.10-1.el7.x86_64                                                                                                            4/6 
  Verifying  : mysql-community-client-5.7.10-1.el7.x86_64                                                                                                          5/6 
  Verifying  : 1:mariadb-libs-5.5.44-2.el7.centos.x86_64                                                                                                           6/6

Installed:
  mysql-community-libs.x86_64 0:5.7.10-1.el7          mysql-community-libs-compat.x86_64 0:5.7.10-1.el7          mysql-community-server.x86_64 0:5.7.10-1.el7

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.10-1.el7                                       mysql-community-common.x86_64 0:5.7.10-1.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.44-2.el7.centos

Complete!
[root@typecodes ~]# clear

最后的截图:

MySQL

5 启动数据库:

[root@typecodes ~]# systemctl start  mysqld.service

然后使用命令systemctl status mysql.service查看MySQL数据库启动后的服务状态:

MySQL

6 获取初始密码

使用YUM安装并启动MySQL服务后,MySQL进程会自动在进程日志中打印root用户的初始密码:

#######从mysql进程日志中获取root用户的初始密码:ra%yk7urCBIh
[root@typecodes ~]# grep "password" /var/log/mysqld.log
2016-02-03T10:42:17.272166Z 1 [Note] A temporary password is generated for root@localhost: ra%yk7urCBIh
2016-02-03T10:42:36.776875Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-02-03T10:42:52.063138Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-02-03T10:42:57.564373Z 4 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-02-03T10:43:01.477007Z 5 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-02-03T10:46:03.642008Z 6 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-02-03T10:46:11.217889Z 7 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-02-03T10:47:44.755199Z 0 [Note] Shutting down plugin 'validate_password'
2016-02-03T10:47:46.505844Z 0 [Note] Shutting down plugin 'sha256_password'
2016-02-03T10:47:46.505851Z 0 [Note] Shutting down plugin 'mysql_native_password'

7 修改root用户密码

使用小节5中获取的root用户的初始密码,然后进行修改:

[root@typecodes ~]# mysql -uroot -p
Enter password:             #######输入默认的root密码后回车
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.10

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

8 安装完毕

至此,使用在CentOS7中使用YUM方法安装MySQL5.7.10数据库完毕。如下所示,可以使用新的root密码登陆MySQL了。

[root@typecodes ~]# mysql -uroot -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.10 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> clear
mysql> exit
Bye
[root@typecodes ~]# clear

附录1:查看MySQL数据库的配置信息

MySQL的配置文件依然是/etc/my.cnf,其它安装信息可以通过mysql_config命令查看。其中,动态库文件存放在/usr/lib64/mysql目录下。

MySQL

附录2:对于C/C++等开发者

由于需要用到类似mysql.h等头文件,需要执行下面的命令安装mysql开发版本即可。

[root@typecodes ~]# yum -y install mysql-community-devel

附录3:删除MySQL的Repository

因为小节3中安装了MySQL的Yum Repository,所以以后每次执行yum操作时,都会去检查更新。如果想要去掉这种自动检查操作的话,可以使用如下命令卸载MySQL的Repository即可。

[root@typecodes ~]# yum -y remove mysql57-community-release-el7-7.noarch

使用Python URLLIB3下载文件

urllib3是一个轻量级的python库,提供了线程安全,HTTP连接池和重用,文件发送等。

Python
为了演示urllib3的使用,我们这里将会从一个网站下载两个文件。
首先,需要导入urllib3库:

import urllib3

这两个文件的源url为:

url1 = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.csv'
url2 = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv'

现在开始创建一个HTTP连接池:

http = urllib3.PoolManager()

然后请求第一个文件并写入到文件:

response = http.request('GET', url1)
with open('all_week.csv', 'wb') as f:
    f.write(response.data)

然后是第二个文件:

response = http.request('GET', url2)
with open('all_month.csv', 'wb') as f:
    f.write(response.data)

最后释放这个HTTP连接:

response.release_conn()

ubuntu 16.04安装配置zabbix3

zabbix是一个开源的网络和应用程序监控软件。提供了zabbix agent来监控远程主机,也支持通过SNMP,TCP和ICMP检查来监控主机。
监控

步骤1:安装Apache,MySQL和PHP

在安装zabbix前,需要安装一个web server,数据库服务器和PHP。在这一步骤中我们将安装这些服务,如果你已经安装有了,可以跳过此步。

$ sudo apt-get update
$ sudo apt-get install apache2 
$ sudo apt-get install mysql-server 
$ sudo apt-get install php5 php5-cli php5-common php5-mysql

在/etc/php5/apache2/php.ini更新时区,如下:

[Date]
; http://php.net/date.timezone
date.timezone = 'Asia/Shanghai'

步骤2:添加Apt仓库

$ wget http://repo.zabbix.com/zabbix/3.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.0-1+xenial_all.deb
$ sudo dpkg -i zabbix-release_3.0-1+xenial_all.deb
$ sudo apt-get update

步骤3: 安装zabbix server

安装zabbix apt仓库后,我们使用如下命令来安装zabbix使用mysql数据库。

$ sudo apt-get install zabbix-server-mysql zabbix-frontend-php

步骤4: 创建数据表

现在为zabbix server创建相关数据表。首先使用如下命令创建mysql数据库和用户

$ mysql -u root -p

mysql> CREATE DATABASE zabbixdb;
mysql> GRANT ALL on zabbixdb.* to zabbix@localhost IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;

现在导入数据表

$ cd /usr/share/doc/zabbix-server-mysql
$ zcat create.sql.gz | mysql -u root -p zabbixdb

步骤5: 编辑zabbix配置文件

打开/etc/zabbix/zabbix_server.conf,根据如下编辑配置文件:

  DBHost=localhost
  DBName=zabbixdb
  DBUser=zabbix
  DBPassword=password

步骤6: 重启apache和zabbix

zabbix在/etc/zabbix/apache.conf创建了自己的配置文件,使用如下命令重启apache

$ sudo service apache2 restart

zabbix server的配置文件在/etc/zabbix/zabbix_server.conf,使用如下命令重启zabbix server

$ sudo service zabbix-server restart

启动zabbix server之后,我们打开zabbix web安装器来完成安装。

步骤7: 启动zabbix web安装器

zabbix web安装器可以使用如下url打开:

http://svr1.example.net/zabbix/

zabbix配置的欢迎界面

这个是zabbix web安装器的欢迎界面。点击next按钮继续

监控

检查先决条件

检查你的系统是否已经安装有所需的安装包,所以一切正常,点击next继续
监控

配置db连接

键入在步骤4创建的数据库信息,点击Test Connection。如果数据库连接正确,将会显示ok信息,之后点击next继续
监控

zabbix server详情

监控

安装前概览

在这个步骤中将会显示之前设置的概览,所以点击next继续。
监控

安装zabbix

监控

zabbix登录界面

使用如下默认凭证登录zabbix

   Username:  admin
   Password:  zabbix

监控
登录成功后将会看到如下控制面板
监控
到此,zabbix server已经安装完成。

centos7源码编译安装apache2.4

本该将介绍在centos 7系统上源码编译apache 2.4。这个也适用于centos 6。你可能会疑问,为什么不直接rpm安装,这可能有几个原因:
– 从仓库中安装的apache默认会安装大量地模板,这些模板可能会占用比较多的资源,而生产环境可能只需要这些模块的30%。
– 假设你只有一个512MB内存的VPS,而且选择了CentOS发行版本,从仓库安装apache,如果不优化的话,将有可能内存不足

现在开始安装。

1) 安装一些依赖包

yum install wget gcc pcre-devel openssl-devel

2) 下载apache,apr和apr-util

apache 2.4版本使用apache 2.4.12,apr-1.5.2和 apr-util-1.5.4

cd ~
mkdir sources
cd sources
wget http://ftp.piotrkosoft.net/pub/mirrors/ftp.apache.org//httpd/httpd-2.4.12.tar.bz2
wget http://ftp.ps.pl/pub/apache//apr/apr-1.5.2.tar.bz2
wget http://ftp.ps.pl/pub/apache//apr/apr-util-1.5.4.tar.bz2
tar -xvf httpd-2.4.12.tar.bz2
tar -xvf apr-1.5.2.tar.bz2
tar -xvf apr-util-1.5.4.tar.bz2
cp -r apr-1.5.2 httpd-2.4.12/srclib/apr
cp -r apr-util-1.5.4 httpd-2.4.12/srclib/apr-util
cd httpd-2.4.12
./configure --prefix=/etc/apache2 --enable-ssl --enable-so --with-included-apr --with-mpm=event
make
make install

需要注意的是由于./configure中的–prefix=/etc/apache2,apache将安装在/etc/apache2,同样的会启用ssl支持,so(动态模块支持),包括apr和event mpm。

3) 启动前一些基本的配置

需要确保/etc/apache2的所有者为apache用户:

chown -R apache.root /etc/apache2

需要对httpd.conf作一些更改

cd /etc/apache2/conf
cp httpd.conf httpd.conf.bak
vi httpd.conf

在文件内,设置用户和用户组为apache

User apache
Group apache

设置一个ServerName,这可以避免apache启动时的warning

ServerName example.com

取消Server-pool管理的注释

Include conf/extra/httpd-mpm.conf

在上面的行添加如下行:

Include conf/vhosts/*.conf

也取消server默认配置的注释

Include conf/extra/httpd-default.conf

4) 启动apache

检查启用的模板

/etc/apache2/bin/apachectl -M

检查配置文件是否有错误

/etc/apache2/bin/apachectl -t

重启apache server

/etc/apache2/bin/apachectl -k graceful

确保apache开机启动

echo '/etc/apache2/bin/apachectl start' >> /etc/rc.local

在centos 7开机80 443端口

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --reload

如果是centos 6,使用iptables开启端口

iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
service iptables save

nginx autoindex开启目录浏览和索引

Nginx默认是不允许列出整个目录的。如需此功能,打开nginx.conf文件或你要启用目录浏览虚拟主机的配置文件,在server或location 段里添加上autoindex on;来启用目录流量,下面会分情况进行说明。

另外Nginx的目录流量有两个比较有用的参数,可以根据自己的需求添加:

autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
改为on后,显示的文件时间为文件的服务器时间

1、整个虚拟主机开启目录流量

在server段添加

location / {
    autoindex on;
    autoindex_localtime on; #之类的参数写这里
}

2、单独目录开启目录流量

2.1:直接二级目录开启目录流量

location /down/ {
    autoindex on;
}

2.2:虚拟目录开启目录流量

location /down/ {
    alias /home/wwwroot/lnmp/test/;
    autoindex on;
}

详细参照:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

需要注意root和alias的区别:

alias 设置的目录是准确的,可以理解为linux的 ln命令创建软连接,location就是软连接的名字。如上面2.2例子访问 http://域名/down/vpser.txt 是直接访问的/home/wwwroot/lnmp/test/下面的vpser.txt文件。

root 设置的目录是根目录,locatoin里所指定名称的目录,必须在root设定下的目录有相同名字的目录。如果将上面2.2例子里的alias改成root 访问 http://域名/down/vpser.txt 是直接访问的的/home/wwwroot/lnmp/test/down/ 目录下的vpser.txt文件。

需要注意的是alias目录必须要以 / 结尾且alias只能在location中使用。

如果想希望做出漂亮的目录列表,支持header,footer则可以安装三方插件:
http://wiki.nginx.org/NginxNgxFancyIndex

重启nginx,使其生效。

nginx auth_basic为目录添加密码保护

nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。

格式为:htpasswd -b -c site_pass username password

site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。

如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd.如centos是yum install httpd

如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。

如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。

例如:基于整个网站的认证,auth_basic在php解释之前。

server {
    listen       80;
    server_name www.wkii.org akii.org;
    root  /www/akii;
    index index.html index.htm index.php;

    auth_basic "input you user name and  password";
    auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd;

    location ~ .php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    location ~ /.ht {
         deny  all;
    }
    access_log /logs/akii.org_access.log main;
}

针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。

server {
    listen       80;
    server_name www.wkii.org akii.org;
    root  /www/akii;
    index index.html index.htm index.php;

    location ~ ^/admin/.* {
        location ~ .php$ {
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        auth_basic "auth";
        auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass;
    }

    location ~ .php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /.ht {
         deny  all;
    }
    access_log /logs/akii.org_access.log main;
}

这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。