Nginx是一个网站架设软件,与Apache一样可完成网站架设。由俄罗斯程序设计师伊戈尔·赛索耶夫开发。目前新浪、网易、腾讯、Purk等都在使用。Nginx,专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力,单台物理服务器可支持30000~50000个并发请求。正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx提供Web服务。
Niginx与Apache
- 轻量级,同样web服务,比Apache占用更少的内存及资源
- 高并发,nginx处理请求是异步非阻塞的,而Apache则是阻塞型的,在高并发下nginx能保持低资源低消耗高性能
- 高度模块化的设计,编写模块相对简单
- 可作为负载均衡服务器,支持7层负载均衡
- 静态处理性能比Apache高3倍以上,但动态处理不足,需要通过后端服务器支持,如Apache
实验环境
- 系统环境:centos7.4
- 服务器IP地址:192.168.100.71
- 客户端IP地址:192.168.100.77
- yum挂载目录:/mnt/sr0
- 相关源码包下载地址:百度云下载 ??密码:uhzm
搭建步骤
一、准备工作
1、关闭防火墙及selinux
[[email protected] ~]# systemctl stop firewalld.service #关闭防火墙
[[email protected] ~]# systemctl disable firewalld.service #随开机自动关闭
[[email protected] ~]# vim /etc/sysconfig/selinux
[[email protected] ~]# reboot #重启Linux生效
二、搭建DNS服务器
1、安装bind软件包
[[email protected] ~]# cd /mnt/sr0/Packages/
[[email protected] Packages]# rpm -ivh bind-9.9.4-50.el7.x86_64.rpm
2、修改主配置文件named.conf
[[email protected] ~]# vim /etc/named.conf
3、创建并修改DNS正向解析文件
[[email protected] ~]# cd /var/named/
[[email protected] named]# cp -p named.localhost bt.com.zone #将模板文件改为正向解析文件进行修改
[[email protected] ~]# vim /var/named/bt.com.zone #修改正向解析文件
4、启动DNS服务器
[[email protected] ~]# systemctl start named.service
5、修改DNS客户机配置文件
[[email protected] ~]# echo "nameserver 192.168.100.71" >> /etc/resolv.conf
6、测试DNS解析
[[email protected] ~]# nslookup www.bt.com #nslookup 用于查询DNS的记录,查看域名解析是否正常
二、搭建Nginx服务
1、安装相应的工具包以及C语言编译器
[[email protected] ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++
2、创建Nginx进程用户
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
3、配置、编译及编译安装Nginx源码包
[[email protected] ~]# tar xvf nginx-1.12.0.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/nginx-1.12.0/
[[email protected] nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-http_stub_status_module
参数解析:
prefix #指定安装位置
user=nginx #指定nginx系统用户
group=nginx #指定组
with-http_stub_status_module #添加日志统计模块
[[email protected] nginx-1.12.0]# make && make install
4、优化路径
[[email protected] ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
三、设置Nginx运行控制
1、检测语法
[[email protected] ~]# nginx -t #测试配置是否有语法错误
2、启动Nginx服务
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf #修改主配置文件
[[email protected] ~]# nginx #启动Nginx
[[email protected] ~]# netstat -anpt | grep ":80" #检测服务是否启动
[[email protected] ~]# cat /usr/local/nginx/logs/nginx.pid #查看pid
3、停止Nginx服务
[[email protected] ~]# kill -1 3809 #平滑重启Nginx服务。相当于killall -s HUP nginx;nginx -s reload
[[email protected] ~]# kill -3 1514 #关闭Nginx服务。相当于killall -s QUIT nginx;nginx -s quit
[[email protected] ~]# nginx #重启服务
[[email protected] ~]# cat /usr/local/nginx/logs/nginx.pid #查看pid号
4、编辑Nginx服务脚本
[[email protected] ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=Nginx Server Control Script #说明
After=network.target #描述服务类别[Service]
Type=forking #后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid #PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx #启动服务
ExecReload=/usr/bin/kill -s HUP $PIDFile #重载服务
ExecStop=/usr/bin/kill -s QUIT $PIDFile #停止服务[Install]
WanteBy=multi-user.target
[[email protected] ~]# systemctl daemon-reload #重新加载服务单元
[[email protected] ~]# systemctl enable nginx.service #设置开机自启动
[[email protected] ~]# systemctl stop nginx.service#停止服务
[[email protected] ~]# systemctl start nginx.service#开启服务
[[email protected] ~]# systemctl reload nginx.service#平滑重启服务
[[email protected] ~]# systemctl restart nginx.service#重启服务
四、构建静态网页、统计访问状态
1、编辑主配置文件
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
[[email protected] ~]# systemctl restart nginx.service #重启服务
2、编辑首页文件
[[email protected] ~]# cd /usr/local/nginx/html/ #进入站点目录
[[email protected] html]# vim index.html #编辑首页