目录
引言
在网站服务器软件中,除了Apache服务之外,还有Nginx服务,其稳定、高效被很多用户认可,下面了解一下简单的Nginx基本的构建、访问控制方式以及虚拟主机的搭建。
一、Nginx服务基础
1、一款高性能、轻量级Web服务软件
(1)稳定性高
(2)系统资源消耗低
(3)对HTTP并发连接的处理能力高:单台物理服务器可支持30000~50000个并发请求
注:NG并发连接能力受2个因素的影响—CUP的个数和本地物理服务器系统的最大文件打开数
企业业务环境三种类型的环境:生产、开发/发布/预生产、测试(生产与开发的比例是2:1,开发环境会有多个)
2、编译安装Nginx服务
(1)关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service
[root@localhost ~]# setenforce 0
(2)安装依赖包
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
(3)编译安装Nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# rz -E #将安装包拖入到里面
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost opt]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \ #指定nginx的安装路径
> --user=nginx \ #指定用户名
> --group=nginx \ #指定组名
> --with-http_stub_status_module #启用模块以支持状态统计
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #创建软链接让系统识别nginx的操作命令
(4)创建运行用户组
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx #创建程序用户,以便准确的控制访问,Nginx服务程序默认以匿名用户运行。
Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限。
(5)检查、启动、重启、停止 nginx服务
[root@localhost nginx-1.12.2]# 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-1.12.2]# nginx #启动
[root@localhost nginx-1.12.2]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23176/nginx: master
[root@localhost nginx-1.12.2]# cat /usr/local/nginx/logs/nginx.pid
23176
[root@localhost nginx-1.12.2]# kill -3 23176 #停止nginx
[root@localhost nginx-1.12.2]# netstat -natp | grep 80
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 19280/sshd: root@pt
tcp 0 52 192.168.32.128:22 192.168.32.1:64439 ESTABLISHED 19280/sshd: root@pt
tcp6 0 0 ::1:6010 :::* LISTEN 19280/sshd: root@pt
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23338/nginx: master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 19280/sshd: root@pt
tcp 0 52 192.168.32.128:22 192.168.32.1:64439 ESTABLISHED 19280/sshd: root@pt
tcp6 0 0 ::1:6010 :::* LISTEN 19280/sshd: root@pt
[root@localhost nginx-1.12.2]# kill -s QUIT 23338 #停止nginx
[root@localhost nginx-1.12.2]# netstat -natp | grep 80
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 19280/sshd: root@pt