Featured image of post CentOS 7.4 安装Nginx

CentOS 7.4 安装Nginx

通过源码包编译安装

个人比较喜欢用.

使用源码包编译安装会比较好管理,可以按自己的想法放在任何目录下.

下载包

nginx 下载列表地址: http://nginx.org/download/

秉着"版本高的就是好的"的原则, 我下载了 nginx-1.9.9.tar.gz 版本.

服务器 wget 获取包 (提前 CD 到自己放程序的目录下.)

1
wget http://nginx.org/download/nginx-1.9.9.tar.gz

解压

1
tar -zxvf nginx-1.9.9.tar.gz

会解压到当前目录下 nginx-1.9.9 目录

配置 nginx

1
2
cd nginx-1.9.9
./configure --prefix=/usr/local/nginx

这个时候可能会报错.

  1. ./configure: error: the HTTP rewrite module requires the PCRE library.
  2. ./configure: error: the HTTP gzip module requires the zlib library.
问题 1.
  • 原因: 缺少 PCRE 依赖包
  • 解决办法: 安装 pcre-devel 依赖包 yum -y install pcre-devel
问题 2.
  • 原因: 缺少 zlib 依赖包
  • 解决办法: 安装 zlib-devel 依赖包 yum -y install zlib-devel

可能还会出现其它包缺少, 但解决办法一样, 安装缺少的包即可

编译 nginx

nginx-1.9.9 目录下执行

1
make

安装

1
sudo make install

启动

1
sudo nginx -c conf/nginx.conf
  • 这里 nginx 默认的目录是在 nginx 安装目录下. -c 指定的目录会变成这样: /usr/local/nginx/nginx.conf
  • 如果是 nginx -c ../nginx.conf 实际目录是 /usr/local/nginx/../nginx.conf

查看

浏览器输入 localhost 就可以看到

Welcome to nginx! 页面了.

创建软连接

1
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

配置到环境变量中,以在所有的地址都可以直接在 cmd 中使用 nginx 命令

其它命令

1
2
3
4
5
6
7
nginx 启动(默认为 nginx 安装目录下的 conf/nginx.conf 文件, 等同与添加了 -c conf/nginx.conf)

nginx -h 查看帮助
nginx -v 查看版本
nginx -s quit 退出
nginx -s stop 停止
nginx -s reload 重启