一、yum方式安装nginx
yum-utils是一款由CentOS官方开发的Linux包管理工具,它是yum软件包管理器的扩展
我们先安装yum-utils
[root@manager1 ~]# yum install yum-utils
我这里已经安装最新版的yum-utils了
###配置yum仓库
cd /etc/yum.repos.d
vim nginx.repo
输入如下内容
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
这样配置默认是拉取稳定版本的nginx包
####安装nginx
[root@manager1 yum.repos.d]# yum install nginx -y
###查看版本
[root@manager1 yum.repos.d]# nginx -v
nginx version: nginx/1.24.0
###启动nginx
[root@manager1 yum.repos.d]# systemctl start nginx
###开机自启动nginx
[root@manager1 yum.repos.d]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@manager1 yum.repos.d]#
###查看nginx状态
[root@manager1 yum.repos.d]# systemctl status nginx
###停止nginx
systemctl stop nginx
####
nginx -t #检查语法
nginx -s reload #重新加载配置
####
nginx配置文件路径:/etc/nginx/nginx.conf
其他一些文件路径:/etc/nginx
www网页路径:/usr/share/nginx/html
日志路径:/var/log/nginx/
###卸载nginx
yum remove nginx
备注:这样安装适用于红帽企业Linux及其衍生产品,如centos,Oracle linux,Rocky linux,AlmaLinux
官方参考文档
https://nginx.org/en/linux_packages.html
其他说明:
默认情况下yum安装的nginx包含ssl模块
二、源码安装nginx
源码安装nginx
官网下载nginx稳定版本的包
http://nginx.org/en/download.html
上传这个nginx-1.24.0.tar.gz到服务器上/home/download目录下
解压 nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
安装nginx前需要提前安装
yum -y install gcc-c++ #gcc环境
yum -y install openssl openssl-devel #ssl模块库
cd 到nginx目录
cd /home/download/nginx-1.24.0
./configure --prefix=/home/nginx --with-http_ssl_module
make && make instal
提示下面错误,说http重写模块需要pcre库,也就是说编译的时候默认回编译-http_rewrite_module模块进去,如果不用就--without-http_rewrite_module,当然还有很多默认编译的模块,下面最后有说明
./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= option.
安装
yum -y install pcre pcre-devel #安装pcre库
make后如下
make install 如下
编译安装成功后在/home/nginx目录如下
启动的时候会报错
mine.types的路径
启动nginx正常
[root@manager1 conf]# /home/nginx/sbin/nginx
检查语法
[root@manager1 conf]# /home/nginx/sbin/nginx -t
重新加载配置
[root@manager1 conf]# /home/nginx/sbin/nginx -s reload
停止nginx
[root@manager1 conf]# /home/nginx/sbin/nginx -s stop
启动nginx
[root@manager1 conf]# /home/nginx/sbin/nginx
一些编译模块说明
如果没有自定义路径,一般都有默认路径
By default开头说明
有一些功能模块需要自己添加编译上去
https://nginx.org/en/docs/configure.html 编译模块说明
三、docker安装nginx
容器镜像网站
https://hub-stage.docker.com/_/nginx/tags?page=3
下载对应版本的镜像
docker pull nginx:latest
查看下载的镜像
docker images
nginx配置文件先准备好,要不然运行容器挂载的时候就变成创建目录了
创建挂载目录
mkdir -p /home/nginx/conf
mkdir -p /home/nginx/log
mkdir -p /home/nginx/html
生成容器
docker run -p 8080:80 -d nginx:latest
把容器里的nginx.conf文件复制出来
[root@manager1 conf]# docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf
将容器里面的html目录复制出来
[root@manager1 conf]# docker cp nginx:/usr/share/nginx/html /home/nginx/
docker stop nginx #停止nginx
docker rm nginx #删除nginx
重新生成容器,挂载目录
docker run -p 8080:80 --name nginx -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/conf/conf.d:/etc/nginx/conf.d -v /home/nginx/log:/var/log/nginx -v /home/nginx/html:/usr/share/nginx/html -d nginx:latest
-p 8080:80
将容器80端口映射到宿主机8080端口
--name nginx
容器名字为nginx
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
挂载nginx配置文件,这样在宿主机修改nginx.conf配置文件重启后,配置内容也会更新到容器里面
-d
后台运行
nginx:latest
使用的镜像