CentOS7使用yum安装Nginx

CentOS7使用yum安装Nginx

安装说明

  • Nginx是一款轻量级的网页服务器、反向代理服务器。相较于Apache、lighttpd具有占有内存少,稳定性高等优势。
    它最常的用途是提供反向代理服务。
  • 在Centos下,yum源不提供nginx的安装,不能直接 yum install nginx进行安装,但可以通过切换yum源的方法获取安装。也可以通过直接下载安装包的方法
  • 安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库),具体版本号根据实际情况改变。这里其他的库就默认安装,我们选定 /usr/local 为nginx的安装目录

安装gcc

查看是否有gcc

gcc -v

如果是新环境,一般没有gcc,都需要进行安装

yum install -y gcc gcc-c++

如果中间有提示install need space 20M 之类的信息
对于 Is this ok?[y/d/N] 的选择,输入y,即yes的意思。

之后遇到有此类提示都选择yes

安装pcre库

输入命令:

yum install pcre pcre-devel

如果报错:configure: error: You need a C++ compiler for C++ support

回到上一步,安装上gcc就好了

安装库的过程中可能会遇到以下错误:

Error: Multilib version problems found. This often means that the root
cause is something else and multilib version checking is just
pointing out that there is a problem. Eg.:

  1. You have an upgrade for pcre which is missing some dependency that another package requires. Yum is trying to solve this by installing an older version of pcre of the different architecture. If you exclude the bad architecture yum will tell you what the root cause is (which package requires what). You can try redoing the upgrade with --exclude zlib.otherarch … this should give you an error message showing the root cause of the problem.
  2. You have multiple architectures of pcre installed, but yum can only see an upgrade for one of those architectures. If you don’t want/need both architectures anymore then you can remove the one with the missing update and everything will work.
  3. You have duplicate versions of pcre installed already. You can use “yum check” to get yum show these errors. …you can also use --setopt=protected_multilib=false to remove this checking, however this is almost never the correct thing to do as something else is very likely to go wrong (often causing much more problems).

上述常见原因是有两个版本的库。

用以下命令查看pcre库:

rpm -qa | grep pcre

一般结果会是两个库

pcre-8.32-17.el7.x86_64

pcre-8.32-17.el7.i686

用以下命令删除其中一个版本(这里我们删除i686)

yum erase pcre-8.32-17.el7.i686

当我们再次使用rpm命令查看pcre库时,就只剩下x86_64库了

安装zlib库

输入命令:

yum install zlibe zlib-devel

我们在安装的过程中可能会碰到和安装pcre库一样的问题,也是采用一样的解决方法,将命令中的pcre换成zlib就行

安装OpenSSL库

输入命令:

yum install openssl openssl-devel

安装Nginx库

1. 来到/usr/local 文件夹

cd /usr/local/

2. 下载nginx的安装包

wget http://nginx.org/download/nginx-1.8.0.tar.gz

如果系统没有安装wget,执行 yum install wget 命令安装wget,然后再下载nginx的安装包。

这里我装的是nginx的1.8.0版本,大家可以去查nginx的版本然后安装新版,比如nginx-1.12.0,对应修改即可

3. 解压

tar -zxvf nginx-1.8.0.tar.gz

4. 来到nginx-1.8.0 文件夹

cd nginx-1.8.0

5. 配置

./configure

这是我们采用的是默认配置,在 nginx-1.12.0 版本中你就不需要去配置相关东西,默认就可以了。

6. 编译安装

make

make install

7. 查找安装路径

whereis nginx

如果不知道nginx装在哪里了,使用whereis命令进行查找

8. 启动、停止nginx

进入nginx的启动目录

cd /usr/local/nginx/sbin/

下面是四种对nginx的操作:

  1. 启动nginx

./nginx

  1. 关闭nginx。stop是先查出nginx进程id再使用kill命令强制杀掉进程。

./nginx -s stop

  1. 关闭nginx。quit是待nginx进程处理任务完毕再进行停止。

./nginx -s quit

  1. 重新加载nginx

./nginx -s reload

重启时推荐先使用quit/stop关闭,再启动nginx

如果是修改了nginx.conf中的配置信息,想让配置生效。不必关闭nginx,用reload重新加载即可


启动nginx

进入nginx的启动目录下启动nginx

最后一步,在浏览器中输入自己的ip地址(默认80端口),如果看到 Welcome to Nginx 界面,即配置成功。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值