0、更好yum源
1、下载wget
yum install -y wget
2、找到CentOS-Base.repo
cd /etc/yum.repos.d
2.1、备份CentOS-Base.repo
cp CentOS-Base.repo /etc/yum.repos.d/CentOS-Base_bak.repo
2.2、删除CentOS-Base.repo
2.3、下载新的
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2.4、修改名称为CentOS-Base.repo
2.5、我们顺便下载个 epel 主要把很多包没得有,安装这个扩展包就能下载到了
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2.6、清理缓存并生成新的缓存
yum clean all
yum makecache
更新为北京时间:https://blog.csdn.net/weixin_43023004/article/details/88738312
一、下载nginx
#1、nginx包目录:/usr/local/soft/nginx
cd /usr/local/soft/nginx
#2、下载
wget https://nginx.org/download/nginx-1.21.6.tar.gz
#3、解压
tar -zxvf nginx-1.21.6.tar.gz
二、执行
#4、进入到/usr/local/soft/nginx/nginx-1.21.6进行配置
cd /usr/local/soft/nginx/nginx-1.21.6
#5、执行./configure
./configure
报错:
原因:缺失 3.10.0-1127.el7.x86_64 x86_64
sudo yum install epel-release
sudo yum install 3.10.0-1127.el7.x86_64 x86_64
sudo yum -y install gcc gcc-c++ autoconf automake make
再次执行./configure 报错
还是缺失包,解决:
# 1.安装 gcc
yum install gcc‐c++
# 2.nginx的http模块使用pcre来解析正则表达式,需要在linux上安装pcre库
yum install ‐y pcre pcre‐devel
yum -y install pcre-devel
# 3.安装zlib库,nginx使用zlib对http包的内容进行gzip
yum install ‐y zlib zlib‐devel
# 4.安装openssl库,让 nginx 支持 https(即在ssl协议上传输http)
yum install ‐y openssl openssl‐devel
执行./configure还是报错
./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.
yum install -y zlib-devel
执行./configure成功
三、编译
make #执行完./configure之后,敲make回车
make install #确认是否安装成功(可执行可不执行,没有影响)
安装完成后nginx目录:/usr/local/nginx
启动:
cd /usr/local/nginx
#启动
sbin/nginx
# 查看nginx启动状态
ps -ef | grep nginx
#退出nginx
sbin/ngin -s quit
重新加载:
./nginx -s reload
访问;开启对外端口,默认80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
结果:
最后设置开机启动
1、切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service
命令:
cd /lib/systemd/system/
touch nginx.service
chmod 777 nginx.service
2、编辑,增加以下内容:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3、退出并保存,执行开机启动命令:
systemctl enable nginx.service
其它命令:
启动nginx:
systemctl start nginx.service
结束nginx:
systemctl stop nginx.service
重启nginx:
systemctl restart nginx.service
四、nginx.conf配置
vi /usr/local/nginx/nginx.conf
基础配置(路由到对应服务上):
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream nginx_boot {
# 30s内检查心跳发送两次包,未回复就代表该机器宕机,请求分发权重比为1:2
# 这里是启动了两个端口的服务,模拟权重
server localhost:9003 weight=100 max_fails=2 fail_timeout=30s;
server localhost:9004 weight=200 max_fails=2 fail_timeout=30s;
# 这里的IP请配置成你WEB服务所在的机器IP
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
client_max_body_size 100m;
location / {
proxy_pass http://nginx_boot;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css){
root /usr/local/nginx/static_resources;
expires 7d;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}