centos下Nginx的安装

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:43   133   0

1 安装预装环境(编译环境以及依赖库)

1)gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,需要安装 gcc( 安装 nginx 会用到 make ,而 make 是 用 gcc 进行编译 ) ,一般 gcc 默认被安装的,可输 入命令: gcc –version 查看是否已安装 .

yum install gcc-c++

2)PCRE 安装

PCRE(Perl Compatible Regular Expressions) 是一个 Perl 库,包括 perl 兼容的正 则表达式库。 nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。

yum install -y pcre pcre-devel

3)zlib 安装

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

yum install -y zlib zlib-devel

4)openssl 安装

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

yum install -y openssl openssl-devel

2 安装 nginx

为避免出错,请用 root 权限安装

1 )获取 nginx 源码到本地

wget -c https://nginx.org/download/nginx-1.12.1.tar.gz

2 )解压

tar -zxvf nginx-1.12.1.tar.gz

3 )配置

进入解压后的文件夹(命令: cd nginx-1.12.1 ),然后执行命令: ./configure 此时若少了编译环境以及依赖库都会在报出。如:

但不会一次性提醒你少了哪些库,如上图提醒你少了 PRCE 库,等你安装好了 PRCE 后,再次执 行 ./configure ,可能又会提示你少了另外哪个库,因此为避免出错,一定要确保所有依赖库已经安装好

4 )安装 rtmp 模块

a. 为了增加对 rtmp 的支持,下载 nginx-rtmp-module ,下载地址

b. 解压

c. 重新配置: ./configure –add-module=/usr/local/nginx-rtmp-module-master

一般是 --add-module= 解压后的路径 + 文件名

5 )编译

make

6 )安装

make install

7 )启动 nginx

安装目录 /sbin 目录下的 nginx 就是 nginx 执行文件

whereis nginx (先查看 nginx 安装在哪)

cd /usr/local/nginx (进入安装目录,上条命令会给出安装路径,复制粘贴即可)

cd sbin (进入 /sbin 目录)

./nginx (启动 nginx,失败换root模式 )

8 )验证是否安装成功

在网页输入网址: http://yourIP/index.html (如 123.12.456.789/index.html )

或者你懒得查自己 IP ,将 IP 改为 localhost 也一样,即 http://localhost/index.html

再或者直接输入自己的 IP

若网页显示如下内容则说明安装成功

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.

Commercial support is available at nginx.com.

Thank you for using nginx.

9 )局域网实现

当你在本机成功后你还可能想在其他电脑试下能不能访问,于是在外网电脑输入刚才的网址,发现并不行, 原因是防火墙的问题,于是配置防火墙,开启 80 端口,命令如下

a ) centos6.5 下

vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT (允许 80 端口通过防火墙)

service iptables save

iptables restart

b )在 centos7 以上版本,上述方法不管用,因为 CentOS 7.0 默认使用的是 firewall 作为防火墙,你也 可以改为用 iptables 防火墙,网上有方法,或者直接用 centos7 下的防火墙,命令如下

开启端口

firewall-cmd --zone=public --add-port=80/tcp –permanent ( 开启 80 端口)

firewall-cmd --reload (重启 firewall)

10)补充:

停止 nginx 服务 : ./nginx -s quit

重启 nginx 服务 : ./nginx -s reload


转载于:https://juejin.im/post/5c330d31e51d4551cd71a3c9

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP