Linux安装nginx完整步骤

 

一、安装依赖(安装过的跳过)
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl-devel
1
二、下载nginx稳定版
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
2
3
4
三、解压–编译–安装
解压
tar -zxvf nginx-1.16.1.tar.gz
1
进入解压目录
cd nginx-1.16.1/
1
编译
# 需要使用https执行指令
./configure --with-http_ssl_module
# 不需要使用https执行
./configure
1
2
3
4
编译如果提示./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后重新编译
安装
make && make install
1
补充查看编辑参数

# 查看编译参数
./configure --help | more
1
2
四、开放访问端口80(可自定义)
# 不同centos 系统指令有差别
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
1
2
五、nginx服务的启动操作
启动
cd /usr/local/nginx/sbin
# 默认配置文件启动
./nginx

# 指定配置文件启动
./nginx -c /usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
停止
cd /usr/local/nginx/sbin
# 停止指令
./nginx -s stop
1
2
3
六、开机启动nginx
编辑文件/etc/rc.d/rc.local在后面添加内容
vi /etc/rc.d/rc.local
1
内容

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

七、补充
查看帮助信息
cd /usr/local/nginx/sbin

./ngxin -h
1
2
3
结果


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

./nginx -V
1
2
3
结果


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

./nginx -t
1
2
3
结果


直接使用nginx指令,提示未找到命令
解决办法:

编辑/etc/profile文件vi /etc/profile在末尾处添加
PATH=$PATH:/usr/local/nginx/sbin
export PATH
1
2
执行source /etc/profile指令
配置
一般把配置放在/etc/nginx下面
1. 新建文件夹(用来存放nginx自定义配置文件)
shell mkdir -p /etc/nginx/conf.d
2. 在nginx的配置文件(默认/usr/local/nginx/conf/nginx.conf)中加入include /etc/nginx/conf.d/*.conf;

3. 修改完成记得nginx -s reload