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

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

二、下载nginx稳定版

wget http://nginx.org/download/nginx-1.16.1.tar.gz 

或者去nginx官网下载http://nginx.org/en/download.html

在这里插入图片描述

下载完成后使用scp指令上传到服务器

三、解压–编译–安装

解压

tar -zxvf nginx-1.16.1.tar.gz

进入解压目录

cd nginx-1.16.1/

编译

# 安装到指定目录并配置用户(推荐:需提前建好目录) 
./configure --prefix=/home/admin/local/nginx --user=admin

不推荐直接启动
# 需要使用https执行指令
./configure  --with-http_ssl_module
# 不需要使用https执行
./configure
  • 编译如果提示./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后重新编译 

编译并安装(如没有权限,可使用sudo以root用户权限安装)

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

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

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

五、nginx服务的启动操作

# Check一下配置文件
# Nginx的安装目录下
cd /home/admin/local/nginx
# 检查默认配置文件
./sbin/nginx -t -c ./conf/nginx.conf
# 启动
./sbin/nginx -c ./conf/nginx.conf
# 停止
./sbin/nginx -s stop
# 或
./sbin/nginx -s quit
# 或
kill -9 <Nginx and works PID>

六、开机启动nginx

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

vi /etc/rc.d/rc.local

内容

/home/admin/local/nginx/sbin/nginx

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

七、补充

查看帮助信息

cd /home/admin/local/nginx/sbin

./ngxin -h

 查看安装时配置

cd /home/admin/local/nginx/sbin

./nginx -V

检查配置文件是否正确

cd /usr/local/nginx/sbin

./nginx -t

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

解决办法:

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

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

更多推荐