一、安装步骤
1.安装 gcc
nginx 源码编译依赖 gcc 环境。
yum install gcc-c++
2.安装 PCRE pcre-devel
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。
yum install -y pcre pcre-devel
3.安装 zlib
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,因此需要安装 zlib 库。
yum install -y zlib zlib-devel
4.安装 OpenSSL
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),因此需要安装 OpenSSL 库。
yum install -y openssl openssl-devel
5.下载Nginx安装包
下载完毕上传到服务器对应目录上
6.解压缩Nginx
tar -zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6
7.Nginx编译安装
./configure --prefix=/usr/local/nginx # prefix 指定安装目录,默认使用 /usr/local/nginx。
make # 编译步骤,从Makefile中读取指令进行编译。
make install #安装步骤,从Makefile中读取指令,安装到指定的位置。
二、安装Nginx时常见的异常或报错
1.没有安装gcc
checking for OS
+ Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
2.没有安装perl库
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
3.没有安装zlib库
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
三、Nginx常用命令
cd /usr/local/nginx/sbin/
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加载配置文件
ps aux|grep nginx 查看nginx进程
四、访问Nginx页面
浏览器打开访问:服务器ip:80
注意:如何连接不上,检查服务器防火墙是否开放端口!
防火墙相关命令:
# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
# 查看防火墙规则
firewall-cmd --list-all
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;