Red Hat7.4离线安装nginx(非root用户启动)
01、环境介绍
nginx下载地址https://nginx.org
系统版本:redhat 7.4
nginx版本:1.17
参考文章https://www.liangzl.com/get-article-detail-146620.html
02、环境准备
首先配置好yum源,我使用的是系统自带的yum安装的相关依赖包,文件名取的是6.5查实际是7.4的ISO文件镜像。
[root@xcxcc1 yum.repos.d]# ls -l /soft/rhel*
-rw-r--r-- 1 root root 4059037696 11月 21 2018 /soft/rhel-server-7.4-x86_64-dvd.iso
[root@xcxcc1 yum.repos.d]#mount -o loop /sof/rhel-server-7.4-x86_64-dvd.iso /soft/rhel6.5
[root@xcxcc1 yum.repos.d]#
[root@xcxcc1 yum.repos.d]# more /etc/yum.repos.d/rhel6.5.repo
[rhel6.5]
name=rhel6.5 source
baseurl=file:///soft/rhel6.5/
enabled=1
gpgcheck=1
gpgkey=file:///soft/rhel6.5/RPM-GPG-KEY-redhat-release
[root@xcxcc1 yum.repos.d]# yum clean all
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在清理软件源: rhel6.5
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
[root@xcxcc1 yum.repos.d]# more /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
2、需要的依赖包括:gcc、pcre、libstdc++、libstdc+±devel、gcc-c++、zlib,下载地址https://pkgs.org,选择对应的系统版本下载。我当前是通过yum方式安装的,先前已经配置好yum源了,直接通过yum安装gcc gcc-c++
[root@xcxcc1 yum.repos.d]# yum install gcc gcc-c++
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
rhel6.5 | 4.1 kB 00:00:00
(1/2): rhel6.5/group_gz | 137 kB 00:00:00
(2/2): rhel6.5/primary_db | 4.0 MB 00:00:00
软件包 gcc-4.8.5-16.el7.x86_64 已安装并且是最新版本
软件包 gcc-c++-4.8.5-16.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@xcxcc1 yum.repos.d]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
[root@xcxcc1 yum.repos.d]# gcc-c++ --version
bash: gcc-c++: 未找到命令...
[root@xcxcc1 yum.repos.d]# g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
安装其它相关依赖包
[root@xcxcc1 yum.repos.d]# rpm -qa |grep libstdc*
libstdc++-4.8.5-16.el7.x86_64
libstdc++-devel-4.8.5-16.el7.x86_64
[root@xcxcc1 yum.repos.d]# rpm -qa |grep pcre*
pcre2-10.23-2.el7.x86_64
pcre-8.32-17.el7.x86_64
[root@xcxcc1 yum.repos.d]# rpm -qa |grep zlib*
zlib-1.2.7-17.el7.x86_64
[root@xcxcc1 yum.repos.d]# yum install pcre-devel
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 pcre-devel.x86_64.0.8.32-17.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
==================================================================================================================================================================
Package 架构 版本 源 大小
==================================================================================================================================================================
正在安装:
pcre-devel x86_64 8.32-17.el7 rhel6.5 480 k
事务概要
==================================================================================================================================================================
安装 1 软件包
总下载量:480 k
安装大小:1.4 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : pcre-devel-8.32-17.el7.x86_64 1/1
验证中 : pcre-devel-8.32-17.el7.x86_64 1/1
已安装:
pcre-devel.x86_64 0:8.32-17.el7
完毕!
[root@xcxcc1 yum.repos.d]# yum install -y zlib-devel
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 zlib-devel.x86_64.0.1.2.7-17.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
==================================================================================================================================================================
Package 架构 版本 源 大小
==================================================================================================================================================================
正在安装:
zlib-devel x86_64 1.2.7-17.el7 rhel6.5 50 k
事务概要
==================================================================================================================================================================
安装 1 软件包
总下载量:50 k
安装大小:132 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : zlib-devel-1.2.7-17.el7.x86_64 1/1
验证中 : zlib-devel-1.2.7-17.el7.x86_64 1/1
已安装:
zlib-devel.x86_64 0:1.2.7-17.el7
完毕!
[root@xcxcc1 yum.repos.d]# rpm --query --queryformat "%{NAME}-%{VERSION}.%{RELEASE} (%{ARCH})\n" gcc gcc-c++ libstdc++ libstdc++-devel pcre zlib pcre-devel zlib-devel
gcc-4.8.5.16.el7 (x86_64)
gcc-c++-4.8.5.16.el7 (x86_64)
libstdc++-4.8.5.16.el7 (x86_64)
libstdc++-devel-4.8.5.16.el7 (x86_64)
pcre-8.32.17.el7 (x86_64)
zlib-1.2.7.17.el7 (x86_64)
pcre-devel-8.32.17.el7 (x86_64)
zlib-devel-1.2.7.17.el7 (x86_64)
03、安装配置
1、下载nginx包,http://nginx.org/en/download.html
2、通过sftp上传服务器/soft目录
# 将文件改为czsbxcx用户,便于有权限操作。
[root@xcxcc1 local]# chown -R czsbxcx:czsbxcx /soft/nginx-1.17.6.tar.gz
[root@xcxcc1 local]# su - czsbxcx
上一次登录:三 12月 18 13:50:45 CST 2019从 10.10.13.200pts/1 上
# 解压
[czsbxcx@xcxcc1 soft]# tar -xvf nginx-1.17.6.tar.gz
[czsbxcx@xcxcc1 soft]# cd nginx-1.17.6
[czsbxcx@xcxcc1 nginx-1.17.6]$ ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
#检查安装环境,是否符合
[czsbxcx@xcxcc1 nginx-1.17.6]$ ./configure --prefix=/home/czsbxcx/u01/nginx --user=czsbxcx --group=czsbxcx --with-http_ssl_module
......
.......
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... not found
checking for OpenSSL library in /usr/local/ ... not found
checking for OpenSSL library in /usr/pkg/ ... not found
checking for OpenSSL library in /opt/local/ ... not found
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
3、发现报错检查ssl模块失败,检查是否安装openssl,发现少了一个openssl-devel没有安装,通过yum进行安装。
[czsbxcx@xcxcc1 nginx-1.17.6]$ rpm --query --queryformat "%{NAME}-%{VERSION}.%{RELEASE} (%{ARCH})\n" openssl openssl-devel
openssl-1.0.2k.8.el7 (x86_64)
未安装软件包 openssl-devel
[czsbxcx@xcxcc1 nginx-1.17.6]$ yum install -y openssl-deve
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
您需要 root 权限执行此命令。
[czsbxcx@xcxcc1 nginx-1.17.6]$ exit
登出
[root@xcxcc1 local]# rpm --query --queryformat "%{NAME}-%{VERSION}.%{RELEASE} (%{ARCH})\n" openssl openssl-devel
openssl-1.0.2k.8.el7 (x86_64)
未安装软件包 openssl-devel
[root@xcxcc1 local]# yum install -y openssl-devel
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
rhel6.5 | 4.1 kB 00:00:00
(1/2): rhel6.5/group_gz | 137 kB 00:00:00
(2/2): rhel6.5/primary_db | 4.0 MB 00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 openssl-devel.x86_64.1.1.0.2k-8.el7 将被 安装
--> 正在处理依赖关系 krb5-devel(x86-64),它被软件包 1:openssl-devel-1.0.2k-8.el7.x86_64 需要
--> 正在检查事务
---> 软件包 krb5-devel.x86_64.0.1.15.1-8.el7 将被 安装
--> 正在处理依赖关系 libkadm5(x86-64) = 1.15.1-8.el7,它被软件包 krb5-devel-1.15.1-8.el7.x86_64 需要
--> 正在处理依赖关系 keyutils-libs-devel,它被软件包 krb5-devel-1.15.1-8.el7.x86_64 需要
--> 正在处理依赖关系 libcom_err-devel,它被软件包 krb5-devel-1.15.1-8.el7.x86_64 需要
--> 正在处理依赖关系 libselinux-devel,它被软件包 krb5-devel-1.15.1-8.el7.x86_64 需要
--> 正在处理依赖关系 libverto-devel,它被软件包 krb5-devel-1.15.1-8.el7.x86_64 需要
--> 正在检查事务
---> 软件包 keyutils-libs-devel.x86_64.0.1.5.8-3.el7 将被 安装
---> 软件包 libcom_err-devel.x86_64.0.1.42.9-10.el7 将被 安装
---> 软件包 libkadm5.x86_64.0.1.15.1-8.el7 将被 安装
---> 软件包 libselinux-devel.x86_64.0.2.5-11.el7 将被 安装
--> 正在处理依赖关系 libsepol-devel(x86-64) >= 2.5-6,它被软件包 libselinux-devel-2.5-11.el7.x86_64 需要
--> 正在处理依赖关系 pkgconfig(libsepol),它被软件包 libselinux-devel-2.5-11.el7.x86_64 需要
---> 软件包 libverto-devel.x86_64.0.0.2.5-4.el7 将被 安装
--> 正在检查事务
---> 软件包 libsepol-devel.x86_64.0.2.5-6.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
==================================================================================================================================================================
Package 架构 版本 源 大小
==================================================================================================================================================================
正在安装:
openssl-devel x86_64 1:1.0.2k-8.el7 rhel6.5 1.5 M
为依赖而安装:
keyutils-libs-devel x86_64 1.5.8-3.el7 rhel6.5 37 k
krb5-devel x86_64 1.15.1-8.el7 rhel6.5 267 k
libcom_err-devel x86_64 1.42.9-10.el7 rhel6.5 31 k
libkadm5 x86_64 1.15.1-8.el7 rhel6.5 174 k
libselinux-devel x86_64 2.5-11.el7 rhel6.5 186 k
libsepol-devel x86_64 2.5-6.el7 rhel6.5 74 k
libverto-devel x86_64 0.2.5-4.el7 rhel6.5 12 k
事务概要
==================================================================================================================================================================
安装 1 软件包 (+7 依赖软件包)
总下载量:2.3 M
安装大小:4.4 M
Downloading packages:
------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计 19 MB/s | 2.3 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : libkadm5-1.15.1-8.el7.x86_64 1/8
正在安装 : libsepol-devel-2.5-6.el7.x86_64 2/8
正在安装 : libselinux-devel-2.5-11.el7.x86_64 3/8
正在安装 : libcom_err-devel-1.42.9-10.el7.x86_64 4/8
正在安装 : libverto-devel-0.2.5-4.el7.x86_64 5/8
正在安装 : keyutils-libs-devel-1.5.8-3.el7.x86_64 6/8
正在安装 : krb5-devel-1.15.1-8.el7.x86_64 7/8
正在安装 : 1:openssl-devel-1.0.2k-8.el7.x86_64 8/8
验证中 : keyutils-libs-devel-1.5.8-3.el7.x86_64 1/8
验证中 : libverto-devel-0.2.5-4.el7.x86_64 2/8
验证中 : libselinux-devel-2.5-11.el7.x86_64 3/8
验证中 : krb5-devel-1.15.1-8.el7.x86_64 4/8
验证中 : libcom_err-devel-1.42.9-10.el7.x86_64 5/8
验证中 : libsepol-devel-2.5-6.el7.x86_64 6/8
验证中 : 1:openssl-devel-1.0.2k-8.el7.x86_64 7/8
验证中 : libkadm5-1.15.1-8.el7.x86_64 8/8
已安装:
openssl-devel.x86_64 1:1.0.2k-8.el7
作为依赖被安装:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-8.el7 libcom_err-devel.x86_64 0:1.42.9-10.el7 libkadm5.x86_64 0:1.15.1-8.el7
libselinux-devel.x86_64 0:2.5-11.el7 libsepol-devel.x86_64 0:2.5-6.el7 libverto-devel.x86_64 0:0.2.5-4.el7
完毕!
[root@xcxcc1 local]# rpm --query --queryformat "%{NAME}-%{VERSION}.%{RELEASE} (%{ARCH})\n" openssl openssl-devel
openssl-1.0.2k.8.el7 (x86_64)
openssl-devel-1.0.2k.8.el7 (x86_64)
4、重新进行编译检查,通过
[czsbxcx@xcxcc1 nginx-1.17.6]$ ./configure --prefix=/home/czsbxcx/u01/nginx --user=czsbxcx --group=czsbxcx --with-http_ssl_module
5、重新进行安装
# 编译
[czsbxcx@xcxcc1 nginx-1.17.6]$make
#安装
[czsbxcx@xcxcc1 nginx-1.17.6]$ make install
#检查安装结果
[czsbxcx@xcxcc1 nginx]$ cd ~/u01/nginx/
[czsbxcx@xcxcc1 nginx]$ ls
conf html logs sbin
[czsbxcx@xcxcc1 nginx]$
[czsbxcx@xcxcc1 nginx]$ sbin/nginx -t
nginx: the configuration file /home/czsbxcx/u01/nginx/conf/nginx.conf syntax is ok
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
nginx: configuration file /home/czsbxcx/u01/nginx/conf/nginx.conf test failed
6、检查上面有两个错误,一个是bind() to 0.0.0.0:80 failed (13: Permission denied)
和配置文件错误。
1)针对第一个错误Permission denied,是因为Linux只有root用户可以使用1024以下的端口,启动解决办法:
a.用root权限启动
b.将nginx.conf文件中的80端口改为1024以上(我使用第二种办法,修改默认端口为8086)
2)第二个错误怀疑是第一个错误引起的,先修改完端口再重新验证发现正常了。
[czsbxcx@xcxcc1 nginx]$ sbin/nginx -t
nginx: the configuration file /home/czsbxcx/u01/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/czsbxcx/u01/nginx/conf/nginx.conf test is successful
关于./configure 参数详解参考https://www.cnblogs.com/flashfish/p/11025961.html
7、启动nginx
#查看版本
[czsbxcx@xcxcc1 sbin]$ ./nginx -v
nginx version: nginx/1.17.6
#启动nginx
[czsbxcx@xcxcc1 sbin]$ ./nginx
[czsbxcx@xcxcc1 sbin]$ ps -ef |grep nginx
czsbxcx 1756 1 0 16:05 ? 00:00:00 nginx: master process ./nginx
czsbxcx 1779 1756 0 16:05 ? 00:00:00 nginx: worker process
czsbxcx 1849 25594 0 16:05 pts/4 00:00:00 grep --color=auto nginx
[czsbxcx@xcxcc1 sbin]$
浏览器访问如:http://10.10.10.1:8086,能够显示nginx页面即正常。
04、设置开机自启动
新增启动文件:vim /usr/lib/systemd/system/nginx.service,7以前系统版本修改/etc/rc.local文件
#关闭nginx
[czsbxcx@xcxcc1 sbin]$ ./nginx -s quit 或者 ./nginx -s stop
[czsbxcx@xcxcc1 sbin]$exit
#新建服务文件
[root@xcxcc1 system]#cd /usr/lib/systemd/system
[root@xcxcc1 system]# vi more nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
User=czsbxcx
Group=czsbxcx
Type=forking
ExecStart=//home/czsbxcx/u01/nginx/sbin/nginx
ExecReload=/home/czsbxcx/u01/nginx/sbin/nginx -s reload
ExecStop=/home/czsbxcx/u01/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@xcxcc1 system]#
#设置开机自启动
[root@xcxcc1 system]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
#服务启动nginx
[root@xcxcc1 system]# systemctl start nginx
[root@xcxcc1 system]# systemctl status nginx
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2019-12-18 17:00:28 CST; 6s ago
Process: 13472 ExecStart=//home/czsbxcx/u01/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 13475 (nginx)
CGroup: /system.slice/nginx.service
├─13475 nginx: master process //home/czsbxcx/u01/nginx/sbin/nginx
└─13476 nginx: worker process
12月 18 17:00:28 xcxcc1 systemd[1]: Starting nginx...
12月 18 17:00:28 xcxcc1 systemd[1]: Started nginx.
[root@xcxcc1 system]# ps -ef |grep nginx
czsbxcx 13475 1 0 17:00 ? 00:00:00 nginx: master process //home/czsbxcx/u01/nginx/sbin/nginx
czsbxcx 13476 13475 0 17:00 ? 00:00:00 nginx: worker process
root 13508 7962 0 17:00 pts/4 00:00:00 grep --color=auto nginx
[root@xcxcc1 system]#
05、 nginx常用命令
nginx主要有如下4个目录
…conf 配置文件
… html 网页文件
…logs 日志文件
…sbin 主要二进制程序
使用./sbin/nginx 命令启动nginx
停止 Nginx
./sbin/nginx -s stop
./sbin/nginx -s quit
重载reload
./sbin/nginx -s reload
指定配置文件
./sbin/nginx -c /usr/local/nginx/conf/nginx.conf
常用命令
./nginx -h # 显示帮助信息
./nginx -v # 显示nginx的版本号
./nginx -V # 显示nginx的版本号和编译信息
./nginx -t # 检查nginx配置文件的正确性
./nginx -t # 检查nginx配置文件的正确定及配置文件的详细配置内容
./nginx -s # 向主进程发送信号,如:./nginx -s reload 配置文件变化后重新加载配置文件并重启nginx服务
./nginx -p # 设置nginx的安装路径
./nginx -c # 设置nginx配置文件的路径