Windows下配置Java环境变量

下载java se development kit - JDK

java官网: www.oracle.com

环境变量配置:

1
2
3
JAVA_HOME:D:/Program Files/Java/jdk1.6.0_10
PATH:%JAVA_HOME%/bin;%JAVA_HOME%/jre/bin
CLASSPATH:.;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar

说明:

1)JAVA_HOME指明JDK安装路径

2)PATH使得系统可以在任何路径下识别java命令

3)为java加载类(class or lib)路径,只有类在classpath中,java命令才能识别。

linux下环境变量:

1
2
3
export JAVA_HOME=/usr/java/jdk1.8.0_77
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

1.安装软件包

1
2
3
yum install httpd
yum install mod_dav_svn
yum install subversion

2、创建仓库

1
2
 mkdir /var/svn/svnrepos
svnadmin create /var/svnrepos/jktest

3、配置

1
2
3
4
5
6
7
8
9
10
11
12
13
/var/svn/svnrepos/jktest
conf目录下找到svnserve.conf
password-db = passwd,将前面的#删除,
authz-db = authz 将前面的#删除
realm = /var/svn/svnrepos/jktest 将前面的#删除改自己的库地址
:wq
vim /var/svn/svnrepos/jktest/conf/authz 添加用户名密码
[/]
test = rw
:wq
vim /var/svn/svnrepos/jktest/conf/passwd
[user]
test = 123456

4、配置svn关联到apache:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vi /etc/httpd/conf.d/subversion.conf //添加下面内容
LoadModule dav_svn_module modules/mod_dav_svn.so
02.LoadModule authz_svn_module modules/mod_authz_svn.so

<Location /svnrepos>
DAV svn
SVNParentPath /var/svn/svnrepos

# Limit write permission to list of valid users.
# <LimitExcept GET PROPFIND OPTIONS REPORT>
# Require SSL connection for password protection.
# SSLRequireSSL

AuthType Basic
AuthName "Authorization svn"
AuthUserFile /var/svn/svnrepos/jktest/pwdfile
AuthzSVNAccessFile /var/svn/svnrepos/jktest/conf/authz
"/etc/httpd/conf.d/subversion.conf" 41L, 1109C

添加

