Nginx源码安装

Nginx源码安装

本实验基于RHEL73.10.0-327.el7.x86_64,尽量使用RHEL7或CentOS7。

安装pcre库

安装 pere 库是为了使 Nginx 支持具备 URI 重写功能的 rewrite 模块,如果不安装 pere 库,则Nginx无法使用该模块。

yum install -y pcre-devel
安装openssl库

防止安装过程中报错。

yum install -y openssl-devel
创建用户及用户组
groupadd -g 958 nginx
useradd  -u 956 -g 958 -c "Nginx User" -s /sbin/nologin nginx
安装编译环境
yum install gcc gcc-c++ make
解压文件
tar zxvf nginx-1.22.0.tar.gz -C /usr/local/src/
drwxr-xr-x. 8 1001 1001 4096 May 24  2022 nginx-1.22.0
检测环境

执行nginx目录下的configure脚本

./configure 
#检查汇总
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
#然后
make
#再
make install
重要目录
[root@nginx nginx]# tree -d -L 1
.
├── client_body_temp 
├── conf #配置文件存放
├── fastcgi_temp
├── html #工作目录,等价于apache /var/www/html
├── logs #日志文件及pid文件
├── proxy_temp
├── sbin #启动脚本存放目录
├── scgi_temp
└── uwsgi_temp
检查
#查看Nginx版本
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
configure arguments:
#启动Nginx
/usr/local/nginx/sbin/nginx
#查看监听端口
netstat -lnutp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      33728/nginx: master
配置服务启动脚本
vim /usr/lib/systemd/system/nginx.service
#Contents
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid #这里的pidFile一定要和配置汇总里的对应。
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /usr/local/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target
#检查
systemctl daemon-reload
#在使用systemctl启动前记得杀死nginx的进程,不然重启会失败。详细可以看错误日志。
systemctl restart nginx.service
netstat -lnutp| grep 80
tcp      0    0 0.0.0.0:80        0.0.0.0:*         LISTEN      70435/nginx: master 

至此Nginx源码安装完成。

查看效果:
#因为我这边虚拟机没有安装界面所以只能在物理机上访问,所以防护墙必须关。
systemctl stop firewalld.service

sed  -i 's/^SELINUX=enforcing/SELINUX=Permissive/' /etc/selinux/config

image-20230803232451633

安装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、付费专栏及课程。

余额充值