nginx 安装

1.检测 gcc 环境

gcc -v

如无则安装

yum install gcc-c++

2.检测 pcre-devel

PCRE 是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式。

nginx也需要此库

yum install -y pcre pcre-devel 

如果报错 Error: Multilib version problems found. This often means that the root

就用这条命令: yum install -y zlib pcre pcre-devel --setopt=protected_multilib=false 跳过重复安装依赖库

3.检测 zlib

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel   --setopt=protected_multilib=false

4.OpenSSL

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

5.下载 nginx 安装包,解压

Nginx 下载地址 http://nginx.org/en/download.html 下载 tar.gz 格式安装包

tar -zxvf nginx-1.8.1.tar.gz -C /data/nginx-1.8.1

6.nginx 包内命令检测安装

进入文件夹 加上---prefix代表安装到某地

./configure  --prefix=/usr/local/nginx

7.编译安装

执行命令 make 编译,执行命令 make install 安装,复制到上面指定的地方。

make && make install

8.检测并启动

// 检测配置是否正确
./sbin/nginx -t
// 启动
./sbin/nginx
// 重新加载配置
./sbin/nginx -s reload
// 关闭
./sbin/nginx  -s stop

9.启动访问默认页面

curl localhost

返回欢迎页面

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

yum 安装

1.修改yum源

1、cd /etc/yum.repos.d/
2、vim nginx.repo
文件内容
=======================
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
=======================

或者安装 EPEL-release 源

rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2.安装nginx

yum clean all
yum makeache
yum -y install nginx

查看安装、配置路径 ,下面两个方法任意一个。

  • which nginx 找到安装目录例如为 /sbin/nginx,执行命令/sbin/nginx -t检测配置文件是否正确会打印路径。
  • locate nginx.conf 直接查询位置。
上次更新时间: 2024/5/7 05:59:02