Nginx源码安装

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

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装nginx源码包,您可以按照以下步骤进行操作: 1. 首先,您需要下载nginx源码包。您可以从官方网站上下载最新版本的源码包。您可以在http://nginx.org/download/找到最新的源码下载链接。 2. 下载完成后,解压源码包。您可以使用tar命令来解压文件。例如,如果您的源码包是nginx-1.20.1.tar.gz,您可以使用以下命令进行解压: ``` tar -zxvf nginx-1.20.1.tar.gz ``` 3. 进入解压后的目录: ``` cd nginx-1.20.1 ``` 4. 在源码目录中,执行configure命令进行配置。该命令将根据您的系统环境进行一些必要的配置: ``` ./configure ``` 5. 配置完成后,运行make命令编译源码: ``` make ``` 6. 编译完成后,运行make install命令将nginx安装到系统中: ``` make install ``` 7. 安装完成后,您可以检查是否成功安装nginx。您可以使用id命令来检查是否存在nginx用户: ``` id nginx ``` 以上就是nginx源码安装的一般步骤。请注意,这里只提供了基本的安装步骤,具体的安装过程可能会因系统环境和配置需求而有所不同。建议您在安装前阅读官方文档或参考更详细的安装指南以确保正确安装nginx。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [nginx源码安装](https://blog.csdn.net/weixin_49185464/article/details/127326489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值