Linux Centos 7安装Nginx(全纪录+错误解决方案)

Linux Centos 7安装Nginx(全纪录+错误解决方案)

1.安装依赖

Nginx运行环境需要用到一下环境变量依赖,安装前确保环境中有以下依赖:(gcc -c++配置和PCRE库)
PCRE是Nignx的rewrite模块和HTTP核心模块会用到的PCRE正则表达式;
安装:

shell> yum -y install gcc-c++
shell> yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel

或者一次性安装四个依赖:
推荐

shell> yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.下载 nginx,这里 nginx 版本号可以根据需要选择,1.17.7 是当前最新的版本

shell> cd /usr/local/src
shell> wget http://nginx.org/download/nginx-1.17.7.tar.gz

3.解压包

shell> tar -xvzf nginx-1.17.7.tar.gz
shell> rm -f nginx-1.17.7.tar.gz

4. 下载解压 openssl

shell> wget https://www.openssl.org/source/openssl-1.0.2u.tar.gz
shell> tar -xvzf openssl-1.0.2u.tar.gz
shell> rm -f openssl-1.0.2u.tar.gz

5. 进入nginx解压包里

shell> cd nginx-1.17.7

6.预编译执行配置脚本,进行预处理

shell> ./configure

# 成功后显示如下信息
Configuration summary
  + using system PCRE library
  + using OpenSSL library: /usr/local/src/openssl-1.0.2u
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx"
  nginx configuration file: "/usr/local/nginx/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

7.编译并安装

shell> make && make install

8.创建 nginx 软连接,全局使用

shell> ln -sf /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

9.查看是否安装配置成功

shell> /usr/local/nginx/sbin/nginx -v
shell> nginx -v    # 已创建软连接

10.启动 nginx

shell> /usr/local/nginx/sbin/nginx
shell> nginx   # 已创建软连接

11.查看 nginx 是否启动成功(查看进程号)

shell> ps -ef|grep nginx

12.停止命令

shell> nginx -s stop   # 停止 nginx
shell> nginx -s reload # 重载 nginx

13.验证是否成功

验证 Nginx 是否成功启动,可以在浏览器中打开 http://YOUR_IP,您将看到默认的 Nginx 欢迎页面,类似于下图所示:
在这里插入图片描述
注:Nginx 的默认欢迎页有好几种样式,和你安装的版本有关,所以大家只用关注这个页面的大标题就行了 Welcome to nginx!


错误提示1.:./configure: error: the HTTP rewrite module requires the PCRE library在第6步:./configure,预编译发生错误提示。

解决方案1:没有安装HTTP模块依赖,安装pcre-devel解决问题

shell> yum -y install pcre-devel

  • 错误提示2.:…/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= option.在第6步:./configure,预编译发生错误提示。

解决方案2:没有安装zlib-devel模块依赖,安装zlib-devel解决问题

shell> yum install -y zlib-devel

安装结束!

1.YUM安装方法:

## 1,将nginx添加到yum repro库中
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2,查看nginx信息
# yum info nginx
3,使用yum安装ngnix
# yum -y install nginx
4,启动nginx
# systemctl start nginx

2.nginx配置

[root@izbp1b498epn4trb75oykez ~]# vi /etc/nginx/conf.d/default.conf
添加一个新的server,这个配置主要是将监听80端口,将来自dev.jindll.com的请求转发到本地4355端口
server {
    listen        80;
    server_name   dev.jindll.com;
    location / {
        proxy_pass_header Server;

        proxy_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Scheme $scheme;

        proxy_pass http://127.0.0.1:4355;

    }
}

配置完成保存退出,然后重启nginx,使配置生效

# systemctl reload nginx.service
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值