2. 在Linux 当中安装 Nginx(13步) 下载&安装&启动(详细说明+附加详细截图说明)
@
目录
- [2. 在Linux 当中安装 Nginx(13步) 下载&安装&启动(详细说明+附加详细截图说明)](#2. 在Linux 当中安装 Nginx(13步) 下载&安装&启动(详细说明+附加详细截图说明))
- [1. 在 Linxu 下安装 Nginx 的详细步骤](#1. 在 Linxu 下安装 Nginx 的详细步骤)
- [2. 最后:](#2. 最后:)
1. 在 Linxu 下安装 Nginx 的详细步骤
Nginx 官方下载地址:https://nginx.org/en/download.html
注意:保证Linux 虚拟机可以访问到外网:
sh
[root@localhost bin]# ping www.baidu.com

具体的安装步骤如下:
- 搭建 gcc 环境
sh
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
特别的:
- 一定要保证当前的 linux 系统, 可以连接外网, 因为 yum 需要到外网,获取数据
- 如 何 在 Linux 配 置 网 络 , 可 以 连 接 到 外 网 , 大家可以移步至:韩老师的教学https://www.bilibili.com/video/BV1Sv411r7vd?p=63
- 执行上面指令的时候, 可能会报
Centos Another app is currently holding the yum lock..
错误, 是因为 yum 不时会自动升级, 占用了端口或文件, 解决方案 :
- 可以重启 Linux, 立即执行该指令
- 或 者等一会再执行
- 或者参考 https://www.cnblogs.com/lzxianren/p/4254059.html
可以执行如下指令:查看 gcc 安装的版本信息:
sh[root@localhost bin]# gcc --version 或者 [root@localhost bin]# gcc -v
安装 gcc 提示一些镜像失败的问题:yum 安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法(2024更新),大家可以移步至:🌟🌟🌟 [Linux 用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法(2024更新)-CSDN博客](Linux 用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法(2024更新)-CSDN博客) 尝试解决,亲测有效。
- 通过 Xftp 将
nginx-1.20.2.tar.gz
上传到Linux /opt
目录当中
一般,在 Linux 当中都是将安装软件放到 opt
目录下


进入到 opt
目录当中查看,是否上传成功。
sh
[root@localhost opt]# cd /opt

- 切换到
/opt
目录 将上传的nginx-1.20.2.tar.gz
文件解压
sh
[root@localhost opt]# tar -zxvf nginx-1.20.2.tar.gz


4. 将解压后的文件放到指定位置/usr/local/nginx
文件目录当中
sh
[root@localhost opt]# mv nginx-1.20.2 /usr/local/nginx

5. 进入到 /usr/local/nginx
文件目录当中
sh
[root@localhost opt]# cd /usr/local/nginx

6. 配置 nginx 路径
注意:一定要进入到 nginx 目录当中去,才行。因为只有在 nginx 目录下,才有这个我们编排的 :configure 文件夹。才能执行如下指令。
sh
./configure
--prefix=/usr/local/nginx
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--with-http_gzip_static_module
--http-client-body-temp-path=/var/temp/nginx/client
--http-proxy-te
mp-path=/var/temp/nginx/proxy
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi
--http-scgi-temp-path=/var/temp/nginx/scgi
--conf-path=/usr/local/nginx/nginx.conf


7. 补全 nginx 配置目录
执行如下命令
sh
mkdir /var/temp/nginx -p

8. 编译并安装
执行如下命令:
sh
make && make install
执行:
shyum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
重新configure,执行下边命令:
sh./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
重新编译,执行下边命令:
shmake && make install
如果还是安装失败的:可以看看这篇文章https://blog.csdn.net/y2020520/article/details/131187585
9. 测试配置与 nginx 是否正常,当出现 successful 即可
sh
[root@localhost nginx]# ./sbin/nginx -t


10. 启动 nginx
sh
./sbin/nginx -c # 启动Nginx
./sbin/nginx -c nginx.conf # 启动 Nginx ,同时指明是通过哪个路径下的 nginx.conf 配置文件进行启动,不写明,则是启动 /usr/local/nginx/conif 默认路径下面的 nginx.conf 进行启动 nginx 的
# 注意:这里我们是在 Nginx 目录下其启动的,所以用的是:./sbin
绝对路径进行启动的方式:
/usr/local/nginx/sbin/nginx -c

启动 Nginx 可能的错误 和解决方案
解决 nginx 启动报错 nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
propertiespid /usr/local/nginx/logs/nginx.pid;
sh[root@localhost conf]# mkdir /usr/local/nginx/log
11. 查看进程/或端口(默认端口是 80)
sh
[root@localhost nginx]# ps -ef | grep nginx
root 2477 1 0 20:07 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 2488 2477 0 20:08 ? 00:00:00 nginx: worker process
root 2519 2417 0 20:10 pts/0 00:00:00 grep --color=auto nginx

注意:养成一个好习惯,每次启动任何服务器,都执行一下
ps -ef | grep xxx
查看该服务器是否启动成功了。
12. 验证Nginx 是否安装成功
nginx 默认监听端口 80,出现 Welcome to nginx!该页面就是搞定了
Linux 的浏览器:http://localhost

13. 配置防火墙,让 Windows 访问 Nginx
说明:默认情况下 Windows 是不能访问 Nginx , 因为Linux虚拟机是当中防火墙是关闭 80 端口的,关闭了,外界是无法访问到 80端口的。所以我们需要将其打开,让外界我们的 windows 可以访问到 80 端口。
查看开放的端口号
sh[root@localhost nginx]# firewall-cmd --list-all
设置开放的端口号
sh#firewall-cmd --add-service=http --permanent #增加了一个 http 服务,理解 firewall-cmd --add-port=80/tcp --permanent
重启防火墙,只有重启了防火墙,你对防火墙的配置才会生效 。
shfirewall-cmd --reload
重启后,再次执行,查看防火墙开放的端口信息:
sh[root@localhost nginx]# firewall-cmd --list-all
测试: windows 浏览器上输入:[Linux ifconfig 查看到的IP地址]+80端口 : http://192.168.12.134/

补充:
查看 Nginx 的版本信息指令:
sh
[root@localhost nginx]# ./sbin/nginx -v

重启 Nginx的指令:该指令也是在 Nginx 安装目录下执行的。
sh
[root@localhost nginx]# ./sbin/nginx -s reload
2. 最后:
"在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。"