编译安装nginx
一、nginx是什么?
Nginx是一个http服务器。是一个使用c语言开发的高性能的http服务器及反向代理服务器。Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。
简单来说:nginx是一个web服务器软件,可以提供web服务,可以使用nginx来搭建一个网站
nginx官方文档 http://nginx.org/en/docs/
二、nginx如何编译安装
1.为什么要编译安装
nginx: 企业里会选择编译安装多些
每个公司都有自己的要求,业务是不一样的,开启的功能也不一样
安全:
功能可以定制,路径可以定制
2.编写shell脚本安装
脚本代码如下:
#!/bin/bash
#解决软件的依赖关系,需要安装的软件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make
# zlib 压缩和解压缩相关的库
#openssl 和https相关的库
#pcre 提供正则表达式
#gcc linux下的c语言编译器
#automake make 编译工具
#新建chenzhu用户和组
id chenzhu || useradd chenzhu -s /sbin/nologin
#下载nginx软件
mkdir /nginx2 -p
cd /nginx2
curl -O http://nginx.org/download/nginx-1.20.0.tar.gz
#解压软件
tar xf nginx-1.20.0.tar.gz
#进入解压后的文件夹
cd nginx-1.20.0
#编译前的配置
./configure --prefix=/usr/local/scnginx2 --user=chenzhu --group=chenzhu --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --without-http_geo_module
#编译
make -j 2
#编译安装
make install
#修改PATH变量
echo "PATH=$PATH:/usr/local/scnginx2/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc
#启动nginx
/usr/local/scnginx2/sbin/nginx
#firewalld and selinux
#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld
#临时停止selinux和永久停止selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/sysconfig/selinux
curl 是linux里的字符界面的浏览器
-O 选项,下载下来后存放在当前目录
configure -->帮助我们去生成一个Makefile文件
Makefile文件:编译的时候的一些配置
–prefix=/usr/local/nginx1 指定nginx的安装路径
–user=chenzhu 指定启动nginx的进程的用户
–group=chenzhu 指定启动的组
–with-http_ssl_module 启用构建将HTTPS协议支持添加 到HTTP服务器的模块的功能。默认情况下未构建此模块。需要OpenSSL库来构建和运行此模块。
- ssl是安全相关的技术,提升web服务的安全性
- openssl openssl-devel(openssl的开发包)
–with-threads 启用线程池的使用
–with-http_v2_module 支持构建提供对HTTP / 2支持的模块 。默认情况下未构建此模块。
–with-http_stub_status_module 支持构建ngx_http_stub_status_module 模块,该 模块提供对基本状态信息的访问。默认情况下未构建此模块。 状态统计功能
–with-stream
–with-stream=dynamic
支持构建 用于通用TCP / UDP代理和负载平衡的 流模块。默认情况下未构建此模块。 --》四层负载均衡的支持
–without-http_geo_module
- geo 有一个数据库,将全球的ip地址属于哪个国家和地区,进行了统计
还有很多功能配置可以再nginx官方文档中查询http://nginx.org/en/docs/
开启和关闭nginx
-
开启 使用路径开启服务 比如:
[root@kafka01 ~]# /usr/local/nginx-jcg/sbin/nginx
-
关闭 -s stop 比如:
[root@kafka01 ~]# /usr/local/nginx-jcg/sbin/nginx -s stop
-
等待关闭 -s quit 比如:
[root@kafka01 ~]# /usr/local/nginx-jcg/sbin/nginx -s quit
-
重启 -s reload 比如:
[root@kafka01 ~]# /usr/local/nginx-jcg/sbin/nginx -s reload
查看nginx服务是否启动
- 查看进程 ps -ef(aux) |grep nginx
- 查看端口占用情况 lsof -i:80 (nginx默认占用80端口)
- 查看端口号 netstat -anplut|grep nginx
查看nginx版本相关信息
nginx -V
nginx version: nginx/1.20.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1g FIPS 21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-jcg --user=jcg --group=jcg --with-threads --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-file-aio --with-http_stub_status_module --with-stream
进入nginx文件目录
[root@kafka01 ~]# cd /usr/local/nginx-jcg/
[root@kafka01 nginx-jcg]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@kafka01 nginx-jcg]#
主要文件作用
- conf 存放配置文件
- html 存放网页(网站)
- logs 存放日志
- sbin 存放启动nginx的程序