Amazon web services 如何在amazon linux disto上安装nginx 1.9.15

Amazon web services 如何在amazon linux disto上安装nginx 1.9.15,amazon-web-services,nginx,amazon-ec2,yum,amazon-ami,Amazon Web Services,Nginx,Amazon Ec2,Yum,Amazon Ami,我尝试在新的AmazonLinux上安装最新版本的nginx(>=1.9.5),以使用http2。我按照此处描述的说明操作-> 我创建了一个包含以下内容的repo文件/etc/yum.repos.d/nginx.repo: [nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=0 enabled=1 如果我运行yum update和yum install n

我尝试在新的AmazonLinux上安装最新版本的nginx(>=1.9.5),以使用http2。我按照此处描述的说明操作->

我创建了一个包含以下内容的repo文件
/etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
如果我运行
yum update
yum install nginx
我会得到以下结果:

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

它似乎仍然从amzn的主要回购协议中获利。如何安装较新版本的nginx

--编辑-- 我在nginx.repo文件中添加了“priority=10”,现在我可以使用
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构建的。AmazonLinux是RHEL6、RHEL7和Fedora的奇怪混合体。您应该联系Amazon并要求他们为其发行版创建一个合适的nginx19 RPM。

在撰写本文时,AWS yum repo提供的nginx的最新版本是1.8

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

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

例如,基于nginx1.10(我假设您是以普通
ec2用户
身份登录的。任何需要超级用户权限的内容都以
sudo
开头)

然后,您需要一个服务文件,以便启动/停止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=\([^ ]*\).*/\1/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 "$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
将服务文件设置为可执行文件:

sudo chmod 755 /etc/rc.d/init.d/nginx
现在,您可以从以下内容开始:

sudo service nginx start
要在启动时自动加载,请执行以下操作:

sudo chkconfig nginx on

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

如果您使用的是AWS Linux2,则必须从AWS“Extras存储库”安装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

更多细节如下:。

好的,这是真的。但是如何安装1.9.15呢?在命令行中指定完整的包名。我会更新一下答案。安装packpage后,记得用
systemctl start nginx
启动它。使用
systemctl status nginx
检查状态,尝试使用sudo amazon linux extras install nginx1.12从额外存储库进行安装,但现在抛出一个异常文件“/usr/lib/python2.7/site packages/amazon_linux_extras/software_catalog.py”,第92行,在fetch_new_catalog url=catalog_url.format(**yumvars)中KeyError:u'basearch'@czeraz我在尝试执行
systemctl start nginx
时出错。它说,
启动nginx.service失败:任何.service文件都没有提供org.freedesktop.PolicyKit1的名称。有关详细信息,请参阅系统日志和“systemctl status nginx.service”
。原来我必须
sudo systemctl启动nginx
才能解决这个问题!这是最好的答案。对于像elastic beanstalk这样的服务,您可以创建一个自定义AMI,如下所述。您好:我升级到了nginx1.17.1。我还必须修改/usr/lib/systemd/system中的nginx.service以使其执行正确的版本(刚刚制作的一个…):ExecStartPre=/usr/sbin/nginx-t和ExecStart=/usr/sbin/nginx——将两者都更改为/etc/nginx/sbin/nginx。。。
sudo amazon-linux-extras install nginx1.12