nginx介绍及安装
一、nginx介绍
1、作用
-
部署web服务
虚拟主机、URL重写、LNMP平台 -
反向代理
隐藏服务器地址,安全
2、优势
高并发、高性能、资源消耗少
基于epoll的模型设计
二、nginx安装、启动管理
1、nginx安装
1.1 下载nginx安装包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
--2024-05-28 10:33:06-- http://nginx.org/download/nginx-1.20.2.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2600::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1062124 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.20.2.tar.gz’
100%[============================================================================================>] 1,062,124 1.20MB/s in 0.8s
2024-05-28 10:33:07 (1.20 MB/s) - ‘nginx-1.20.2.tar.gz’ saved [1062124/1062124]
1.2 安装依赖
[root@localhost ~]# yum install -y gcc openssl-devel pcre-devel zlib-devel
1.3 编译安装nginx
[root@localhost ~]# tar xf nginx-1.20.2.tar.gz
[root@localhost ~]# cd nginx-1.20.2/
[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.20.2]# make && make install
1.4 nginx相关文件目录
[root@localhost ~]# ls /usr/local/nginx/
conf html logs sbin
sbin: 可执行命令,二进制程序
logs: 日志
html:默认的网页目录
conf: 配置文件,nginx.conf
2、启动管理
1、nginx启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]#
[root@localhost ~]# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3973/nginx: master
[root@localhost ~]#
[root@localhost ~]# ps -elf | grep nginx
1 S root 3973 1 0 80 0 - 5144 sigsus 10:44 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nobody 3974 3973 0 80 0 - 5255 ep_pol 10:44 ? 00:00:00 nginx: worker process
master process: 主进程,负责记录日志、读取配置文件,管理子进程
2、设置开机自启动
[root@localhost ~]# sed -ri '$a \/usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local
[root@localhost ~]# chmod a+x /etc/rc.d/rc.local
3、关闭nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
4、检测配置文件语法
[root@localhost ~]# /usr/local/nginx/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
5、查看版本
[root@localhost ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.20.2
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module
6、加载配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]#
[root@localhost ~]# netstat -tunlp | grep nginx
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 4014/nginx: master