【Nginx】3. windows&linux下的安装

1.windows下安装

1、下载nginx

网址:http://nginx.org/en/download.html下载稳定版本。以nginx/Windows-1.16.1为例,直接下载 nginx-1.16.1.zip。下载后解压,解压后如下:

img

2、启动nginx

有多种方法启动nginx:

(1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过

(2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe ,回车即可

3、检查nginx是否启动成功

直接在浏览器地址栏输入网址 http://localhost:80 回车,出现 welcome to nginx 页面说明启动成功!

4、配置监听

nginx 的配置文件:conf 下的 nginx.conf 文件,默认配置的 nginx 监听的端口为80,如果80端口被占用可以修改为未被占用的端口即可。

img

修改了配置文件 nginx.conf 后,不需要关闭nginx后重新启动nginx,只需要执行命令 nginx -s reload 即可让改动生效。

5、关闭nginx

如果使用cmd命令窗口启动nginx, 关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx

(1)输入nginx命令 nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)

(2)使用 taskkill /f /t /im nginx.exe,终止进程。/f:强制终止;/t:终止指定的进程和任何由此启动的子进程;/im:指定的进程名称。


2.linux下安装

1、安装依赖包

1、安装 gcc

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,若没有 gcc 环境,则需要安装:

yum install gcc-c++

2、PCRE pcre-devel 安装

PCRE 是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。pcre-devel 是使用 pcre 开发的一个二次开发库,nginx也需要此库。命令:

yum install -y pcre pcre-devel

3、zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

4、OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

nginx 不仅支持 http 协议,还支持 https,所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

2、安装nginx

1、下载安装包

手动下载.tar.gz安装包,地址:https://nginx.org/en/download.html。下载完毕上传到服务器上:/root目录

img

2、解压

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

3、配置启动 nginx

使用默认配置,在nginx根目录下执行

./configure
make
make install

查找安装路径: whereis nginx

img

cd /usr/local/nginx/sbin/
./nginx  启动

检查:在浏览器地址栏输入网址 http://localhost:80 回车,出现welcome to nginx页面说明启动成功!


3.Nginx常用命令

cd /usr/local/nginx/sbin/
./nginx  启动
./nginx -s stop  停止,-s:采用向 Nginx 发送信号的方式
./nginx -s quit  安全退出
./nginx -s reload  重新加载配置文件
ps aux|grep nginx  查看nginx进程
netstat -npa|grep 80|grep nginx  查看nginx监听端口的状态

注意:如何连接不上,检查阿里云安全组是否开放端口,或者服务器防火墙是否开放端口!
相关命令:

# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
# 查看防火墙规则
firewall-cmd --list-all
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;
原文地址:https://www.cnblogs.com/mind18/p/15359142.html