Nginx在CentOS 7 安装及配置 yum安装和源码安装

本文介绍了如何在CentOS系统中手动编译安装Nginx,包括安装依赖、配置编译选项、执行编译和安装过程,以及解决可能出现的问题。此外,还提供了使用YUM源安装的选项和相关命令,帮助读者了解和掌握Nginx的安装管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装依赖

  1. 安装gcc环境
    • yum install gcc-c++
  2. 安装PCRE库,用于解析正则表达式
    • yum install -y pcre pcre-devel
  3. zlib压缩和解压缩依赖,
    • yum install -y zlib zlib-devel
  4. SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https
    • yum install -y openssl openssl-devel

源码安装

官网下载地址
http://nginx.org/en/download.html

解压

解压,需要注意,解压后得到的是源码,源码需要编译后才能安装

tar -zxvf nginx-1.x.tar.gz
编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错
mkdir /var/temp/nginx -p
在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件
../configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/run/nginx/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module
命令解释
–prefix指定nginx安装目录
–pid-path指向nginx的pid
–lock-path锁定安装文件,防止被恶意篡改或误操作
–error-log错误日志
–http-log-pathhttp日志
–with-http_gzip_static_module启用gzip模块,在线实时压缩输出数据流
–http-client-body-temp-path设定客户端请求的临时目录
–http-proxy-temp-path设定http代理临时目录
–http-fastcgi-temp-path设定fastcgi临时目录
–http-uwsgi-temp-path设定uwsgi临时目录
–http-scgi-temp-path设定scgi临时目录

make编译

make

安装

make install
查找Nginx
whereis nginx

YUM源安装

To set up the yum repository for RHEL/CentOS, create the file named     /etc/yum.repos.d/nginx.repo with the following contents:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
OS换成centos
OSRELEASE 换成对应的版本号

可能遇到的问题

Not using downloaded repomd.xml because it is older than what we have
Renewed Red Hat and updated the system with yum update, the following error:
Not using downloaded repomd.xml because it is older than what we have:

解决方案
yum clean all && yum check-update
清除centos缓存文件就可以了

常用命令

启动服务
systemctl start nginx.service
设置开机自启
systemctl enable nginx.service
检查nginx配置
nginx -t -c /etc/nginx/nginx.conf 
重新加载配置
nginx -s reload -c /etc/nginx/nginx.conf
停止nginx
nginx -s stop -c c /etc/nginx/nginx.conf
检查端口号
netstat -luntp | grep 443
命令
nginx -v 查看版本号
nginx -V 查看编译参数
目录
rpm -ql nginx 查看所有安装的目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值