前言
事情起因是甲方客户服务器已使用 yum
安装了 nginx
,且有路人乙在用。身为路人丙的我又不想共用 nginx
,作为一个环保的程序员,源码安装必须安排上。
话不多说,这篇文章是速记一些安装配置,以便大家参考。其实nginx
官网[^1]上有各种安装配置的文档,大家可以详细参考。
这里记录我自己用到的系统:CentOS7.9
。
一、安装依赖类库
1、gcc 和 gcc++ 安装
nginx
核心代码是c语言开发的,nginx
很多模块使用c++语言开发的,所以 nginx
源码安装,编译源码需要依赖 c
编译器和c++
编译器。
CentOS 系统最小安装一般都会默认安装了 gcc
和 gcc-c++
,如果安装过程提示找不到编译器,需要选择合适版本进行重新安装。
yum -y install gcc gcc-c++
2、zlib和zlib-devel安装
nginx
对于协议报文的压缩会使用到zlib相关的压缩支持。
yum -y install zlib zlib-devel
3、pcre和pcre-devel安装
PCRE
是一个正则表达式引擎 nginx
的 location
正则匹配模式用的到。
yum -y install gcc pcre pcre-devel
4、openssl和openssl-devel安装
openssl
是基于TLS/SSL
协议安全通信、加解密的类库,nginx
要实现socket
安全通信或者https
通信等用的到。
yum -y install openssl openssl-devel
二、编译安装nginx
1、下载解压
cd /usr/local
#下载安装包
wget https://nginx.org/download/nginx-1.24.0.tar.gz
#解压安装包
tar zxvf nginx-1.24.0.tar.gz
2、编译安装
开放源码的软件安装命令make 与 configure[^2]
编译参数说明:
–prefix= 安装目录
–conf-path= 配置文件目录(nginx.conf)
–error-log-path= 错误日志目录
–pid-path= pid文件(nginx.pid)
–http-log-path= access log路径
–with-http_gzip_static_module 该模块主要负责搜索和发送经过Gzip功能预压缩的数据。如果客户端请求的数据在之前被压缩过,并且客户端浏览器支持Gzip压缩,服务器会就直接返回压缩后的数据。这些数据以“.gz”作为后缀名存储在服务器上。
–with-http_stub_status_module 该模主要负责监控nginx基本的工作状态
–with-http_ssl_module 启用ngx_http_ssl_module 支持https
cd /usr/local/nginx-1.24.0
#编译检查
./configure
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--http-log-path=/usr/local/nginx/logs/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-stream
#编译
make
#编译安装
make install
3、启动与检查
#启动nginx
/usr/local/nginx/sbin/nginx
#查看是否启动
ps -ef | grep nginx
三、设置开机启动
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
#pid文件
PIDFile=/usr/local/nginx/logs/nginx.pid
#启动前检查
ExecStartPre=/usr/local/nginx/sbin/nginx -t
#启动命令
ExecStart=/usr/local/nginx/sbin/nginx
#重启
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#默认
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
开机启动
systemctl enable nginx.service
※参考数据
- [^1]:nginx官网源码安装。https://nginx.org/en/docs/configure.html。
- [^2]:鸟哥的Linux私房菜-源码编译命令。http://cn.linux.vbird.org/linux_basic/0520source_code_and_tarball_1.php