概述
Nginx专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发链接的高处理能力(单台服务器可支持30000 ~ 50000个并发请求)正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx来提供Web服务
实验环境
系统:Linux6.5 IP :192.168.100.10
yum仓库已安装
共享文件夹挂载到Linux下
软件包链接:https://pan.baidu.com/s/1f6Zt8cmkCL3qXICjhYipWw 密码:f1v3
实验目的
成功搭建出Nginx网页服务
实验步骤
1.编译安装nginx
(1)安装支持软件
[root@localhost ~ ]# yum -y install pcre-devel zlib-devel
(2)创建运行用户、组
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
//建立nginx用户,不建立宿主文件夹,禁止登陆shell环境
(3)编译安装nginx
[root@localhost ~]# mount.cifs //192.168.100.8/ww /abc //挂载共享文件夹到linux目录下
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt/ //解压软件包到opt目录下
[root@localhost abc]# cd nginx-1.12.0/ //切换目录
[root@localhost nginx-1.12.0]# ./configure \
>--prefix=/usr/local/nginx \ //设置Nginx安装位置
>--user=nginx \ //用户
>--group=nginx \ //组
>--with-http_stub_status_module ////开启stub_status状态统计模块//
[root@localhost nginx-1.2.8]# make && make install //进行编译安装
(4)为nginx创建链接文件,方便管理员调用命令
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# ls -l /usr/local/nginx/sbin/nginx
2.nginx的运行控制
(1)检查配置文件
[root@localhost ~]# nginx -t //检查配置文件
(2)启动,停止
[root@localhost ~]# nginx //启动服务
[root@localhost ~]# killall -1 nginx //重启
[root@localhost ~]# killall -3 nginx //停止
[root@localhost ~]# yum install elinks –y //安装elinks浏览器
[root@localhost ~]# elinks http://localhost //使用elinks浏览器查看
(3)使用nginx服务脚本
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx" //主程序路径
PIDF="/usr/local/nginx/logs/nginx.pid" //PID存放路径
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF) //根据PID终止Nginx进程
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF) //根据进程号重载配置
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx //给脚本添加执行权限
[root@localhost ~]# chkconfig --add nginx //添加为系统服务
[root@localhost ~]# service start nginx //开启nginx 服务
[root@localhost ~]# service status nginx //查看nginx运行状态
3.打开网页查看