注意:

安装 nginx 之前需要安装其他软件, 如果是centos8执行以下命令,需移除gcc+,gcc-c++,会找不到这两个软件

  1. yum install -y pcre pcre-devel openssl openssl-devel gcc gcc+ gcc-c++ wget vim net-tools automake autoconf make

1、安装gcc

  1. yum -y install gcc

2、pcre、pcre-devel安装

pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库。

  1. yum install -y pcre pcre-devel

3、zlib安装

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

  1. yum install -y zlib zlib-devel

4、安装openssl

  1. yum install -y openssl openssl-devel

5、安装wget

  1. yum install -y wget

6、下载nginx

下载完成后默认地址是当前目录

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

7、解压nginx压缩包

  1. # 解压到当前目录
  2. tar -zxvf nginx-1.18.0.tar.gz
  3. # 若想解压到其他目录如下
  4. tar -zxvf nginx-1.18.0.tar.gz -C /usr/share(路径)

8、进入解压目录中执行编译安装

安装完成后的路径:/usr/local/nginx

  1. cd /usr/local/nginx-1.18.0.tar.gz
  2. ./configure
  3. make
  4. make install

9、启动nginx

  1. /usr/local/nginx/sbin/nginx

10、查看是否已经启动(查看进程命令)

  1. ps -ef | grep nginx

若未启动显示如下

  1. root 28306 16077 0 15:47 pts/0 00:00:00 grep --color=auto nginx

若已启动显示如下

  1. root 28303 1 0 15:47 ? 00:00:00 nginx: master process /usr/local/nginx-1.9.9/sbin/nginx
  2. nobody 28304 28303 0 15:47 ? 00:00:00 nginx: worker process
  3. root 28306 16077 0 15:47 pts/0 00:00:00 grep --color=auto nginx

11、启动页面

若显示html页面则代表启动无误

  1. curl http://localhost

12、外部端口访问

nginx启动后,内网访问无误,外网无法访问
1、检查是否已经启动局域网,没有启动则开启局域网
2、配置80端口开放
3、立即生效配置
传送门

13、nginx其他命令

测试配置文件

  1. /usr/local/nginx/sbin/nginx -t

重启服务

  1. /usr/local/nginx/sbin/nginx -s reload

停止命令

  1. /usr/local/nginx/sbin/nginx -s stop

平滑重启

  1. kill -HUP [Ningx主进程号(即ps命令查到的PID)]