文章目录
下载安装包
我的系统是CentOS-8.3,是运行在Win系统里的虚拟机环境,本来是想用yum装的,但是yum list nginx老提示我“下载元数据失败”,可以看到我已经换了清华源了,头疼!
我这个网络应该没问题,切换过多种网络了都这样。在虚拟机外面的Win系统访问清华源,下载东西都是飞快,就是在虚拟机里就不行,有没有清楚的大佬救救我!
2021-06-06:(注:该问题已解决,如下)
下载元数据失败的解决办法
现在遇到这种情况的话,那我只能自己去官网上下载tar包了
里面有三种版本,这里我选择了最新的稳定版,因为学习的过程中,万一出岔子,排查问题还要老半天。如果因为版本的问题,浪费掉的时间很可惜。而且公司生产环境也一般都会选择稳定版。
- Mainline version:Nginx官方主力研发版本,理解为开发版
- Stable version:最新的稳定版,建议生产环境使用
- Legacy versions:历史版本的稳定版
解压
接下来开始解压,下载的文件通过共享文件夹放到/opt/nginx,然后解压
# tar -xzvf nginx-1.20.1.tar.gz
# du -sh *
7.1M nginx-1.20.1
1.1M nginx-1.20.1.tar.gz
配置
用Nginx的 ./configure 进行配置,prefix是设置安装目录,可以用 --help查看帮助
# ./configure --help
问题1:C compiler cc is not found
执行命令遇到问题,看起来像是C语言的编译器cc命令没找到。有问题就解决,网上搜下
# ./configure --prefix=/opt/nginx/nginx-1.20.1/
checking for OS
+ Linux 4.18.0-240.22.1.el8_3.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
安装c++
# yum -y install gcc-c++
问题2:the HTTP rewrite module requires the PCRE library.
装完了,继续执行配置命令,又遇到报错,继续解决吧!
# ./configure --prefix=/opt/nginx/nginx-1.20.1/
……中间略过一堆日志输出……
./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.
看报错是缺少PCRE依赖库,查到的也是这样,那就继续装
# yum -y install pcre-devel
问题3:the HTTP gzip module requires the zlib library.
装好了,继续配置,又报错了,可能这台Linux就是Nginx依赖库的荒漠吧!继续继续~
# yum install -y zlib-devel
-----------------一条不起眼的分割线-----------------
继续安装Nginx
# ./configure --prefix=/opt/nginx/nginx-1.20.1/
……省略一些安装日志……
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/opt/nginx/nginx-1.20.1/"
nginx binary file: "/opt/nginx/nginx-1.20.1//sbin/nginx"
nginx modules path: "/opt/nginx/nginx-1.20.1//modules"
nginx configuration prefix: "/opt/nginx/nginx-1.20.1//conf"
nginx configuration file: "/opt/nginx/nginx-1.20.1//conf/nginx.conf"
nginx pid file: "/opt/nginx/nginx-1.20.1//logs/nginx.pid"
nginx error log file: "/opt/nginx/nginx-1.20.1//logs/error.log"
nginx http access log file: "/opt/nginx/nginx-1.20.1//logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
安装
# make && make install
bash: make: 未找到命令...
装一下make
# yum -y install gcc automake autoconf libtool make
继续
# make && make install
问题1:环境变量不生效
先查看版本号
# nginx -v
bash: nginx: 未找到命令...
去nginx的sbin目录下执行
# /opt/nginx/nginx-1.20.1/sbin/nginx -v
nginx version: nginx/1.20.1
这回可以了,应该是环境变量的问题,配置一下,编辑环境变量
# vim /etc/profile
按快捷键 Shift + G 跳到最后,按 i 进入编辑模式,加入
export NGINX_HOME=/opt/nginx/nginx-1.20.1/
export PATH=$PATH:$NGINX_HOME/sbin
按 Esc 退出编辑模式,输入 :wq 保存文件,执行命令使环境变量立即生效
# source /etc/profile
再看下版本,OK,环境变量生效
# nginx -v
nginx version: nginx/1.20.1
启动
检查配置文件是否OK
# nginx -t
nginx: [alert] could not open error log file: open() "/opt/nginx/nginx-1.20.1//logs/error.log" failed (2: No such file or directory)
2021/06/04 07:27:12 [emerg] 4320#0: open() "/opt/nginx/nginx-1.20.1//logs/access.log" failed (2: No such file or directory)
难受呀马飞!继续解决问题吧~
网上的问题大多是 Permission denied ,这个确实是无权限,但我已经是root了,我进到logs目录下发现没有这个目录
# cd /opt/nginx/nginx-1.20.1/logs
bash: cd: /opt/nginx/nginx-1.20.1/logs: 没有那个文件或目录
OK,那我自己建一个
# cd /opt/nginx/nginx-1.20.1/
# mkdir logs
再执行一遍,OK
# nginx -t
nginx: the configuration file /opt/nginx/nginx-1.20.1//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/nginx-1.20.1//conf/nginx.conf test is successful
启动Nginx
# nginx -c /opt/nginx/nginx-1.20.1/conf/nginx.conf
没有任何反应,有点害怕,那我只能自己测下,curl访问默认的80端口
# curl http://127.0.0.1:80/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Welcome to Nginx终于出来了,喜大普奔,我终于可以下班了,宝贵的星期五!
停止Nginx
网上说有好几种,什么从容停止、快速停止,老夫着急下班从来都是一把梭,强制停止安排一下
# pkill -9 nginx
验证一下,再去访问果然访问不到了
# curl http://127.0.0.1:80/
curl: (7) Failed to connect to 127.0.0.1 port 80: 拒绝连接