服务器必备对外网关,路由,限流,请求分发…。这里就记录安装与简单使用配置过程
环境
Centos7 64位 。能访问外网
下载软件
上传到linux并解压
一般外部文件放到/usr/local/src下
tar -zxvf nginx-1.8.1.tar.gz -C /usr/local/src
编译nginx
进入到解压后的文件夹
cd /usr/local/src/nginx-1.8.1
检查安装环境,并指定将来要安装的路径
./configure --prefix=/usr/local/nginx
如上指定安装在:/usr/local/nginx
注,如果执行该命令出现很多 not found 例如:./configure: error: C compiler cc is not found。说明缺少了nginx编译的依赖库
使用yum安装缺少的包
不缺的就忽略
执行如下命令:
扫描二维码关注公众号,回复:
868379 查看本文章
yum -y install gcc pcre-devel openssl openssl-devel
则缺啥安装啥,有则检查更新,是最新版本则忽略执行。
编译安装
make && make install
执行完成,可以查看到目录结构
[root@izwz9c79fdwp9sb65vpyk3z nginx]# ls -l
total 16
drwxr-xr-x 2 root root 4096 Apr 25 17:49 conf
drwxr-xr-x 2 root root 4096 Apr 25 17:49 html
drwxr-xr-x 2 root root 4096 Apr 25 17:49 logs
drwxr-xr-x 2 root root 4096 Apr 25 17:49 sbin
文件名已经很清晰的说明职能了
启动
进入到安装目录的sbin下,执行
./nginx
测试
- 查看端口是否有nginx进程监听(nginx的默认监听端口是80)
netstat -ntlp | grep 80
显示:
[root@izwz9c79fdwp9sb65vpyk3z sbin]# ./nginx
[root@izwz9c79fdwp9sb65vpyk3z sbin]# netstat -ntlp | grep 80
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN 11265/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 20393/nginx: master
- 或者查看正在执行的进程
ps -aux| grep nginx
a表示所有进程,u表示当前用户下,x表示正在执行的
[root@izwz9c79fdwp9sb65vpyk3z sbin]# ps -aux| grep nginx
root 20393 0.0 0.0 20480 608 ? Ss 21:54 0:00 nginx: master process ./nginx
nobody 20394 0.0 0.0 20924 1068 ? S 21:54 0:00 nginx: worker process
root 20408 0.0 0.0 112648 968 pts/1 R+ 21:55 0:00 grep --color=auto nginx
- 直接外部访问
访问链接:http://服务器ip或映射的域名/
如:http://www.ourbrain.top/
访问结果如下:
即表示成功安装与开启,下一步就可以进行负载均衡和代理等高级配置了
检查配置、关闭、热加载
检查配置
./nginx -t
显示如下即正确
[root@izwz9c79fdwp9sb65vpyk3z sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
关闭
./nginx -s quit
或者
./nginx -s stop
热加载
一般修改配置文件后可以先确定配置是否正确,然后重新加载配置文件而不是先关闭再启动
./nginx -s reload