Nginx的安装和启停
# 01.Nginx的安装和启停
本文讲解下Windows和Linux环境下,Nginx如何安装。
本文后续主要是在虚拟机VirtualBox下安装Linux(Centos 7.2)来实验,也推荐读者这样做。
# 下载nginx
官网下载页面:http://nginx.org/en/download.html (opens new window),分文Linux和Windows的安装包
# Windows
Windows的安装方式非常简单,解压即可完成安装。建议解压到一个没有中文和空格的目录下。
推荐下载稳定版本,例如:nginx/Windows-1.12.2 (opens new window)
下载后解压到一个自定义路径,压缩包内容如下:

# 查看Nginx版本
打开cmd命令窗口,切换到nginx解压目录下,输入nginx -v
命令
nginx -v
nginx version: nginx/1.12.2
2
# 启动nginx
有很多种方法启动nginx
- 直接双击nginx.exe,双击后会有一个黑色的弹窗一闪而过
- 打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start nginx ,回车即可
# 检查nginx是否启动成功
直接在浏览器地址栏输入网址 http://localhost (opens new window),回车,出现以下页面说明启动成功
# 关闭nginx
如果使用cmd命令窗口启动nginx,关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx
- 使用nginx命令:nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)
- 杀进程,例如Windows可以在命令行里使用taskkill: taskkill /f /t /im nginx.exe
# 重新加载配置文件
有时候修改了Nginx的配置文件,但不想重启,此时可以通过重新加载的方式使配置文件生效:
cmd进入Nginx的目录,然后输入nginx -s reload 或者 nginx.exe -s reload
有时候也叫重启,但并不是真的重启,重新加载期间Nginx还是可以正常处理请求的。
# Linux
可以使用yum或者apt等工具一键安装,也可以使用编译源码来安装,由于一键安装比较简单,且笔者在工作中经常遇到的是内网中不联网的情况下安装,这里着重讲解编译安装的方式。
建议读者启动Linux后,关闭 firewalld 和 selinux
# 编译安装
安装依赖
Nginx是依赖不少组件的:
- 编译安装需要使用C语言
- gzip 模块需要 zlib 库
- rewrite 模块需要 pcre 库
- ssl 模块需要openssl库
模块可以理解为某个功能,例如gzip模块是用来压缩的。笔者这里将Nginx安装到/opt目录下
检查是否安装了gcc
gcc -v
没有则使用yum install gcc gcc-c++
来安装gcc
然后是其他依赖,可以手动下载依赖:
- nginx各版本下载地址 (opens new window)
- zlib各版本下载地址 (opens new window)
- openssl各版本下载地址 (opens new window)
- pcre各版本下载地址-GitHub (opens new window),或者在sourceforge (opens new window)里下载
也可以去我的GitHub (opens new window)或Gitee (opens new window)下载依赖
也可以使用wget下载依赖:
cd /opt/
wget https://nginx.org/download/nginx-1.12.2.tar.gz
wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
wget https://www.openssl.org/source/old/1.1.0/openssl-1.1.0f.tar.gz
2
3
4
解压各个依赖:
tar -zxvf nginx-1.12.2.tar.gz
tar -zxvf pcre-8.41.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
tar -zxvf openssl-1.1.0f.tar.gz
2
3
4
逐个安装依赖:
cd /opt/pcre-8.41
./configure
make && make install
cd /opt/zlib-1.2.11/
./configure
make && make install
cd /opt/openssl-1.1.0f/
./config
make && make install
2
3
4
5
6
7
8
9
10
11
安装Nginx:
cd /opt
mkdir nginx
cd nginx-1.12.2/
./configure --prefix=/opt/nginx --with-openssl=/opt/openssl-1.1.0f --with-http_ssl_module
make && make install
2
3
4
5
启动Nginx
cd /opt/nginx/sbin
./nginx
2
然后可以打开浏览器验证Nginx访问情况
- 可以打开本机IP访问,或者在本地用
curl localhost
访问 - 如果使用云服务器,可以通过外网IP访问
停止和重启的话,和Windows下一致
# 停止Nginx
nginx -s stop:快速停止nginx
nginx -s quit:完整有序的停止nginx
#重启
nginx -s reload
2
3
4
5
6
# yum安装
如果你使用yum,可以这样安装依赖:
yum -y install gcc gcc-c++ libtool make pcre pcre-devel zlib zlib-devel openssl openssl-devel
# apt-get安装
如果你使用ubuntu,想要一键安装,可以这样做:
安装命令:
apt-get install nginx
检查安装是否成功:
nginx -v
启动Nginx
service nginx start
启动后,在网页重输入ip地址,如果看到nginx的欢迎页面,说明启动成功
nginx文件安装完成之后的文件位置:
日志路径:/var/log/nginx
请求日志:/var/log/nginx/access.log
错误日志:/var/log/nginx/error.log
2
3
# 总结
本文讲解了Windows和Linux下Nginx的安装,不同版本的操作系统下安装可能有点不同,若有失败的情况可以通过搜索引擎解决
参考
手把手教你VirtualBox安装Centos,全网最全教程_量化杂货铺的博客-CSDN博客 (opens new window)