如何在亚马逊 linux disto 上安装 nginx 1.9.15

How to install nginx 1.9.15 on amazon linux disto

我尝试在新亚马逊 linux 上安装最新版本的 nginx (>= 1.9.5) 以使用 http2。我按照此处描述的说明进行操作 -> http://nginx.org/en/linux_packages.html

我创建了一个回购文件/etc/yum.repos.d/nginx.repo,内容如下:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

如果我 运行 yum updateyum install nginx 我明白了:

nginx x86_64 1:1.8.1-1.26.amzn1 amzn-main 557 k

它似乎仍然从 amzn-main 仓库中获取。如何安装更新版本的 nginx?

-- 编辑 -- 我将 "priority=10" 添加到 nginx.repo 文件,现在我可以使用 yum install nginx 安装 1.9.15,结果如下:

Loaded plugins: priorities, update-motd, upgrade-helper
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.9.15-1.el7.ngx will be installed
--> Processing Dependency: systemd for package: 1:nginx-1.9.15-1.el7.ngx.x86_64
--> Processing Dependency: libpcre.so.1()(64bit) for package: 1:nginx-1.9.15-1.el7.ngx.x86_64
--> Finished Dependency Resolution
Error: Package: 1:nginx-1.9.15-1.el7.ngx.x86_64 (nginx)
           Requires: libpcre.so.1()(64bit)
Error: Package: 1:nginx-1.9.15-1.el7.ngx.x86_64 (nginx)
           Requires: systemd
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

请注意,您查找的位置没有 1.10。您可以在此处查看列表

http://nginx.org/packages/mainline/centos/7/x86_64/RPMS/

在您yum update使用yum search nginx查看您拥有的不同版本并选择一个特定版本后:

yum search nginx

在 centos 6 上

nginx.x86_64 : A high performance web server and reverse proxy server
nginx16.x86_64 : A high performance web server and reverse proxy server
nginx18.x86_64 : A high performance web server and reverse proxy server

我有两个版本可供选择,1.6 和 1.8。

您收到错误消息是因为这些 nginx RPM 是为 RHEL7 而不是 Amazon Linux 构建的。 Amazon Linux 是 RHEL6、RHEL7 和 Fedora 的奇怪混合体。您应该联系亚马逊并要求他们创建专门为其发行版构建的合适的 nginx19 RPM。

在撰写本文时,AWS yum 存储库提供的最新版本的 nginx 是 1.8。

目前最好的办法是从源代码构建任何更新的版本。

AWS Linux AMI 已经有了必要的构建工具。

例如,基于 Nginx 1.10(我假设您以常规 ec2-user 身份登录。任何需要超级用户权限的内容都以 sudo 开头)

cd /tmp #so we can clean-up easily
wget http://nginx.org/download/nginx-1.10.0.tar.gz
tar zxvf nginx-1.10.0.tar.gz && rm -f nginx-1.10.0.tar.gz
cd nginx-1.10.0
sudo yum install pcre-devel openssl-devel #required libs, not installed by default
./configure \
  --prefix=/etc/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --pid-path=/var/run/nginx.pid \
  --lock-path=/var/run/nginx.lock \
  --with-http_ssl_module \
  --with-http_v2_module \
  --user=nginx \
  --group=nginx
make
sudo make install
sudo groupadd nginx
sudo useradd -M -G nginx nginx
rm -rf nginx-1.10.0

然后你需要一个服务文件,这样你就可以 start/stop nginx,并在启动时加载它。

这是一个与上述配置相匹配的配置。放入 /etc/rc.d/init.d/nginx:

#!/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
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# 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="/etc/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/run/nginx.lock

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*//g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   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 "" in
    start)
        rh_status_q && exit 0
        
        ;;
    stop)
        rh_status_q || exit 0
        
        ;;
    restart|configtest)
        
        ;;
    reload)
        rh_status_q || exit 7
        
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: [=11=] {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

将服务文件设置为可执行:

sudo chmod 755 /etc/rc.d/init.d/nginx

现在您可以开始:

sudo service nginx start

开机自动加载:

sudo chkconfig nginx on

最后,不要忘记编辑 /etc/nginx/nginx.conf 以符合您的要求,并 运行 sudo service nginx reload 刷新更改。

如果您使用的是 AWS Linux2,则必须从 AWS "Extras Repository" 安装 nginx。要查看可用包的列表:

# View list of packages to install
amazon-linux-extras list

您会看到类似于以下内容的列表:

0  ansible2   disabled  [ =2.4.2 ]
1  emacs   disabled  [ =25.3 ]
2  memcached1.5   disabled  [ =1.5.1 ]
3  nginx1.12   disabled  [ =1.12.2 ]
4  postgresql9.6   disabled  [ =9.6.6 ]
5  python3   disabled  [ =3.6.2 ]
6  redis4.0   disabled  [ =4.0.5 ]
7  R3.4   disabled  [ =3.4.3 ]
8  rust1   disabled  [ =1.22.1 ]
9  vim   disabled  [ =8.0 ]
10  golang1.9   disabled  [ =1.9.2 ]
11  ruby2.4   disabled  [ =2.4.2 ]
12  nano   disabled  [ =2.9.1 ]
13  php7.2   disabled  [ =7.2.0 ]
14  lamp-mariadb10.2-php7.2   disabled  [ =10.2.10_7.2.0 ]

使用amazon-linux-extras install命令安装,如:

sudo amazon-linux-extras install nginx1.12

这里有更多详细信息:https://aws.amazon.com/amazon-linux-2/faqs/