一、安装依赖(安装过的跳过)

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl-devel make

二、下载nginx稳定版

  1. 方法一:在目标linux服务上执行
wget http://nginx.org/download/nginx-1.16.1.tar.gz 
  1. 方法二:在本机下载压缩包,上传到目标服务器

访问nginx官网下载

在这里插入图片描述
下载完成后使用scp指令上传到目标服务器

scp 源文件地址 目标文件地址
# 示例
scp nginx-1.16.1.tar.gz root@120.xxx.xxx.232:/opt
# 上述示例,用户进入(cd)nginx下载存放到目录之后再操作

三、解压–编译–安装

  1. 解压
tar -zxvf nginx-1.16.1.tar.gz
  1. 进入解压目录
cd nginx-1.16.1/
  1. 编译
# 需要使用https执行指令
./configure  --with-http_ssl_module
# 需要使用stream
./configure --with-stream
# 需要https、stream指令 
./configure  --with-http_ssl_module --with-stream
# 不需要使用https执行
./configure 

!!!强力推荐使用指令!!!—>>>./configure --with-http_ssl_module --with-stream

  • 编译如果提示./configure: error: the HTTP rewrite module requires the PCRE library.,则执行yum -y install pcre-devel后重新编译
  • 提示./configure: error: the HTTP gzip module requires the zlib library.,则执行yum install -y zlib-devel后重新编译
  1. 安装
make && make install

补充查看编辑参数

# 查看编译参数
./configure --help | more 

四、开放访问端口80(可自定义)

# 不同centos 系统指令有差别
/sbin/iptables -I INPUT  -p tcp --dport 80 -j ACCEPT

五、nginx服务的启动操作

启动
cd /usr/local/nginx/sbin
# 默认配置文件启动
./nginx

# 指定配置文件启动
./nginx -c  /usr/local/nginx/conf/nginx.conf
停止
cd /usr/local/nginx/sbin
# 停止指令
./nginx -s stop

六、开机启动nginx

编辑文件/etc/rc.d/rc.local在后面添加内容
vi /etc/rc.d/rc.local

内容

/usr/local/nginx/sbin/nginx

ll查看下rc.local文件,如果不是绿色表示没有执行权限,则执行指令chmod +x /etc/rc.d/rc.local

七、补充

查看帮助信息
cd /usr/local/nginx/sbin

./ngxin -h

结果
在这里插入图片描述

查看安装时配置
cd /usr/local/nginx/sbin

./nginx -V

结果
在这里插入图片描述

检查配置文件是否正确
cd /usr/local/nginx/sbin

./nginx -t

结果
在这里插入图片描述

直接使用nginx指令,提示未找到命令

解决办法:

  1. 编辑/etc/profile文件vi /etc/profile在末尾处添加
PATH=$PATH:/usr/local/nginx/sbin
export PATH
  1. 执行source /etc/profile指令
配置

一般把配置放在/etc/nginx下面
1. 新建文件夹(用来存放nginx自定义配置文件)
mkdir -p /etc/nginx/conf.d
3. 在nginx的配置文件(默认/usr/local/nginx/conf/nginx.conf)中加入include /etc/nginx/conf.d/*.conf;
在这里插入图片描述
4. 修改完成记得nginx -s reload

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