一、Nginx下载

进入http://nginx.org/后下载对应的即可。下载速度还是很快的,本身文件就不大。下载完成直接解压就行。至于windows版本的,直接双击运行nginx.exe文件即可启动。然后访问本机的80端口即可。如果出现访问不了的情况,可以检查一下防火墙,或者以管理员权限运行。window版本使用太简单了,这里一笔带过了。
image

image

二、Linux版安装

2.1 编译安装之前

  1. 将压缩包(nginx-1.21.6.tar.gz)放在任意指定的linux目录下,如‘/root’。

  2. 解压缩文件(解压到当前路径)。

    1
    tar -zxvf nginx-1.21.6.tar.gz

    备注:也可以解压缩到指定目录,比如:

    1
    tar -zxvf nginx-1.21.6.tar.gz -C /usr/local/nginx

    前提是/usr/local/nginx路径要存在,所以可以提前创建相应文件夹,比如在/usr/local目录下创建nginx文件夹。

    1
    mkdir nginx
  3. 然后进入解压后的安装包中,准备编译安装。

    1
    cd /root/nginx-1.21.6

2.2 编译安装

编译安装到指定路径下,此路径不写也是默认安装到这里

1
2
3
./configure --prefix=/usr/local/nginx  
make
make install

如果出现警告或报错

提示

1
2
3
4
checking for OS
Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found

安装gcc,c语言编译器

1
yum install -y gcc

-y 表示不提示直接进行安装

提示

1
2
3
4
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

安装perl库

1
yum install -y pcre pcre-devel

提示

1
2
3
4
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

安装zlib库

1
yum install -y zlib zlib-devel

接下来再执行

1
2
make
make install

2.3 启动Nginx

进入安装好的目录 /usr/local/nginx/sbin,然后执行下面对应的方法即可启动、停止等

1
2
3
4
./nginx 	        # 启动
./nginx -s stop # 快速停止
./nginx -s quit # 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload # 重新加载配置

2.4 关于防火墙

关闭防火墙

1
systemctl stop firewalld.service

禁止防火墙开机启动

1
systemctl disable firewalld.service

ok,这样就不用担心访问不到服务了。当然了,如果你不想关闭防火墙,也可以放行80端口,这样也能访问到nginx。

放行端口

1
2
3
4
5
6
firewall-cmd --zone=public --add-port=80/tcp --permanent
````
重启防火墙

```sh
firewall-cmd --reload

三、Linux版卸载(彻底)

3.1 检查一下Nginx服务是否在运行

1
ps -ef | grep nginx

3.2 停止Nginx服务

1
/usr/sbin/nginx -s stop

查看端口占用情况:

1
netstat -lntp

3.3 查找、删除Nginx相关文件

1
whereis nginx

find查找相关文件

1
find / -name nginx 

依次删除find查找到的所有目录:

1
rm -rf /usr/sbin/nginx

3.4 再使用yum清理

1
yum remove nginx

ok Nginx 卸载完成!