注意:
安装 nginx 之前需要安装其他软件, 如果是centos8执行以下命令,需移除gcc+,gcc-c++,会找不到这两个软件
yum install -y pcre pcre-devel openssl openssl-devel gcc gcc+ gcc-c++ wget vim net-tools automake autoconf make
1、安装gcc
yum -y install gcc
2、pcre、pcre-devel安装
pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库。
yum install -y pcre pcre-devel
3、zlib安装
zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
yum install -y zlib zlib-devel
4、安装openssl
yum install -y openssl openssl-devel
5、安装wget
yum install -y wget
6、下载nginx
下载完成后默认地址是当前目录
wget http://nginx.org/download/nginx-1.18.0.tar.gz
7、解压nginx压缩包
# 解压到当前目录
tar -zxvf nginx-1.18.0.tar.gz
# 若想解压到其他目录如下
tar -zxvf nginx-1.18.0.tar.gz -C /usr/share(路径)
8、进入解压目录中执行编译安装
安装完成后的路径:/usr/local/nginx
cd /usr/local/nginx-1.18.0.tar.gz
./configure
make
make install
9、启动nginx
/usr/local/nginx/sbin/nginx
10、查看是否已经启动(查看进程命令)
ps -ef | grep nginx
若未启动显示如下
root 28306 16077 0 15:47 pts/0 00:00:00 grep --color=auto nginx
若已启动显示如下
root 28303 1 0 15:47 ? 00:00:00 nginx: master process /usr/local/nginx-1.9.9/sbin/nginx
nobody 28304 28303 0 15:47 ? 00:00:00 nginx: worker process
root 28306 16077 0 15:47 pts/0 00:00:00 grep --color=auto nginx
11、启动页面
若显示html页面则代表启动无误
curl http://localhost
12、外部端口访问
nginx启动后,内网访问无误,外网无法访问
1、检查是否已经启动局域网,没有启动则开启局域网
2、配置80端口开放
3、立即生效配置
传送门
13、nginx其他命令
测试配置文件
/usr/local/nginx/sbin/nginx -t
重启服务
/usr/local/nginx/sbin/nginx -s reload
停止命令
/usr/local/nginx/sbin/nginx -s stop
平滑重启
kill -HUP [Ningx主进程号(即ps命令查到的PID)]