1.下载源码安装解压,并入解压目录
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar xf nginx-1.24.0.tar.gz
cd nginx-1.24.0.tar.gz
2.安装环境包的依赖
yum install make gcc pcre-devel zlib-devel openssl-devel -y
3.添加configure参数
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
参数详细解释如下: [root@Nginx nginx-1.24.0]# ./configure --help 在安装目录下可查询更多可选参数
--prefix=/usr/local/nginx #安装路径
--user=nginx # 指定nginx运行用户
--group=nginx # 指定nginx运行组
--with-http_ssl_module # 支持https://
--with-http_v2_module # 支持http版本2
--with-http_realip_module # 支持ip透传
--with-http_stub_status_module # 支持状态页面
--with-http_gzip_static_module # 支持压缩
--with-pcre # 支持正则
--with-stream # 支持tcp反向代理
--with-stream_ssl_module # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip
执行结束后如果正常就可以看到Makefile文件
4.编译以及安装
make && make install #可能需要一会儿 根据硬件资源而定
5.创建启动用户、访问测试
cd /usr/local/nginx/sbin/
[root@Nginx sbin]# useradd -s /sbin/nologin -M nginx #创建nginx用户
[root@Nginx sbin]# nginx #启动
[root@Nginx sbin]# ps -ef | grep nginx
root 56861 1 0 11:08 ? 00:00:00 nginx: master process nginx
nginx 56862 56861 0 11:08 ? 00:00:00 nginx: worker process
root 56867 3038 0 11:08 pts/0 00:00:00 grep --color=auto nginx
后续操作:
1.添加nginx到环境变量:
#编辑用户环境变量文件
[root@Nginx sbin]# vim ~/.bash_profile
#添加一行
export PATH=$PATH:/usr/local/nginx/sbin/
#使环境变量生效
[root@Nginx sbin]# source ~/.bash_profile
此后即可在root用户下直接只用nginx启动服务
nginx #启动nginx
nginx -s stop #停止nginx
nginx -s reload #重载nginx配置文件 masterPID不变,workerPID会刷新
nginx -s quit #优雅停止:即等待当前nginx手头任务处理结束再关闭nginx
nginx -s reopen #:重新打开nginx日志文件
2.创建单元文件 使用systemctl管理nginx服务
在编辑前一定要关闭nginx!! nginx -s stop
vim /usr/lib/systemd/system/nginx.service #创建nginx服务管理文件
[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
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 daemon-reload
[root@Nginx ~]# nginx -s quit
[root@Nginx ~]# ps -ef | grep nginx
root 83761 57124 0 17:56 pts/1 00:00:00 vim /usr/local/nginx/conf/nginx.conf
root 93491 3038 0 21:40 pts/0 00:00:00 grep --color=auto nginx
[root@Nginx ~]# systemctl start nginx
[root@Nginx ~]# ps -ef | grep nginx
root 83761 57124 0 17:56 pts/1 00:00:00 vim /usr/local/nginx/conf/nginx.conf
root 93634 1 0 21:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 93635 93634 4 21:41 ? 00:00:00 nginx: worker process
nginx 93636 93634 7 21:41 ? 00:00:00 nginx: worker process
nginx 93637 93634 4 21:41 ? 00:00:00 nginx: worker process
nginx 93638 93634 5 21:41 ? 00:00:00 nginx: worker process
root 93640 3038 0 21:41 pts/0 00:00:00 grep --color=auto nginx
[root@Nginx ~]# systemctl stop nginx
[root@Nginx ~]# ps -ef | grep nginx
root 83761 57124 0 17:56 pts/1 00:00:00 vim /usr/local/nginx/conf/nginx.conf
root 93651 3038 0 21:41 pts/0 00:00:00 grep --color=auto nginx
需要注意的是,systemctl无法关闭nginx本身创建的进程,就是nginx -s 和 systemctl 不建议混用!
3.编译前更改服务器名称、取消DEBUG
(1).更改Server名称
Server的名称可以通过curl -I 查看到
1234@MacBookPro ~ % curl -I www.google.com
HTTP/1.1 200 OK
...
Server: gws
curl: (56) chunk hex-length char not a hex digit: 0xd
1234@MacBookPro ~ % curl -I www.baidu.com
HTTP/1.1 200 OK
...
Server: bfe/1.0.8.18
1234@MacBookPro ~ % curl -I www.xiaomi.com
...
Server: nginx
[root@Nginx ~]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.26.2
...
当我们想隐藏的时候可以更改原本的默认版本信息
[root@Nginx nginx-1.24.0]# vim src/core/nginx.h
#更改即可
12 #define nginx_version 1026002
13 #define NGINX_VERSION "6.6.6"
14 #define NGINX_VER "Mynginx/" NGINX_VERSION
(2).取消DEBUG
DEUBUG取消后nginx 程序的体积会大大减小 (3-4倍)
#在编译前
[root@Nginx nginx-1.24.0]# vim auto/cc/gcc #注释掉即可
(3).其他安装
docker : docker pull nginx:latest
yum安装: yum install nginx -y