当前位置:   article > 正文

安装nginx:手动安装和yum安装_yum 安装 nginx

yum 安装 nginx

本文在centos7.9下分别尝试了yum安装和手动安装,记录一下试验过程。为后来者少踩点坑。

下载

下载地址:链接 。建议下载稳定版本,也就是Stable Version,这里下载的是 nginx-1.24.0

# 我下载在如下文件夹
mkdir/opt/apps
cd /opt/apps
# 用wget下载
wget https://nginx.org/download/nginx-1.24.0.tar.gz
  • 1
  • 2
  • 3
  • 4
  • 5

确认依赖库

需要以下依赖库

  1. gcc

    用以下命令确认是否已安装gcc:

    gcc –-version
    
    • 1

    如果没有安装,用以下命令安装:

    yum install gcc
    
    • 1
  2. PCRE

    确认是否安装:

    rpm -qa pcre
    
    • 1

    如果没有安装,请用如下指令安装:

    yum install -y pcre pcre-devel
    
    • 1
  3. Zlib

    确认:

    rpm -qa zlib
    
    • 1

    安装:

    yum install -y zlib zlib-devel
    
    • 1
  4. OpenSSL

    确认:

    openssl version
    
    • 1

    如果是1.1或以上就可以,如果不是则需要升级

    SSL升级如下:

    # 1.安装对应的依赖库
    sudo yum install -y zlib yum install zlib-devel openssl-devel sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++
    
    # 2. 下载openssl安装文件
    mkdir /tmp/sslbak
    cd /tmp/sslbak
    wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz   --no-check-certificate
    
    # 3. 解压安装openssl,并进入openssl的目录
    tar -zxvf openssl-1.1.1k.tar.gz
    cd openssl-1.1.1k
    
    # 设置输出为中文,可忽略
    export LANG=zh_CN.UTF-8
    export LANGUAGE=zh_CN.UTF-8
    
    # 编译和安装
    ./config --prefix=/usr/local/openssl shared zlib 
    sudo make && make install
    
    # 4. 备份当前openssl
    mv /usr/bin/openssl /usr/bin/openssl.bak
    mv /usr/include/openssl /usr/include/openssl.bak 
    # 5. 配置使用新版本
    ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
    ln -s /usr/local/openssl/include/openssl /usr/include/openssl
    # 6. 更新动态链接库数据并重新加载
    echo /usr/local/openssl/lib >> /etc/ld.so.conf
    ldconfig -v
    # 7. 查看是否升级成功
    openssl version
    # 8. 如果缺少libssl.s0.1.1
    ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
    ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  5. 检查nginx是否已安装

    find / -name "*nginx*"
    
    • 1

继续安装nginx

  1. 首先,我们进入到下载的nginx文件的目录,执行解压命令,然后进入到该文件夹

    cd /opt/apps
    tar -zxvf nginx-1.24.0.tar.gz
    cd nginx-1.24.0/
    
    
    • 1
    • 2
    • 3
    • 4
  2. 进行安装配置,可以参考如下语句

    注意,如果上面升级了openssl,这里需要修改nginx对openssl的默认设置,否则make时会报错

    # 进入如下目录
    cd /opt/apps/nginx-1.24.0/auto/lib/openssl
    vim conf
    
    • 1
    • 2
    • 3

    去掉下图中的/.openssl

    在这里插入图片描述

    回到nginx解压目录,运行如下命令,各参数意义请参考链接

    ./configure --prefix=/opt/nginx  --with-openssl=/usr/local/openssl --with-http_ssl_module --with-http_gzip_static_module
    
    • 1

    注意,此处的—prefix安装路径不能和下载路径是一个路径。

    可选设置,可以通过如下参数设置log位置

    --error-log-path=/var/log/nginx/nginx.log --pid-path=/var/log/nginx/pid
    
    • 1

    如果不设置,log默认路径在安装路径下的logs文件夹下

    在这里插入图片描述

  3. 编译和安装

    make && make install
    
    • 1

启动服务

进入安装目录

/opt/nginx/sbin
  • 1

启动nginx

# 添加一个nginx用户,因为默认用户是nobody,这是为了安全性
useradd nginx -s /sbin/nologin -M
./nginx
  • 1
  • 2
  • 3

开放端口

防火墙需要开启80端口

**# 开启80端口
firewall-cmd --add-port=80/tcp --permanent
# 重启防火墙
firewall-cmd --reload**
  • 1
  • 2
  • 3
  • 4

加入系统菜单

nginx目录无法直接使用,每次还要到/opt/nginx的 sbin 目录,来启动ngin。我们通过如下方式加入系统菜单,这样就可以直接使用nginx命令了:

# 修改profile文件
vim /etc/profile

# 在最后加上如下语句,注意路径,然后wq保存退出
export PATH=$PATH:/opt/nginx/sbin

# 让profile生效
source /etc/pfofile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

测试访问

输入ip进行访问测试,出现Nginx的欢迎界面,说明安装成功。

在这里插入图片描述

设置开机启动

首先需要创建nginx服务


vim /etc/init.d/nginx

# 将下面脚本复制进去保存,注意路径要和安装路径对上
  • 1
  • 2
  • 3
  • 4
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ ""$NETWORKING"" = ""no"" ] && exit 0
 
nginx=""/opt/nginx/sbin/nginx""
prog=$(basename $nginx)
 
NGINX_CONF_FILE=""/opt/nginx/conf/nginx.conf""
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep ""configure arguments:"" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d ""="" -f 2`
           if [ ! -d ""$value"" ]; then
               # echo ""creating"" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $""Starting $prog: ""
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $""Stopping $prog: ""
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $""Reloading $prog: ""
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case ""$1"" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $""Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}""
        exit 2
esac

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124

保存后,进入该目录,执行以下操作

cd /etc/init.d/
# 添加执行权限
chmod +x nginx
# 添加到系统服务
chkconfig --add nginx
# 查看是否添加成功
chkconfig --list nginx
# 添加到开机启动
chkconfig nginx on
# 再次查看
chkconfig --list nginx
# 以下结果说明添加成功
# 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

常用命令

# 启动
nginx
# 关闭
nginx -s stop
# 刷新配置
nginx -s reload
# 配置测试
nginx -t
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

yum安装Nginx

yum安装就比较简单了

yum install nginx
  • 1

在centos7.9中,yum安装默认的版本是Nginx1.20

yum安装文件位置

yum安装的各常用文件位置如下:

  • nginx的配置文件在/etc/nginx/nginx.conf
  • 自定义的配置文件放在/etc/nginx/conf.d
  • 项目文件存放在/usr/share/nginx/html/
  • 日志文件存放在/var/log/nginx/

另外,yum安装的版本,默认首页是centos首页,看不到那个Nginx的欢迎页面。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/882152?site
推荐阅读
相关标签
  

闽ICP备14008679号