1
2
vim /etc/httpd/conf/httpd.conf 
include conf.d/*.conf

添加http访问用户
htpasswd -c /var/svn/svnrepos/jktest/pwdfile test
这里用到参数-c,是因为pwdfile文件不存在,如果文件存在,则无需该参数!否则,将覆盖掉原有密码文件
启动svn服务
svnserve -d -r /var/www/svn/project
启动httpd

安装nginx

选择稳定版本

我们编译安装nginx来定制自己的模块。
•机器CentOS 6.6 x86_64。
•软件版本:Nginx 1.9.9

yum 一键安装nginx

yum安装rpm包会比编译安装简单很多,默认会安装许多模块,帮我们解决到很多依赖的安装包,但缺点是如果你想以后安装第三方模块那就没办法了。

使用Nginx官方源,Epel扩展库和remi源,remi源基于epel,必须先安装epel源,remi包含php-fpm,mysql-server5.5,如果只需要php-fpm可以单独安装php-fpm后禁用此源.

第一种方法:

1
2
3
4
5
6
# vi /etc/yum.repo.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

剩下的就yum install nginx搞定,也可以yum install nginx-1.9.9安装指定版本(前提是你去packages里看到有对应的版本,默认是最新版稳定版)

第二种方法:

各节点时间同步

1
2
3
4
5
6
7
[root@nginx ~]# ntpdate 202.120.2.101
安装EPEL源:
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum clean all
yum makecache
yum install -y nginx
service nginx start

nginx编译安装:

首先安装缺少的依赖包:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
```
这些软件包如果yum上没有的话可以下载源码来编译安装,只是要注意编译时默认安装的目录,确保下面在安装nginx时能够找到这些动态库文件(ldconfig)。

下载nginx地址:[nginx](http://nginx.org/en/download.html)

新建nginx用户与组
``` bash
groupadd www
useradd -s /sbin/nologin -g www www

编译配置文件
tar -zxvf nginx-1.9.9.tar.gz
cd nginx-1.9.9/
./configure --user=www --group=www
--prefix=/usr/local/nginx
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--with-http_stub_status_module
--with-http_ssl_module
--with-http_gzip_static_module
--with-http_realip_module

make && make install


常用编译选项说明

nginx大部分常用模块,编译时./configure –help以–without开头的都默认安装。


--prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
--conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
--user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。--group=name类似
--with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用--with-pcre自动找到库文件。使用--with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 - 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
--with-zlib=PATH : 指定 zlib(版本1.1.3 - 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
--with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
--with-http_stub_status_module : 用来监控 Nginx 的当前状态
--with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
--add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)



再提供一种编译方案:


./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--with-pcre=../pcre-7.8
--with-zlib=../zlib-1.2.3



启动nginx服务
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx ##做软连接
cp nginx.init /etc/init.d/nginx 启动脚本
chmod +x /etc/init.d/nginx


nginx启动脚本
nginx
```php
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/var/run/${prog}.pid"

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f $sysconfig ] && . $sysconfig


start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest_q || return 6
stop
start
}

reload() {
configtest_q || return 6
echo -n $"Reloading $prog: "
killproc -p $pidfile $prog -HUP
echo
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

configtest_q() {
$nginx -t -q -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

# Upgrade the binary with no downtime.
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin"

configtest_q || return 6
echo -n $"Upgrading $prog: "
killproc -p $pidfile $prog -USR2
retval=$?
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
killproc -p $oldbin_pidfile $prog -QUIT
success $"$prog online upgrade"
echo
return 0
else
failure $"$prog online upgrade"
echo
return 1
fi
}

# Tell nginx to reopen logs
reopen_logs() {
configtest_q || return 6
echo -n $"Reopening $prog logs: "
killproc -p $pidfile $prog -USR1
retval=$?
echo
return $retval
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest|reopen_logs)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac

启动关闭nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 检查配置文件是否正确
# /usr/local/nginx-1.6/sbin/nginx -t
# ./sbin/nginx -V # 可以看到编译选项

## 启动、关闭
# ./sbin/nginx # 默认配置文件 conf/nginx.conf,-c 指定
# ./sbin/nginx -s stop
或 pkill nginx

## 重启,不会改变启动时指定的配置文件
# ./sbin/nginx -s reload
或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid`


设置开机启动
chkconfig --level 345 nginx on
chkconfig nginx on
chkconfig nginx --list
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

当然也可以将 nginx 作为系统服务管理,下载 nginx 到/etc/init.d/,修改里面的路径然后赋予可执行权限。
service nginx {start|stop|status|restart|reload|configtest}

nginx.conf配置文件

Nginx配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置),每部分包含若干个指令。main部分设置的指令将影响其它所有部分的设置;server部分的指令主要用于指定虚拟主机域名、IP和端口;upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。

当前nginx支持的几个指令上下文:

配置文件
下面的nginx.conf简单的实现nginx在前端做反向代理服务器的例子,处理js、png等静态文件,jsp等动态请求转发到其它服务器tomcat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

user www www;
worker_processes 2;

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

pid logs/nginx.pid;


events {
use epoll;
worker_connections 2048;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
# tcp_nopush on;

keepalive_timeout 65;

# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;

# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;

# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}

# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;

charset utf-8;
access_log logs/host.access.log main;

#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;

proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

}

#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;

}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}

location ~ ^/(WEB-INF)/ {
deny all;
}
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

## 其它虚拟主机,server 指令开始
}



这里我整理了一份每个含义的详解:

#定义Nginx运行的用户和用户
user www www;

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;

#进程文件
pid /var/run/nginx.pid;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

#工作模式与连接数上限
events
{
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 65535;
}

#设定http服务器
http
{
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型
#charset utf-8; #默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 32k; #上传文件大小限制
large_client_header_buffers 4 64k; #设定请求缓
client_max_body_size 8m; #设定请求缓
sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout 120; #长连接超时时间,单位是秒

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml;
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

upstream blog.kern.com {
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}

#虚拟主机的配置
server
{
#监听端口
listen 80;
#域名可以有多个,用空格隔开
server_name www.kern.com ; ##(服务器名)
index index.html index.htm index.php;
root /data/www/ha97;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#图片缓存时间设置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
}
#日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定义本虚拟主机的访问日志
access_log /var/log/nginx/ha97access.log access;

#对 "/" 启用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选。
proxy_set_header Host $host;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传
}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}

#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}

Nginx + Tomcat做负载均衡

Nginx负载均衡,其实主要就是用upstream、server指令,再配以权重等等参数。如果为了让nginx支持session共享,还需要额外增加一个模块。

一、Nginx负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
在http{…}中配置一个upstream{…},参考如下:
引用
upstream tomcat-account {
server 10.11.155.26:8080 weight=1;
server 10.11.155.41:8080 weight=1;
}


接着修改location节点,配置代理:

引用


server {
listen 80;
server_name 10001.test.intranet;
location / {
index index.html index.php index.jsp index.htm;
proxy_pass http://tomcat-account;
proxy_ignore_client_abort on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
```
这里可以写自己的内网域名也可以写IP
当访问根路径时,会轮播路由到两台服务器上,至于后端服务器是tomcat还是jetty之类的,都无所谓,照葫芦画瓢就是了。

当然,有的机器性能好,或者负载低,可以承担高负荷访问量,可以通过权重(weight),提升访问频率。数值越高,被分配到的请求数越多。


server指令参数如下:

``` bash
- weight ——权重,数值越大,分得的请求数就越多,默认值为1
- max_fails ——对访问失败的后端服务器尝试访问的次数。默认值为1,当设置为0时将关闭检查。
- fail_timeout——失效超时时间,当多次访问失败后,对该节点暂停访问。
- down——标记服务器为永久离线状态,用于ip_hash指令。
- backup——仅当非backup服务器全部宕机或繁忙时启用。



例如,可以这样配置:

引用
upstream tomcat {
server 10.11.155.26:8080 weight=5;
server 10.11.155.41:8080 weight=10;
}

后者分得的请求数就会较高。

详细点的负载均衡配置nginx.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

user www;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /opt/logs/nginx/error.log crit;

# pid /usr/local/nginx/nginx.pid;
pid /var/run/nginx.pid;

worker_rlimit_nofile 1024;

events {
use epoll;
worker_connections 65535;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {

#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;

#access_log logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';


#设定日志格式
access_log /var/log/nginx/access.log;

#省略上文有的一些配置节点
#。。。。。。。。。。

#设定负载均衡的服务器列表
upstream tomcat-account {
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.8.1x:3128 weight=5;
#本机上的Squid开启3128端口,不是必须要squid
server 192.168.8.2x:80 weight=1;
server 192.168.8.3x:80 weight=6;
}

upstream mysvr2 {
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.8.x:80 weight=1;
server 192.168.8.x:80 weight=6;
}

#第一个虚拟服务器
server {
#侦听192.168.8.x的80端口
listen 80;
server_name 192.168.8.x;

#对aspx后缀的进行负载均衡请求
location ~ .*.aspx$ {
#定义服务器的默认网站根目录位置
root /root;
#定义首页索引文件的名称
index index.php index.html index.htm;

#请求转向mysvr 定义的服务器列表
proxy_pass http://tomcat-account;

#以下是一些反向代理的配置可删除.所以可以按nginx+tomcat 做负载均衡即可。

proxy_redirect off;

#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#允许客户端请求的最大单文件字节数
client_max_body_size 10m;

#缓冲区代理缓冲用户端请求的最大字节数,
client_body_buffer_size 128k;

#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 90;

#连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 90;

#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 4k;

#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 4 32k;

#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;

#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 64k;

}
}
}

常用指令说明

nginx在运行时与具体业务功能(比如http服务或者email服务代理)无关的一些参数,比如工作进程数,运行的身份等。

woker_processes 2
在配置文件的顶级main部分,worker角色的工作进程的个数,master进程是接收并分配请求给worker处理。这个数值简单一点可以设置为cpu的核数grep ^processor /proc/cpuinfo | wc -l,也是 auto 值,如果开启了ssl和gzip更应该设置成与逻辑CPU数量一样甚至为2倍,可以减少I/O操作。如果nginx服务器还有其它服务,可以考虑适当减少。

worker_cpu_affinity
也是写在main部分。在高并发情况下,通过设置cpu粘性来降低由于多CPU核切换造成的寄存器等现场重建带来的性能损耗。如worker_cpu_affinity 0001 0010 0100 1000; (四核)。

worker_connections 2048
写在events部分。每一个worker进程能并发处理(发起)的最大连接数(包含与客户端或后端被代理服务器间等所有连接数)。nginx作为反向代理服务器,计算公式 最大连接数 = worker_processes * worker_connections/4,所以这里客户端最大连接数是1024,这个可以增到到8192都没关系,看情况而定,但不能超过后面的worker_rlimit_nofile。当nginx作为http服务器时,计算公式里面是除以2。

worker_rlimit_nofile 10240
写在main部分。默认是没有设置,可以限制为操作系统最大的限制65535。

use epoll
写在events部分。在Linux操作系统下,nginx默认使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue。在操作系统不支持这些高效模型时才使用select。

2.2.2 http服务器

与提供http服务相关的一些配置参数。例如:是否使用keepalive啊,是否使用gzip进行压缩等。

sendfile on
开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。

keepalive_timeout 65 : 长连接超时时间,单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操作系统的设置,可以另外起一片文章了。长连接请求大量小文件的时候,可以减少重建连接的开销,但假如有大文件上传,65s内没上传完成会导致失败。如果设置时间过长,用户又多,长时间保持连接会占用大量资源。

send_timeout : 用于指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

client_max_body_size 10m
允许客户端请求的最大单文件字节数。如果有上传较大文件,请设置它的限制值

client_body_buffer_size 128k
缓冲区代理缓冲用户端请求的最大字节数
模块http_proxy:
这个模块实现的是nginx作为反向代理服务器的功能,包括缓存功能(另见文章)

proxy_connect_timeout 60
nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 60
连接成功后,与后端服务器两个成功的响应操作之间超时时间(代理接收超时)

proxy_buffer_size 4k
设置代理服务器(nginx)从后端realserver读取并保存用户头信息的缓冲区大小,默认与proxy_buffers大小相同,其实可以将这个指令值设的小一点

proxy_buffers 4 32k
proxy_buffers缓冲区,nginx针对单个连接缓存来自后端realserver的响应,网页平均在32k以下的话,这样设置

proxy_busy_buffers_size 64k
高负荷下缓冲大小(proxy_buffers*2)

proxy_max_temp_file_size
当 proxy_buffers 放不下后端服务器的响应内容时,会将一部分保存到硬盘的临时文件中,这个值用来设置最大临时文件大小,默认1024M,它与 proxy_cache 没有关系。大于这个值,将从upstream服务器传回。设置为0禁用。

proxy_temp_file_write_size 64k
当缓存被代理的服务器响应到临时文件时,这个选项限制每次写临时文件的大小。proxy_temp_path(可以在编译的时候)指定写到哪那个目录。

proxy_pass,proxy_redirect见 location 部分。

模块http_gzip:

gzip on : 开启gzip压缩输出,减少网络传输。
gzip_min_length 1k : 设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是20。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_buffers 4 16k : 设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。
gzip_http_version 1.0 : 用于识别 http 协议的版本,早期的浏览器不支持 Gzip 压缩,用户就会看到乱码,所以为了支持前期版本加上了这个选项,如果你用了 Nginx 的反向代理并期望也启用 Gzip 压缩的话,由于末端通信是 http/1.0,故请设置为 1.0。
gzip_comp_level 6 : gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_types :匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
gzip_proxied any : Nginx作为反向代理的时候启用,决定开启或者关闭后端服务器返回的结果是否压缩,匹配的前提是后端服务器必须要返回包含”Via”的 header头。
gzip_vary on : 和http头有关系,会在响应头加个 Vary: Accept-Encoding ,可以让前端的缓存服务器缓存经过gzip压缩的页面,例如,用Squid缓存经过Nginx压缩的数据。。
2.2.3 server虚拟主机

http服务上支持若干虚拟主机。每个虚拟主机一个对应的server配置项,配置项里面包含该虚拟主机相关的配置。在提供mail服务的代理时,也可以建立若干server。每个server通过监听地址或端口来区分。

listen
监听端口,默认80,小于1024的要以root启动。可以为listen *:80、listen 127.0.0.1:80等形式。

server_name
服务器名,如localhost、www.example.com,可以通过正则匹配。

模块http_stream
这个模块通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡,upstream后接负载均衡器的名字,后端realserver以 host:port options; 方式组织在 {} 中。如果后端被代理的只有一台,也可以直接写在 proxy_pass 。

2.2.4 location

http服务中,某些特定的URL对应的一系列配置项。

root /var/www/html
定义服务器的默认网站根目录位置。如果locationURL匹配的是子目录或文件,root没什么作用,一般放在server指令里面或/下。

index index.jsp index.html index.htm
定义路径下默认访问的文件名,一般跟着root放

proxy_pass http:/backend
请求转向backend定义的服务器列表,即反向代理,对应upstream负载均衡器。也可以proxy_pass http://ip:port。

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
这四个暂且这样设,如果深究的话,每一个都涉及到很复杂的内容,也将通过另一篇文章来解读。

访问控制 allow/deny

Nginx 的访问控制模块默认就会安装,而且写法也非常简单,可以分别有多个allow,deny,允许或禁止某个ip或ip段访问,依次满足任何一个规则就停止往下匹配。如:

1
2
3
4
5
6
7
8
9
10
location /nginx-status {
stub_status on;
access_log off;
# auth_basic "NginxStatus";
# auth_basic_user_file /usr/local/nginx-1.6/htpasswd;

allow 192.168.1.100;
allow 172.29.73.0/24;
deny all;
}

我们也常用 httpd-devel 工具的 htpasswd 来为访问的路径设置登录密码:

1
2
3
4
5
6
7
8
# htpasswd -c htpasswd admin
New passwd:
Re-type new password:
Adding password for user admin

# htpasswd htpasswd admin //修改admin密码
# htpasswd htpasswd sean //多添加一个认证用户
这样就生成了默认使用CRYPT加密的密码文件。打开上面nginx-status的两行注释,重启nginx生效。

nginx编译参数详细说明

1
–prefix=<path> – 安装路径,如果没有指定,默认为/usr/local/nginx。

–sbin-path=<path> – nginx可执行命令的文件,如果没有指定,默认为<prefix>/sbin/nginx。

–conf-path=<path> – 在没有使用-c参数指定的情况下nginx.conf的默认位置,如果没有指定,默认为<prefix>/conf/nginx.conf。

–pid-path=<path> – nginx.pid的路径,如果没有在nginx.conf中通过“pid”指令指定,默认为<prefix>/logs/nginx.pid。

–lock-path=<path> – nginx.lock文件路径,如果没有指定,默认为<prefix>/logs/nginx.lock。

–error-log-path=<path> – 当没有在nginx.conf中使用“error_log”指令指定时的错误日志位置,如果没有指定,默认为<prefix>/logs/error.log。

–http-log-path=<path> – 当没有在nginx.conf中使用“access_log”指令指定时的访问日志位置,如果没有指定,默认为<prefix>/logs/access.log。

–user=<user> – 当没有在nginx.conf中使用“user”指令指定时nginx运行的用户,如果没有指定,默认为“nobody”。

–group=<group> – 当没有在nginx.conf中使用“user”指令指定时nginx运行的组,如果没有指定,默认为“nobody”。

–builddir=DIR – 设置构建目录。

–with-rtsig_module – 启用rtsig模块。

–with-select_module –without-select_module – 如果在configure的时候没有发现kqueue, epoll, rtsig或/dev/poll其中之一,select模块始终为启用状态。

–with-poll_module –without-poll_module – 如果在configure的时候没有发现kqueue, epoll, rtsig或/dev/poll其中之一,poll模块始终为启用状态。

–with-http_ssl_module – 启用ngx_http_ssl_module,启用SSL支持并且能够处理HTTPS请求。需要OpenSSL,在Debian系统中,对应的包为libssl-dev。

–with-http_realip_module – 启用ngx_http_realip_module

–with-http_addition_module – 启用ngx_http_addition_module

–with-http_sub_module – 启用ngx_http_sub_module

–with-http_dav_module – 启用ngx_http_dav_module

–with-http_flv_module – 启用ngx_http_flv_module

–with-http_stub_status_module – 启用”server status”(服务状态)页

–without-http_charset_module – 禁用ngx_http_charset_module

–without-http_gzip_module – 禁用ngx_http_gzip_module,如果启用,需要zlib包。

–without-http_ssi_module – 禁用ngx_http_ssi_module

–without-http_userid_module – 禁用ngx_http_userid_module

–without-http_access_module – 禁用ngx_http_access_module

–without-http_auth_basic_module – 禁用ngx_http_auth_basic_module

–without-http_autoindex_module – 禁用ngx_http_autoindex_module

–without-http_geo_module – 禁用ngx_http_geo_module

–without-http_map_module – 禁用ngx_http_map_module

–without-http_referer_module – 禁用ngx_http_referer_module

–without-http_rewrite_module – 禁用ngx_http_rewrite_module。如果启用,需要PCRE包。

–without-http_proxy_module – 禁用ngx_http_proxy_module

–without-http_fastcgi_module – 禁用ngx_http_fastcgi_module

–without-http_memcached_module – 禁用ngx_http_memcached_module

–without-http_limit_zone_module – 禁用ngx_http_limit_zone_module

–without-http_empty_gif_module – 禁用ngx_http_empty_gif_module

–without-http_browser_module – 禁用ngx_http_browser_module

–without-http_upstream_ip_hash_module – 禁用ngx_http_upstream_ip_hash_module

–with-http_perl_module – 启用ngx_http_perl_module

–with-perl_modules_path=PATH – 为perl模块设置路径

–with-perl=PATH – 为perl库设置路径

–http-client-body-temp-path=PATH – 为http连接的请求实体临时文件设置路径,如果没有指定,默认为<prefix>/client_body_temp

–http-proxy-temp-path=PATH – 为http代理临时文件设置路径,如果没有指定,默认为<prefix>/proxy_temp

–http-fastcgi-temp-path=PATH - 为http fastcgi临时文件设置路径,如果没有指定,默认为<prefix>/fastcgi_temp

–without-http – 禁用HTTP服务

–with-mail – 启用IMAP4/POP3/SMTP代理模块

–with-mail_ssl_module – 启用ngx_mail_ssl_module

–with-cc=PATH – 设置C编译器路径

–with-cpp=PATH – 设置C预处理器路径

–with-cc-opt=OPTIONS – 变量CFLAGS中附加的参数,用于FreeBSD中的PCRE库,同样需要指定–with-cc-opt=”-I /usr/local/include”,如果我们使用select()函数则需要同时增加文件描述符数量,可以通过–with-cc-opt=”-D FD_SETSIZE=2048”指定。

–with-ld-opt=OPTIONS – 通过连接器的附加参数,用于FreeBSD中的PCRE库,同样需要指定–with-ld-opt=”-L /usr/local/lib”。

–with-cpu-opt=CPU – 指定编译的CPU,可用的值为: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

–without-pcre – 禁用PCRE库文件,同时将禁用HTTP rewrite 模块,如果要在”location”指令中使用正则表达式,同样需要PCRE库。

–with-pcre=DIR – 设置PCRE库源文件路径。

–with-pcre-opt=OPTIONS – 在编译时为PCRE设置附加参数。

–with-md5=DIR – 设置md5库源文件路径。

–with-md5-opt=OPTIONS – 在编译时为md5设置附加参数。

–with-md5-asm – 使用md5汇编源。

–with-sha1=DIR – 设置sha1库源文件路径。

–with-sha1-opt=OPTIONS – 在编译时为sha1设置附加参数。

–with-sha1-asm – 使用sha1汇编源。

–with-zlib=DIR – 设置zlib库源文件路径。

–with-zlib-opt=OPTIONS – 在编译时为zlib设置附加参数。

–with-zlib-asm=CPU – 为指定的CPU使用zlib汇编源进行优化,可用值为: pentium, pentiumpro。

–with-openssl=DIR – 设置openssl库源文件路径。

–with-openssl-opt=OPTIONS – 在编译时为openssl设置附加参数。

–with-debug – 启用debug记录。

–add-module=PATH – 增加一个在PATH中的第三方模块。

升级前系统镜像:CentOS 6.5 64位

内核版本:2.6.32-431.23.3.el6_x86_64

1、导入public key

rpm –import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

2、安装elrepo到内核为2.6.32的CentOS中

rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm

3、安装kernel-lt(long term support)长期支持版本

yum –enablerepo=elrepo-kernel install kernel-lt -y

注:

1)

如果直接通过yum方式使用elrepo源速度会较慢(该源在国外)。

推荐采用rpm的方式安装kernel-lt:

访问http://elrepo.org/linux/kernel/el6/x86_64/RPMS/下载对应的rpm包,通过rpm方式安装:

rpm -ivh kernel-lt-3.10.93-1.el6.elrepo.x86_64.rpm

2)

关于kernel-lt的介绍可以参考elrepo官网介绍:http://elrepo.org/tiki/kernel-lt

4、编辑grub.conf文件,修改Grub引导顺序

vim /etc/grub.conf

确认安装的新内核的位置,将default的值调整为新内核的顺序,如本次升级案例中新装的内核位置为0,所以将default修改为0,保存退出,reboot重启服务器。

一. 准备工作
确认内核及版本信息

1
2
[root@localhost ~]# uname -r
2.6.32-220.el6.x86_64

安装编译安装新内核,依赖于开发环境和开发库

1
2
3
4
5
6
yum grouplist  //查看已经安装的和未安装的软件包组,来判断我们是否安装了相应的开发环境和开发库;
yum groupinstall "Development Tools" //一般是安装这两个软件包组,这样做会确定你拥有编译时所需的一切
工具
yum install ncurses-devel //你必须这样才能让 make *config 这个指令正确地执行
yum install qt-devel //如果你没有 X 环境,这一条可以不用
yum install hmaccalc zlib-devel binutils-devel elfutils-libelf-devel //创建 CentOS-6 内核时需要它们

二. 编译内核

Linux内核版本有两种:稳定版和开发版 ,Linux内核版本号由3个数字组成:r.x.y
•r: 主版本号

•x: 次版本号,偶数表示稳定版本;奇数表示开发中版本。

•y: 修订版本号 , 表示修改的次数

获取并解压内核源码,配置编译项
http://www.kernel.org 首页,可以看到有stable, longterm等版本,longterm是比stable更稳定的版本,会长时间更新,选择 3.12.61。

Linux内核版本有两种:稳定版和开发版 ,Linux内核版本号由3个数字组成:r.x.y
•r: 主版本号
•x: 次版本号,偶数表示稳定版本;奇数表示开发中版本。
•y: 修订版本号 , 表示修改的次数

在系统原有的内核配置文件的基础上建立新的编译选项,所以复制一份到当前目录下,命名为.config

1
2
3
root@localhost linux-3.12.61]#tar -xf linux-3.12.61.tar.xz -C /usr/src/kernels/
root@localhost linux-3.12.61]#cd /usr/src/kernels/linux-3.12.61/
root@localhost linux-3.12.61]#cp /boot/config-2.6.32-220.el6.x86_64 .config

通过菜单方式配置内核使用make memuconfig,它便是根据需要定制模块

1
2
root@localhost linux-3.12.61]# cd /usr/src/kernels/linux-3.12.61
root@localhost linux-3.12.61]# make menuconfig

\\需要安装yum -y install ncurses-devel

这里使用下面方式继续配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@localhost ~]#sh -c 'yes "" | make oldconfig' //使用旧内核配置,并自动接受每个新增选项的默认设置
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
scripts/kconfig/conf --oldconfig Kconfig
.config:555:warning: symbol value 'm' invalid for PCCARD_NONSTATIC
.config:2567:warning: symbol value 'm' invalid for MFD_WM8400
.config:2568:warning: symbol value 'm' invalid for MFD_WM831X
.config:2569:warning: symbol value 'm' invalid for MFD_WM8350
.config:2582:warning: symbol value 'm' invalid for MFD_WM8350_I2C
.config:2584:warning: symbol value 'm' invalid for AB3100_CORE
.config:3502:warning: symbol value 'm' invalid for MMC_RICOH_MMC
*
* Restart config...
*
*
* General setup
*

... ...
XZ decompressor tester (XZ_DEC_TEST) [N/m/y/?] (NEW)
Averaging functions (AVERAGE) [Y/?] (NEW) y
CORDIC algorithm (CORDIC) [N/m/y/?] (NEW)
JEDEC DDR data (DDR) [N/y/?] (NEW)
#
# configuration written to .config
#

开始编译

1
2
3
root@localhost linux-3.12.61]# make -j4 bzImage  //生成内核文件
root@localhost linux-3.12.61]# make -j4 modules //编译模块
root@localhost linux-3.12.61]# make -j4 modules_install //编译安装模块

安装

1
root@localhost linux-3.12.61]#make install

如果出现了 ERROR: modinfo: could not find module xxx,数量少的话,可以忽略

修改grub引导,重启

安装完成后,需要修改Grub引导顺序,让新安装的内核作为默认内核。 编辑 grub.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vim /etc/grub.conf
#boot=/dev/sda1
device (hd0) HD(1,800,200000,b99f9008-34a9-4d88-8288-177f4cf74dc2)
default=0
timeout=5
splashimage=(hd0,1)/grub/splash.xpm.gz
hiddenmenu
title CentOS (3.12.61)
root (hd0,1)
kernel /vmlinuz-3.12.61 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS rd_NO_MD rd_LVM_LV=VolGroup/lv_swap LANG=zh_CN.UTF-8 rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet crashkernel=auto SYSFONT=latarcyrheb-sun16
initrd /initramfs-3.12.61.img
title CentOS (2.6.32-573.8.1.el6.x86_64)
root (hd0,1)
kernel /vmlinuz-2.6.32-573.8.1.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS rd_NO_MD rd_LVM_LV=VolGroup/lv_swap LANG=zh_CN.UTF-8 rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet crashkernel=auto
initrd /initramfs-2.6.32-573.8.1.el6.x86_64.img
title CentOS (2.6.32-431.el6.x86_64)
root (hd0,1)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS rd_NO_MD rd_LVM_LV=VolGroup/lv_swap LANG=zh_CN.UTF-8 rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img
`

在 grub.conf 文件中决定开机使用哪个内核版本做启动的参数是 default,默认为 0(代表从最新的内核启动,代表的内核版本从上往下依次是 0,1, 2 等)
reboot
[root@localhost ~]# uname -r
3.12.61

如果安装失败执行以下命令重新编译

1
2
# make mrproper         #完成或者安装过程出错,可以清理上次编译的现场
# make clean

软件

apache-tomcat-7.0.61.tar.gz http://tomcat.apache.org/download-70.cgi

jdk-8u77-linux-x64.rpm http://www.oracle.com/technetwork/java/javase/archive-139210.html

一、安装与配置Tomcat

1安装jdk

1
# rpm -ivh jdk-7u40-linux-x64.rpm

2、修改环境变量下面内容

1
2
3
4
5
6
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_77
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH
:wq
# source /etc/profile

3、测试java

1
2
3
4
java -version
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

二、安装tomcat
1、

1
2
3
4
tar zxvf apache-tomcat-7.0.42.tar.gz -C /usr/local/
ln -sv apache-tomcat-7.0.42 tomcat
cd tomcat/bin
./startup.sh

2、tomcat目录结构

bin ——Tomcat执行脚本目录

•conf ——Tomcat配置文件

•lib ——Tomcat运行需要的库文件(JARS)

•logs ——Tomcat执行时的LOG文件

•temp ——Tomcat临时文件存放目录

•webapps ——Tomcat的主要Web发布目录(存放我们自己的JSP,SERVLET,类)

•work ——Tomcat的工作目录,Tomcat将翻译JSP文件到的Java文件和class文件放在这里。
3、bin/主要主文件有,

•catalina.sh 用于启动和关闭tomcat服务器

•configtest.sh 用于检查配置文件

•startup.sh 启动Tomcat脚本

•shutdown.sh 关闭Tomcat脚本

4、conf目录下的文件

server.xml Tomcat 的全局配置文件

•web.xml 为不同的Tomcat配置的web应用设置缺省值的文件

•tomcat-users.xml Tomcat用户认证的配置文件

pdsh,与pssh类似,pdsh可并行执行对远程目标主机的操作,在有批量执行命令或分发任务的运维需求时,使用这个命令可达到事半功倍的效果。同时,pdsh还可支持交互模式,当要执行的命令不确定时,可直接进入pdsh命令行。

官网https://code.google.com/p/pdsh/

安装

1
2
3
4
5
6
1.tar jxvf pdsh-2.29.tar.bz2   
2.cd pdsh-2.29
3../configure --with-ssh --with-rsh --with-mrsh --with-mqshell --with-qshell --with-dshgroups --with-machines=/etc/pdsh/machines
4.make && make install
--with-dshgroups : 启用主机组支持
--with-machines : 是--with-dshgroups的扩展,通过所有要管理的主机列表都写入指定的文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
1.pdsh -V  
2.pdsh-2.29
3.rcmd modules: ssh,rsh,exec (default: rsh)
4.misc modules: machines,dshgroup
5.
6.pdsh -h
7.Usage: pdsh [-options] command ...
8.-S return largest of remote command return values
9.-h output usage menu and quit
10.-V output version information and quit #查看版本以及可用的模块
11.-q list the option settings and quit #列出pdsh执行时的一些配置信息
12.-b disable ^C status feature (batch mode)
13.-d enable extra debug information from ^C status
14.-l user execute remote commands as user #指定远程主机上使用的用户名。pdsh -R ssh -l opsuser -w user00[1-10] "date"
15.-t seconds set connect timeout (default is 10 sec) #指定连接远程主机的超时时间,以s为单位,默认是10s。pdsh -R ssh -w slave00[1-9] -t 15 "date"
16.-u seconds set command timeout (no default) #指定远程执行命令的超时时间,以s为单位,以ssh方式连接时,默认时间为无限
17.-f n use fanout of n nodes #设置同时连接到远程主机的数量
18.-w host,host,... set target node list on command line #指定远程主机,可以指定多台,每台主机用逗号隔开,host可以是主机名也可以是IP地址。eg: pdsh -w ssh:user001,ssh:user002 "date" 或者 pdsh -w ssh:user00[1-10] "date" 或者使用正则 pdsh -w ssh:user00[10-31],/1$/ "uptime" ,10-31中选择以1结尾的主机
19.-x host,host,... set node exclusion list on command line #排除某些主机。pdsh -R ssh -l opsuser -w user00[1-9] -x user005,user007 "date"
20.-R name set rcmd module to name #指定rcmd的模块名,默认是rsh。如果要选择sshpdsh -R ssh -w user00[1-10] "date"
21.-M name,... select one or more misc modules to initialize first
22.-N disable hostname: labels on output lines #用来关闭远程主机所返回结果的主机名显示
23.-L list info on all loaded modules and exit
24.-g groupname target hosts in dsh group "groupname" #用来指定一组远程主机,编译pdsh时可以通过--with-dshgroups 参数激活此选项,默认可以将一组主机列表写入一个文件并放到本地主机的~/.dsh/group或/etc/dsh/group目录下。eg: pdsh -R ssh -g userhosts "date",其中userhosts是一个主机列表文件,可以放到~/.dsh/group或/etc/dsh/group目录下
25.-X groupname exclude hosts in dsh group "groupname" #用来排除指定组内的所有主机,经常与-a参数一起使用。pdsh -R ssh -a -X userhosts "date"
26.-a target all nodes #可以指定所有的远程主机,pdsh会查看/etc/pdsh/machines文件中的主机列表,要改变此路径,在编译pdsh时通过--with-machines参数指定
27.available rcmd modules: ssh,rsh,exec (default: rsh)

1、pdsh批量统计主机信息

1.pdsh -w ssh:192.168.1.153,ssh:192.168.1.222 “uname -n”
2.192.168.1.222: localhost.localdomain
3.192.168.1.153: local-centos02

排除某些主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.pdsh -w ssh:192.168.1.[153-222] -x 192.168.1.[154-221] "uptime"  
2.192.168.1.153: 11:46:57 up 2:42, 2 users, load average: 0.00, 0.00, 0.00
3.192.168.1.222: 11:46:55 up 3 days, 3:26, 3 users, load average: 0.00, 0.01, 0.00

支持正则

1.pdsh -w ssh:192.168.1.[153-222],/2$/ "uptime"

对于不规范的主机,可以写到一个文件中,路径是编译时指定

1.cat /etc/pdsh/machines
2.aaa
3.bbb
4.user001
5.user1111
6.
7.pdsh -R ssh -a uptime

按照组进行调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1.cat /etc/dsh/group/userhosts  
2.
3.pdsh -R ssh -g userhosts uptime
4.
5.-X #排除指定组的主机
6.
7.pdsh -R ssh -X userhosts uptime
8.
9.pdsh 在远程主机上执行命令
10.
11.pdsh -R ssh -g userhosts "rm -rf /home/opsuser/mysql"
12.pdsh -R ssh -g userhosts "sudo mkdir /mnt/test"
13.pdsh -R ssh -g userhosts "sudo /etc/init.d/gmond start"

pdsh 交互模式

1.pdsh -R ssh -w 192.168.1.153

pdcp应用

pdcp主要是本地主机和远程主机进行文件复制,在使用pdcp时,两台主机都要安装pdcp


1.pdcp -R ssh -g userhosts /home/opsuser/mysqldb.tar.gz /home/opsuser #复制文件
2.pdcp -R ssh -w userhosts -r /home/opsuser/webdata /home/opsuser #复制目录

1、在A机器上使用ssh-keygen产生公钥私钥对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ssh-keygen -t rsa -b 1024
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
0c:f8:73:aa:46:52:06:ba:54:e0:fc:d4:40:58:07:33 root@HA-1
The key's randomart image is:
+--[ RSA 1024]----+
| .=E.. |
|o...*. |
| +.o... |
|..o o. o |
|.. + o S |
|. . . + |
| o . |
| .. |
| .. |
+-----------------+

2、第二步:用ssh-copy-id将公钥复制到远程机器中

1
2
3
4
5
 #ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
`

ssh-copy-id 将key写到远程机器的 ~/ .ssh/authorized_key.文件中

#ssh remote-host

一、注册微信公众号
https://mp.weixin.qq.com

关注微信号点击用户管理查看用户的微信ID号。在浏览器查看用户的微信ID号

https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=oOHYNvxkEyqwlYZzymXmGMUQxtKk&token=778777510&lang=zh_CN

获取微信接口git clone https://github.com/lealife/WeiXin-Private-API
接口配置文件

1
2
3
4
[root@zabbix ~]# git clone https://github.com/lealife/WeiXin-Private-API
[root@zabbix ~]# cp -r WeiXin-Private-API/ /usr/local/zabbix/share/zabbix/alertscripts/
[root@zabbix ~]# cd /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API
[root@zabbix WeiXin-Private-API]# chown zabbix.zabbix /usr/local/zabbix/share/zabbix/alertscripts/WeiXin-Private-API

先在conifg.php中配置公众账号信息:

1
2
3
4
5
6
7

$G_CONFIG["weiXin"] = array(
'account' => '公众平台账号',
'password' => '密码',
'cookiePath' => $G_ROOT. '/cache/cookie', // cookie缓存文件路径
'webTokenPath' => $G_ROOT. '/cache/webToken', // webToken缓存文件路径
);