linux安装Nginx
1.1将Nginx素材内容上传到/usr/local目录(pcre,zlib,openssl,nginx)(注意:必须登录用对这个文件具有操作权限的)
1.2安装pcre库
1.2.1 cd到/usr/local目录
1.2.2 tar -zxvf pcre-8.36.tar.gz 解压
1.2.3 cd pcre-8.36目录
1.2.4 执行./configure 编译 如果报错configure: error: no acceptable C compiler found in $PATH
安装gcc 库:yum install -y gcc gcc-c++ 如果发现进程被占用:kiil -9 PID 然后再次执行yum安装gcc即可
在此./configure编译
1.2.5 make编译
1.2.6 make install安装
1.3安装zlib库
1.3.1 cd到/usr/local目录
1.3.2 tar -zxvf zlib-1.2.8.tar.gz
1.3.3 cd zlib-1.2.8目录
1.3.4 ./configure编译
1.3.5 make
1.3.6 make install
1.4安装ssl
1.3.1 cd到/usr/local目录
1.3.2 tar -zxvf open-1.0.1.tar.gz
1.3.3 cd open-1.0.1l目录
1.3.4 ./config编译
1.3.5 make
1.3.6 make install
1.5安装Nginx
1.5.1 cd到/usr/local目录
1.5.2 tar -zxvf nginx-1.5.9.tar.gz
1.5.3 cd nginx-1.5.9目录
1.5.4 ./configure --prefix=/usr/local/nginx 注意编译之后的文件都放在/usr/local/nginx
1.5.5 make
1.5.6 make install
1.6启动测试
启动命令:/usr/local/nginx/sbin/nginx
本机浏览器测试:虚拟机IP地址即可 如果访问不了关闭防火墙 centos7关闭防火墙命令:systemctl stop firewalld.service 禁用:systemctl disable firewalld.service
keepalived 管理Nginx
什么keepalived
Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。
主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务。
上图是Keepalived的功能体系结构,大致分两层:用户空间(user space)和内核空间(kernel space)。
内核空间:主要包括IPVS(IP虚拟服务器,用于实现网络服务的负载均衡)和NETLINK(提供高级路由及其他相关的网络功能)两个部份。
用户空间:
- WatchDog:负载监控checkers和VRRP进程的状况
- VRRP Stack:负载负载均衡器之间的失败切换FailOver,如果只用一个负载均稀器,则VRRP不是必须的。
- Checkers:负责真实服务器的健康检查healthchecking,是keepalived最主要的功能。换言之,可以没有VRRP Stack,但健康检查healthchecking是一定要有的。
- IPVS wrapper:用户发送设定的规则到内核ipvs代码
- Netlink Reflector:用来设定vrrp的vip地址等。
Keepalived的所有功能是配置keepalived.conf文件来实现的。
linux安装keepalived
2.1 将keepalived上传到linux系统当中
2.2 cd /usr/local目录
2.3 tar -zxvf keepalived-xxxxxxxxx
2.4 切换到解压目录
2.5 yum install -y openssl openssl-devel
2.5 ./configure --prefix=/usr/local/keepalived 编译,将编译文件放到/usr/local/keepalived
2.6 make
2.7 make install
2.8 在/etc/keepalived 创建目录: mkdir /etc/keepalived
2.9 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
2.10 cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
2.11 cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
2.12 ln -s /usr/local/sbin/keepalived /usr/sbin/
2.13 ln -s /usr/local/keepalived/sbin/keepalived /sbin/
2.14 将nginx启动脚本和keepalived.conf配置上传到/etc/keepalived
2.15 主机:修改keepalived.conf配置文件
interface 配置成与主机相同的网络 ip a
mcast_src_ip 配置成自己机器的IP
virtual_ipaddress {
192.168.7.88 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}
2.16 启动keepalived 如果报错:Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.
解决办法:rm /usr/sbin/keepalived
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
2.17 备机keepalived.conf
state BACKUP 备机
interface 配置成与主机相同的网络 ip a
mcast_src_ip 配置成自己机器的IP
priority 90
virtual_ipaddress {
192.168.7.88 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}
注意:ip地址必须映射 、本机网络共享给vment8、在/ect/keepalived目录下配置keepalived.conf