centos 7 之nginx
环境信息
[root@node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.1. (Core)
[root@node1 ~]# uname -r
3.10.-.el7.x86_64
yum安装nginx
查看是有安装包
[root@node1 ~]# yum list | grep nginx
如果没有配置配置epel源
[root@node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
如果没有wget则安装
[root@node1 ~]# yum -y install wget
安装nginx
[root@node1 ~]# yum -y install nginx
查看版本
[root@node1 ~]# nginx -V
nginx version: nginx/1.10.
启动 nginx
[root@node1 nginx]# nginx
关闭
[root@node1 nginx]# nginx -s stop
重启
[root@node1 nginx]# nginx -s reload
安装netstat 命令并查看端口
[root@node1 nginx]# yum -y install net-tools
[root@node1 nginx]# netstat -lntup
开机启动
[root@node1 ~]# systemctl enable nginx
编译安装
安装环境
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel
tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y
安装启动nginx
下载源码包
[root@localhost opt]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
解压源码包
[root@localhost opt]# tar -zxvf nginx-1.12.0.tar.gz
配置,编译安装
[root@localhost nginx-1.12.0]# ./configure --prefix=/opt/nginx1-12/ --with-http_ssl_module --with-http_stub_status_module [root@localhost nginx-1.12.0]# make && make install
启动nginx
[root@localhost nginx-1.12.0]# cd ../ [root@localhost opt]# cd nginx1-12/sbin [root@localhost sbin]# ./nginx #启动 [root@localhost sbin]# ./nginx -s stop #关闭 [root@localhost sbin]# ./nginx -s reload #平滑重启
添加环境变量,在 /etc/profile文件中尾部添加
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/nginx1-12/sbin" 刷新
[root@localhost ~]# source /etc/profile
安装完成后检测服务
[root@localhost sbin]# curl -I 10.0.0.21
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Mon, 11 Mar 2019 05:38:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 11 Mar 2019 05:29:12 GMT
Connection: keep-alive
ETag: "5c85f228-264"
Accept-Ranges: bytes
日志文件
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;
配置站点
配置nginx.conf文件
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {
listen ;
server_name www.demo.com;
location / {
root /var/www/demo;
index index.html ;
}
location /wx{
root /var/www/;
index index.html ;
}
error_page /50x.html;
location = /50x.html {
root html;
}
}
}
创建 /var/www/demo 和 /var/www/wx 目录
[root@node1 ~]# mkdir -p /var/www/demo
[root@node1 ~]# mkdir -p /var/www/wx
将 www 的用户和用户组改为nginx
[root@node1 var ]# chown -R nginx.nginx .
分别在 demo 和 wx 目录下创建index.html文件并写入数据
[root@node1 demo]# echo "demo" >/var/www/demo/index.html
[root@node1 demo]# cd ../wx/
[root@node1 wx]# echo "wx" >/var/www/wx/index.html
[root@node1 www]# cat demo/index.html
demo
[root@node1 www]# cat wx/index.html
wx
通过crul 命令进行测试也可以通过浏览器访问IP或域名+目录进行测试(不要忘了hosts做解析)
[root@node1 www]# curl 10.0.0.22
demo
[root@node1 www]# curl 10.0.0.22/wx/
wx
配置多个虚拟主机
在配置文件中与第一个server平级写入
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
server_name www.wl21.com; location / {
root /opt/wl21/;
index index.html;
} }
负载均衡
环境
主机名 | IP | 说明 |
---|---|---|
node2 | 10.0.0.21 | 负载 |
node3 | 10.0.0.22 | web01服务器 |
node4 | 10.0.0.23 | web02服务器 |
node2配置
[root@node2 nginx]# vim nginx.conf worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
upstream www_server_pools {
server 10.0.0.22: weight=;
server 10.0.0.23: weight=;
}
server {
listen ;
server_name www.demo.com;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://www_server_pools;
}
}
}
node2 的IP和域名需要解析
[root@node2 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
:: localhost localhost.localdomain localhost6 localhost6.localdomain6 10.0.0.19 www.demo.com
node3 和 node4 配置相同
[root@node3 nginx]# vim nginx.conf worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {
listen ;
server_name www.demo.com;
location / {
root /var/www/demo;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
}
}
创建 /var/www/demo 目录并创建 index.html 文件
10.0.0.22 配置
[root@node3 ~]# mkdir -p /var/www/demo
[root@node3 ~]# echo "demo3" >/var/www/demo/index.html
10.0.0.23 配置
[root@node4 ~]# mkdir -p /var/www/demo
[root@node4 ~]# echo "demo4" >/var/www/demo/index.html
域名解析后通过域名进行访问
访问结果
Keepalived 高可用
环境
主机名 | IP | 说明 |
---|---|---|
node1 | 10.0.0.20 | 备负载 |
node2 | 10.0.0.21 | 主负载 |
node3 | 10.0.0.22 | web01服务器 |
node4 | 10.0.0.23 | web02服务器 |
安装 keepalived (10.0.0.20 备负载同样安装)
[root@node2 ~]# yum -y install keepalived
[root@node2 ~]# rpm -qa keepalived
配置keepalived (主负载配置)
[root@node2 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout
router_id node2 #路由器标识,一个局域网内是唯一的
} vrrp_instance VI_1 { #一个实例
state MASTER #角色 有 Master 和 Backup 两种
interface eth0 #通信接口
virtual_router_id 51 #虚拟路由标识,在一个配置文件内唯一
priority 150 #竞选优先级
advert_int 1 #同步通知间隔
authentication { #权限认证
auth_type PASS
auth_pass
}
virtual_ipaddress { #虚拟IP地址 实际中为域名相对应的IP
10.0.0.19
}
}
配置keepalived (备负载配置)
[root@node1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout
router_id node1
} vrrp_instance VI_1 {
state BACKUP #角色,有Master和Backup两种
interface eth0
virtual_router_id
priority 100 #竞选优先级 数值越大优先级越高
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
10.0.0.19
}
}
配置完成后重启keepalived
[root@node2 ~]# systemctl restart keepalived
[root@node1 ~]# systemctl restart keepalived
查看node2 主负载IP
[root@node2 ~]# ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::1c:0e: brd ff:ff:ff:ff:ff:ff
inet 10.0.0.21/ brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet 10.0.0.19/ scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1c:e18/ scope link
valid_lft forever preferred_lft forever
关闭主负载 keepalivied 再次查看主负载IP
[root@node2 ~]# systemctl stop keepalived
[root@node2 ~]# ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::1c:0e: brd ff:ff:ff:ff:ff:ff
inet 10.0.0.21/ brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1c:e18/ scope link
valid_lft forever preferred_lft forever
此时查看备负载IP
[root@node1 ~]# ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::::b8 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.20/ brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet 10.0.0.19/ scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe29:39b8/ scope link
valid_lft forever preferred_lft forever
启动主负载keepalived 并查看IP
root@node2 ~]# systemctl start keepalived
[root@node2 ~]# ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::1c:0e: brd ff:ff:ff:ff:ff:ff
inet 10.0.0.21/ brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet 10.0.0.19/ scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1c:e18/ scope link
valid_lft forever preferred_lft forever
域名IP回到了主负载上面,此时备负载不在有域名IP
[root@node1 ~]# ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
valid_lft forever preferred_lft forever
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::::b8 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.20/ brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe29:39b8/ scope link
valid_lft forever preferred_lft forever
最后要将keepalives 加入开机启动
[root@node1 ~]# systemctl enable keepalived
[root@node2 ~]# systemctl enable keepalived
centos 7 之nginx的更多相关文章
- CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx 1.6.2)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...
- CentOS 6.6 nginx PHP 配置
/************************************************************************* * CentOS 6.6 nginx PHP 配置 ...
- CentOS 6.6 nginx install
/************************************************************************* * CentOS 6.6 nginx instal ...
- 删:Centos 7安装Nginx 1.8
[CentOS 7] 安装nginx! 首先进行 nginx yum Nginx安装记录 注意:如果用源码安装,nginx配置时需要指定--with-pcer对应的压缩包路径,如果使用二进制安装不需要 ...
- linux/centos下安装nginx(rpm安装和源码安装)详细步骤
Centos下安装nginx rpm包 ...
- CentOS下安装Nginx并添加nginx_upload_module
安装前,最好能保证依赖的系统软件已经升级. yum update CentOS上安装Nginx,如果只是简单安装,不附加其他第三方模块,一句话可以搞定: yum install nginx ...
- CentOS 7安装nginx
CentOS 7安装nginx 参考网上其他文章做的 安装Nginx 我们从nginx官方的RPM源来安装一个预构建的稳定版本的nginx包. rpm --import http://nginx.or ...
- CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 负载均衡源码安装
CentOS 6.5 + Nginx 1.8.0 + PHP 5.6(with PHP-FPM) 负载均衡源码安装 http://www.cnblogs.com/ppoo24/p/4918288.ht ...
- CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒
http://chenhao6.blog.51cto.com/6228054/1323192 标签:软件包 配置文件 nagios 服务端 监控 原创作品,允许转载,转载时请务必以超链接形式标明文章 ...
- 在CentOS 上搭建nginx来部署静态页面网站
在centOs 上搭建nginx来部署静态页面网站 一.部署服务器环境 nginx:轻量级.高性能的HTTP及反向代理服务器,占用内存少,并发能力强,相比老牌的apache作为web服务器,性能更加卓 ...
随机推荐
- python---数学表达式的分析树实现
先走一遍, 前面很多知道点,都串起来了. # coding = utf-8 # 使用列表实现栈的功能 class Stack: def __init__(self): self.items = [] ...
- 运维基础——Zabbix 设置Redis监控
https://blog.csdn.net/xundh/article/details/77604357
- jquery .On()绑定事件的触发机制
选择器只能选择已存在元素,其他元素需要作为参数传递给on
- js中时间大小的比较
今天在前台做到一个需要比较两个日期大小的地方,乍一看,发现一个比较奇怪地地方: var t1 = new Date(2018,1,1), t2 = new Date(2018,1,1); consol ...
- flink--DateSet开发--简单入门
开发流程 1. 获得一个execution environment, 2. 加载/创建初始数据, 3. 指定这些数据的转换, 4. 指定将计算结果放在哪里, 5. 触发程序执行 例子: object ...
- HDFS-HA高可用 | Yarn-HA
HDFS-HA HA(High Available),即高可用(7*24小时不中断服务) 单点故障即有一台机器挂了导致全部都挂了:HA就是解决单点故障,就是针对NameNode: 主Active:读写 ...
- Spring错误小结
之前在做项目的时候发现无法解析xml文件导致项目运行失败的问题,其实是无法找到导入的Schema约束,在配置Schema约束的时候在XML Catalog 中key配置*.xsd网址,location ...
- JavaEE 之 Mybatis
1.Mybatis a.定义:MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架 b.步骤: ①在src下创建 SqlMapConfig.xml 及 datasource.pr ...
- 转 国内的go get问题的解决
转 国内的go get问题的解决 go get golang.org/x 包失败解决方法 由于各种问题,国内使用 go get 安装 golang 官方包可能会失败,如我自己在安装 colli ...
- Python语言说明
第一章:Python入门一.语言什么是语言:人与人之间的沟通计算机语言:计算机语言,即人和计算机之间的沟通语言. 按照级别分类:机器语言:最底层,最低级的语言,只能识别0/1,电平信号汇编语言:计算机 ...