关于Nginx
一款高性能,轻量级web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
环境
一台Linux服务器(192.168.13.128)
一台win10测试机
一,在Windows上将LAMP所需压缩软件包共享出来(此处如有问题请看之前的博客相关文章)
二,在Linux上使用远程共享获取文件并挂载到mnt目录下
[root@localhost ~]# smbclient -L //192.168.100.3/ ##远程共享访问
Enter SAMBA\root's password:
Sharename Type Comment
--------- ---- -------
LAMP-C7 disk
[root@localhost ~]# mount.cifs //192.168.100.3/LAMP-C7 /mnt ##挂载到/mnt目录下
三,编译安装Nginx
1,解压源码包到/opt下,并查看
[root@localhost ~]# cd /mnt ##切换到挂载点目录
[root@localhost mnt]# ls
apr-1.6.2.tar.gz discuz_X2.5_SC_UTF8.zip LAMP-PHP5.6.txt
apr-util-1.6.0.tar.gz error.png mysql-5.6.26.tar.gz
awstats-7.6.tar.gz httpd-2.4.29.tar.bz2 Nginx-1.12.0.tar.gz
cronolog-1.6.2-14.el7.x86_64.rpm kali.jpg PHP-5.6.11.tar.bz2
[root@localhost mnt]# tar zxvf Nginx-1.12.0.tar.gz -C /opt ##解压Nginx源码包到/opt下
[root@localhost mnt]# cd /opt/ ##切换到解压的目录下
[root@localhost opt]# ls
Nginx-1.12.0 rh
2,安装编译需要的环境组件包
[root@localhost opt]# yum -y install \
gcc \ //c语言
gcc-c++ \ //c++语言
pcre-devel \ //pcre语言工具
zlib-devel //数据压缩用的函式库
3,创建程序用户Nginx并编译Nginx
[root@localhost opt]# useradd -M -s /sbin/nologin Nginx ##创建程序用户,安全不可登陆状态
[root@localhost opt]# id Nginx
uid=1001(Nginx) gid=1001(Nginx) 组=1001(Nginx)
[root@localhost opt]# cd Nginx-1.12.0/ ##切换到Nginx目录下
[root@localhost Nginx-1.12.0]# ./configure \ ##配置Nginx
> --prefix=/usr/local/Nginx \ ##安装路径
> --user=Nginx \ ##用户名
> --group=Nginx \ ##用户组
> --with-http_stub_status_module ##状态统计模块
4,编译和安装
[root@localhost Nginx-1.12.0]# make ##编译
...
[root@localhost Nginx-1.12.0]# make install ##安装
...
5,优化Nginx启动脚本,以便于系统识别
[root@localhost Nginx]# ln -s /usr/local/Nginx/sbin/Nginx /usr/local/sbin/ ##创建软连接让系统识别Nginx启动脚本
[root@localhost Nginx]# 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 Nginx]# Nginx ##开启ngnix
[root@localhost Nginx]# netstat -ntap | grep 80 ##查看端口,Nginx已经开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39620/Nginx: master
[root@localhost Nginx]# systemctl stop firewalld.service ##关闭防火墙
[root@localhost Nginx]# setenforce 0
6,安装elinks网页测试工具,并进行测试
[root@localhost Nginx]# yum install elinks -y ##安装elinks软件
[root@localhost Nginx]# elinks http://localhost ##测试Nginx网页
7,服务开启重载以及关闭
[root@localhost Nginx]# killall -s QUIT Nginx ##停止 或者使用killall -3 Nginx
[root@localhost Nginx]# killall -s HUP Nginx ##重启 或者使用killall -1 Nginx
[root@localhost Nginx]# Nginx ##开启
8,制作管理脚本,便于使用service管理使用
[root@localhost Nginx]# cd /etc/init.d/ ##切换到启动配置文件目录
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# vim Nginx ##编辑启动脚本文件
#!/bin/bash
# chkconfig: - 99 20 ##注释信息
# description: Nginx Service Control Script
PROG="/usr/local/Nginx/sbin/Nginx" ##设置变量为Nginx命令文件
PIDF="/usr/local/Nginx/logs/Nginx.pid" ##设置变量PID文件 进程号为5346
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 init.d]# chmod +x /etc/init.d/Nginx ##给启动脚本执行权限
[root@localhost init.d]# chkconfig --add Nginx ##添加到service管理器中
[root@localhost init.d]# service Nginx stop ##就可以使用service控制Nginx
[root@localhost init.d]# service Nginx start
Nginx的访问状态统计
一,修改Nginx配置文件
[root@localhost ~]# cd /usr/local/Nginx/conf ##切换到配置文件目录
[root@localhost conf]# vim Nginx.conf ##修改Nginx配置文件
server {
listen 80;
server_name www.kgc.com; ##指明一个域名
charset utf-8; ##中文字符集
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /status { ##添加状态统计
stub_status on;
access_log off;
}
二,安装DNS服务器做域名解析
1,安装bind服务
[root@localhost conf]# yum install bind -y ##安装DNS服务
2,配置主配置文件/etc/named.conf
[root@localhost conf]# vim /etc/named.conf ##主配置文件
options {
listen-on port 53 { any; }; ##将本机监听为所有
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; ##允许所有
3,配置区域配置文件(etc/named.rfc1912.zones)
[root@localhost conf]# vim /etc/named.rfc1912.zones ##配置区域配置文件
zone "localhost" IN { ##复制模板到下面
type master;
file "named.localhost";
allow-update { none; };
};
zone "kgc.com" IN { ##修改localhost为kgc.com
type master;
file "kgc.com.zone"; ##创建区域数据配置文件
allow-update { none; };
};
4,编辑区域数据配置文件( kgc.com.zone)
[root@localhost conf]# cd /var/named
[root@localhost named]# cp -p named.localhost kgc.com.zone
##复制模板为kgc.com.zone
[root@localhost named]# vim kgc.com.zone ##编辑区域数据配置文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.13.128 ##删除ipv6 添加域名解析地址为本机
5,关闭防火墙并开启服务
[root@localhost named]# systemctl start named ##开启dns服务
[root@localhost named]# systemctl stop firewalld.service ##关闭防火墙
[root@localhost named]# setenforce 0 ##关闭增强功能
6,利用win10测试机来测试
基于授权的访问控制
配置步骤与Apache基本一致
一,修改主配置文件
[root@localhost ~]# cd /usr/local/Nginx/conf ##切换到配置文件目录
[root@localhost conf]# vim Nginx.conf ##修改Nginx配置文件
location / {
auth_basic "secret"; ##验证类型
auth_basic_user_file /usr/local/Nginx/passwd.db; ##验证文件路径
root html;
index index.html index.htm;
}
二,安装httpd-tools工具包,设置密码认证文件
[root@localhost conf]# yum install httpd-tools -y ##安装工具包
[root@localhost conf]# htpasswd -c /usr/local/Nginx/passwd.db test ##设置密码认证文件
New password: ##输入密码
Re-type new password: ##确认密码
Adding password for user test
[root@localhost conf]# cat /usr/local/Nginx/passwd.db ##查看密码认证文件
test:$apr1$LqqHZeX3$24E7/HeacTVRzKA7nvSgY/
[root@localhost conf]# service Nginx stop ##关闭服务
[root@localhost conf]# service Nginx start ##开启服务