源码编译安装Nginx服务及访问控制(实战!)

关于Nginx

款高性能、轻量级Web服务软件

  • 稳定性高

  • 系统资源消耗低

  • 对HTTP并发连接的处理能力高

  • 单台物理服务器可支持30000 ~ 50000个并发请求

Nginx编译安装

1.宿主机共享所需的工具包

源码编译安装Nginx服务及访问控制(实战!)

2.虚拟机挂载共享目录

[root@localhost ~]# smbclient -L //192.168.100.50/
Enter SAMBA\root's password: 
OS=[Windows 10 Enterprise LTSC 2019 17763] Server=[Windows 10 Enterprise LTSC 2019 6.3]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       远程 IPC
    share           Disk      
    tools           Disk      
    Users           Disk      
Connection to 192.168.100.50 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
NetBIOS over TCP disabled -- no workgroup available
[root@localhost ~]# mkdir /mnt/tools
[root@localhost ~]# mount.cifs //192.168.100.50/tools /mnt/tools/
Password for root@//192.168.100.50/tools:  
[root@localhost ~]# cd /mnt/tools/
[root@localhost tools]# ls
awstats-7.6.tar.gz                extundelete-0.2.4.tar.bz2  forbid.png                 jdk-8u191-windows-x64.zip  LAMP-C7  picture.jpg
cronolog-1.6.2-14.el7.x86_64.rpm  fiddler.exe                intellijideahahau2018.rar  john-1.8.0.tar.gz          LNMP
[root@localhost tools]#

3.解压Nginx源码包

[root@localhost tools]# cd LNMP/
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip  mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
[root@localhost LNMP]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
.......//省略解压过程

4.安装编译Nginx所需环境包

[root@localhost LNMP]# yum -y install gcc gcc-c++ pcre-devel zlib-devel
........//省略安装过程
[root@localhost LNMP]#

5.新建一个程序用户nginx

[root@localhost LNMP]# useradd -M -s /sbin/nologin nginx   //-M,不创建家目录
[root@localhost LNMP]# id nginx     //查看nginx用户
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
[root@localhost LNMP]#

6.配置Nginx服务

[root@localhost LNMP]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \           //安装路径
> --user=nginx \             //属主
> --group=nginx \          //属组
> --with-http_stub_status_module    //开启统计模块
........//省略配置过程

7.编译安装Nginx服务

[root@localhost nginx-1.12.2]# make && make install
.........//省略编译过程
[root@localhost nginx-1.12.2]# 

8.优化nginx命令执行路径

[root@localhost nginx-1.12.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost sbin]# 

9.开启nginx服务

[root@localhost sbin]# nginx -t   //检查测试配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# nginx    //开启服务
[root@localhost sbin]# netstat -ntap | grep 80  //查看80端口
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      52709/nginx: master 
[root@localhost sbin]#

10.关闭防火墙和增强性安全功能

[root@localhost sbin]# systemctl stop firewalld.service   //关闭防火墙
[root@localhost sbin]# setenforce 0   //关闭增强性安全功能
[root@localhost sbin]# 

11.安装elinks工具,测试nginx服务

[root@localhost sbin]# yum install elinks -y   //安装工具
.........//省略安装过程
[root@localhost sbin]# 
[root@localhost sbin]# elinks http://localhost    //测试能否访问nginx服务

源码编译安装Nginx服务及访问控制(实战!)

12.用浏览器测试能否访问nginx服务(访问成功)

源码编译安装Nginx服务及访问控制(实战!)

Nginx服务优化

1.nginx服务基础命令

[root@localhost sbin]# killall -s QUIT nginx     //停止服务
[root@localhost sbin]# killall -3 nginx        //停止服务
[root@localhost sbin]# killall -s HUP nginx     //重载服务
[root@localhost sbin]# killall -1 nginx     //重载服务
[root@localhost sbin]# nginx    //启动服务

2.制作管理服务的脚本

[root@localhost sbin]# 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"
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    ;;
  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 sbin]# chmod +x /etc/init.d/nginx    //添加执行权限
[root@localhost sbin]# chkconfig --add nginx   //添加让系统可以识别
[root@localhost sbin]# 

3.测试服务管理脚本

[root@localhost sbin]# service nginx stop    //停止服务
[root@localhost sbin]# netstat -ntap | grep 80   //查看80端口,无
[root@localhost sbin]# service nginx start    //开启服务
[root@localhost sbin]# netstat -ntap | grep 80   //查看80端口,有
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      53614/nginx: master 
[root@localhost sbin]# 

4.修改配置文件,开启统计功能

[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /status {
            stub_status on;
            access_log off;
        }

[root@localhost sbin]# service nginx stop
[root@localhost sbin]# service nginx start 
[root@localhost sbin]#

5.测试统计功能

源码编译安装Nginx服务及访问控制(实战!)

Nginx服务访问控制

1.修改配置文件,开启密码访问功能

[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/passwd.db;
            root   html;
            index  index.html index.htm;
        }
        location /status {
            stub_status on;
            access_log off;
        }
[root@localhost sbin]#

2.安装密码访问工具

[root@localhost sbin]# yum install httpd-tools -y
........//省略安装过程
[root@localhost sbin]# 

3.创建访问登录的用户和密码

[root@localhost sbin]# htpasswd -c /usr/local/nginx/passwd.db test  
New password: 
Re-type new password: 
Adding password for user test
[root@localhost sbin]# cat /usr/local/nginx/passwd.db 
test:$apr1$od5a34WH$MduYUJbQ2W0oihB0Bs/bx.
[root@localhost sbin]# 
[root@localhost sbin]# service nginx stop
[root@localhost sbin]# service nginx start 
[root@localhost sbin]# 

4.测试访问控制(成功)

源码编译安装Nginx服务及访问控制(实战!)

源码编译安装Nginx服务及访问控制(实战!)

猜你喜欢

转载自blog.51cto.com/14449541/2449634