详解Linux下安装配置Nginx
1.先解压
[root@Master nginx-1.8.1]# tar -zxvf nginx-1.8.1.tar.gz -C /usr/local/src/ (/usr/local/src/是大家约定放置的目录)
2.进入到nginx源码目录
[root@Master ~]# cd /usr/local/src/
[root@Master src]# ll
总用量 4
drwxr-xr-x. 8 hadoop hadoop 4096 1月 26 2016 nginx-1.8.1
[root@Master src]# cd nginx-1.8.1/
[root@Master nginx-1.8.1]# ll
总用量 644
drwxr-xr-x. 6 hadoop hadoop 4096 4月 30 13:22 auto
-rw-r--r--. 1 hadoop hadoop 251319 1月 26 2016 CHANGE
-rw-r--r--. 1 hadoop hadoop 383019 1月 26 2016 CHANGES.ru
drwxr-xr-x. 2 hadoop hadoop 4096 4月 30 13:22 conf
-rwxr-xr-x. 1 hadoop hadoop 2478 1月 26 2016 configure
drwxr-xr-x. 4 hadoop hadoop 68 4月 30 13:22 contrib
drwxr-xr-x. 2 hadoop hadoop 38 4月 30 13:22 html
-rw-r--r--. 1 hadoop hadoop 1397 1月 26 2016 LICENSE
drwxr-xr-x. 2 hadoop hadoop 20 4月 30 13:22 man
-rw-r--r--. 1 hadoop hadoop 49 1月 26 2016 README
drwxr-xr-x. 8 hadoop hadoop 71 4月 30 13:22 src
3.检查安装环境,并指定将来要安装的路径
[root@Master nginx-1.8.1]# ./configure --prefix=/usr/local/nginx
检查安装环境成功如下所示:
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using builtin md5 code
+ sha1 library is not found
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/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"
源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)。
Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./configure –help输出详细的选项列表。
其中--prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较凌乱。
如果配置--prefix,如:
- ./configure --prefix=/usr/local/test
可以把所有资源文件放在/usr/local/test的路径中,不会杂乱。
用了—prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。
当然要卸载程序,也可以在原来的make目录下用一次make uninstall,但前提是make文件指定过uninstall
4.使用yum安装缺少的包
yum -y install gcc pcre-devel .......
5.编译安装
make&&make install
安装完成后测试是否正常
1.进入/usr/local/nginx/bin目录
2.启动nginx脚本文件
[root@Master sbin]# ./nginx
3.查看端口是否有nginx进程监听(80端口) [root@Master sbin]# netstat -ntlp Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 32829/mysqld tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1488/nginx: master tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2533/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1293/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1290/cupsd tcp6 0 0 :::8009 :::* LISTEN 41740/java tcp6 0 0 :::8080 :::* LISTEN 41740/java tcp6 0 0 :::22 :::* LISTEN 1293/sshd tcp6 0 0 127.0.0.1:8005 :::* LISTEN 41740/java
反向代理配置
[root@Master conf]# vi nginx.conf
配置nginx.conf的配置文件
server { listen 80; server_name Master; #nginx所在服务器的主机名 #charset koi8-r; #access_log logs/host.access.log main; #反向代理的配置 location / { #拦截所有请求 root html; proxy_pass http://192.168.0.21:8080; #这里是代理走向的目标服务器:tomcat }
动静分离
#动态资源 index.jsp
location ~ .*\.(jsp|do|action)$ { # proxy_pass http://192.168.0.21:8080; #}
#静态资源
location ~ .*\.(html|js|css|gif|jpg|jpng|png)$ { (~表示大小写敏感,.表示任何非回车字符,*表示多个或0个,\.表示.)
expires 3d; #缓存3天
}
负载均衡
upstream 后的名字可以随意取,但是要和location下的proxy_pass http://后的保持一致
http {
upstream tomcats {
server Master:8080 weight=1;
server Slave1:8080 weight=1;
server Slave2:8080 weight=1;
}
location ~ .*\.(jsp|do|action)$ {
# proxy_pass http://tomcats; #tomcats是后面tomcat服务器组的逻辑组号
#}
}
利用keepalived实现高可靠(HA)
1.安装keepalived
2.将keepalived添加到系统服务中(可以使用service调用)
[root@Master local]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@Master local]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
[root@Master local]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Master local]# mkdir -p /etc/keepalived
[root@Master local]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[root@Master local]# chmod +x /etc/init.d/keepalived
3.添加keepalived到开机自启动
[root@Master local]# chkconfig --add keepalived
[root@Master local]# chkconfig keepalived on
配置keepalived虚拟IP
进入配置文件
[root@Master local]# vi /etc/keepalived/keepalived.conf
#Master节点配置
vrrp_instance VI_1 {
state MASTER #指定A节点为主节点 ,备用节点上设置为BACKUP即可
interface eth0 #绑定虚拟IP的网络接口
virtual_router_id 51 #VRRP组名,两个节点必须一样,以指明属于同一个组
priority 100 #主节点的优先级(1-254之间) ,备用节点必须必主节点优先级低
advert_int 1 #组播信息发送间隔,两个节点设置必须一样
authentication { #设置验证信息
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定虚拟IP,两个节点设置一样,必须和Ngnix服务器属于同一网段
192.168.200.16
192.168.200.17
192.168.200.18
}
}
###启动机器上的keepalived
service keepalived start
多个ip查看 ip addr
配置keepalived心跳检查
进入配置文件
[root@Master local]# vi /etc/keepalived/keepalived.conf
原理:
keepalived并不跟nginx耦合,它俩完全不是一家人
但是keepalived提供一个机制:让用户自定义一个shell脚本去检测用户自己的程序,返回状态给keepalived就可以了。
vrrp_script chk_health{
script "|| ps -ef | grep nginx | grep -v grep | wc -l -ge 2||&& exit 0 || exit 1"
interval 1 #每隔1秒执行上面的脚本,去检查ngnix程序
weight -2 #返回1,即出现宕机时,weight-2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script{ #跟踪脚本
chk_health
}
#管理ngnix用户程序,防止ngnix宕机之后不能重启
notify_master "/usr/local/keepalived/sbin/notify.sh master" (成为Master)
notify_backup "/usr/local/keepalived/sbin/notify.sh backup" (成为backup)
notify_fault "/usr/local/keepalived/sbin/notify.sh fault" (出现异常)
切换到/usr/local/keepalived/sbin,建立脚本notify.sh,自己写
case “$1” in
master)
/usr/local/keepalived/sbin/nginx
exit 0
::
backup)
/usr/local/keepalived/sbin/nginx -s stop
/usr/local/keepalived/sbin/nginx
exit 0
::
fault)
/usr/local/keepalived/sbin/nginx -s stop
exit 0
::
*)
exit 1
::
esac