Nginx安装及配置
开源Nginx官网地址(https://nginx.org)
Nginx源码包下载地址(https://nginx.org/en/download.html)
Mainline version 主线版本
Stable version 稳定版本
Legacy versions 陈旧版本
下载Nginx源码文件
curl -O https://nginx.org/download/nginx-1.26.2.tar.gz
源码安装
安装依赖
在安装操作系统的安装软件配置部分,建议选择“Server with GUI”,并选择“Development Tools”和“Compatibility Libraries”两项附加软件。确保gcc、libgcc、gcc-c++等编译器已经正确安装。
在安装Nginx之前,需要安装一些Nginx的依赖程序,Nginx的主要依赖程序有zlib、pcre、openssl三个,其中,zlib用于支持gzip模块,pcre用于支持rewrite模块,openssl用于支持ssl功能,为了简单快捷,推荐通过yum安装zlib、pcre、openssl软件包。
安装方式如下:
yum -y install zlib pcre pcre-devel openssl openssl-devel # 如果安装的操作系统为最小化,那么可以使用yum安装一下软件包组 # 查看软件包组列表 yum grouplist # 下载“Development Tools”和“Compatibility Libraries”两项附加软件 yum -y groupinstall "Development Tools" yum -y groupinstall "Compatibility Libraries"检测编译环境并配置安装规则
解压文件
tar -xf nginx-1.26.2.tar.gz
进入目录
cd nginx-1.26.2
开始检测
./configure
–prefix=/usr/local/nginx
–sbin-path=/usr/local/nginx/sbin/nginx
–conf-path=/usr/local/nginx/conf/nginx.conf
–error-log-path=/usr/local/nginx/logs/error.log
–http-log-path=/usr/local/nginx/logs/access.log
–pid-path=/usr/local/nginx/logs/nginx.pid
–with-http_stub_status_module
–with-http_ssl_module
–with-http_gzip_static_module
–with-pcre# 基本配置,够用 # 以下路径均为默认路径,可自行更改 # 指定程序安装路径 --prefix=/usr/local/nginx # 指定二进制文件路径 --sbin-path=/usr/local/nginx/sbin/nginx # 指定配置文件路径 --conf-path=/usr/local/nginx/conf/nginx.conf # 指定报错日志文件路径 --error-log-path=/usr/local/nginx/logs/error.log # 指定访问日志文件路径 --http-log-path=/usr/local/nginx/logs/access.log # 指定进程号文件路径 --pid-path=/usr/local/nginx/logs/nginx.pid # 安装用来监控Nginx状态的模块 --with-http_stub_status_module # 启用Nginx的gzip压缩 --with-http_gzip_static_module # 设置Niginx启用正则表达式 --with-pcre # 启用Nginx的SSL模块,此模块依赖“--with-openssl”这个选项,通常一起使用 --with-http_ssl_module # 指定OpenSSL源码包的路径,如果编译的时候没有指定“--with-openssl”选项,那么会默认使用系统自带的openssl库 --with-openssl编译安装
make
make install
编译安装完成后,可以使用nginx命令来查看编译安装的配置规则
# -v :显示版本并退出 # -V :显示版本和配置选项然后退出 /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.26.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre编写启动脚本
cat < /etc/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true[Install]
WantedBy=multi-user.target
EOF重新加载systemd管理器配置
systemctl daemon-reload
管理Nginx服务
当我们配置好启动文件之后,就可以通过systemctl来管理nginx服务了。
# 重载nginx服务 systemctl reload nginx # 启动nginx服务 systemctl start nginx # 关闭nginx服务 systemctl stop nginx # 设置nginx服务开机自启动 systemctl enable nginx # 查看nginx服务运行状态 systemctl status nginx # 查看nginx服务是否设置自启动 systemctl is-enabled nginx使用Nginx
建立软链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
查看帮助
nginx -h
查看版本信息
nginx -v
查看版本信息及配置选项
nginx -V
检测配置文件是否有误
nginx -t
……