23

I try to install the latest version of nginx (>= 1.9.5) on a fresh amazon linux to make use of http2. I followed the instructions that are described here -> http://nginx.org/en/linux_packages.html

I created a repo file /etc/yum.repos.d/nginx.repowith this content:

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

If I run yum update and yum install nginx I get this:

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

It seems that it fetches still from the amzn-main repo. How do I install a newer version of nginx?

-- edit -- I added "priority=10" to the nginx.repo file and now I can install 1.9.15 with yum install nginx with this result:

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
Lee Benson
  • 11,185
  • 6
  • 43
  • 57
wiesson
  • 6,544
  • 5
  • 40
  • 68

4 Answers4

64

If you're using AWS Linux2, you have to install nginx from the AWS "Extras Repository". To see a list of the packages available:

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

You'll see a list similar to:

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 ]

Use the amazon-linux-extras install command to install it, like:

sudo amazon-linux-extras install nginx1.12

More details are here: https://aws.amazon.com/amazon-linux-2/faqs/.

Dan Sterrett
  • 1,160
  • 11
  • 12
  • 4
    After the packcage was installed, remember to start it with `systemctl start nginx`. Check the status using `systemctl status nginx` – czerasz Mar 14 '18 at 19:34
  • Trying to install from the extra repository using sudo amazon-linux-extras install nginx1.12 but this, now throws an exception File "/usr/lib/python2.7/site-packages/amazon_linux_extras/software_catalog.py", line 92, in fetch_new_catalog url = CATALOG_URL.format(**yumvars) KeyError: u'basearch' – florin Feb 22 '19 at 08:05
  • 2
    @czerasz I get an error when I try doing `systemctl start nginx`. It says, `Failed to start nginx.service: The name org.freedesktop.PolicyKit1 was not provided by any .service files See system logs and 'systemctl status nginx.service' for details`. Turns out I had to `sudo systemctl start nginx` to fix this! – Qasim Nov 23 '19 at 07:24
19

At the time of writing, the latest version of nginx available from the AWS yum repo is 1.8.

The best thing to do for now is to build any newer version from source.

The AWS Linux AMI already has the necessary build tools.

For example, based on the Nginx 1.10 (I've assumed you're logged in as the regular ec2-user. Anything needing superuser rights is preceded with 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

You'll then want a service file, so that you can start/stop nginx, and load it on boot.

Here's one that matches the above config. Put it in /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

Set the service file to be executable:

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

Now you can start it with:

sudo service nginx start

To load it automatically on boot:

sudo chkconfig nginx on

Finally, don't forget to edit /etc/nginx/nginx.conf to match your requirements and run sudo service nginx reload to refresh the changes.

Lee Benson
  • 11,185
  • 6
  • 43
  • 57
  • 1
    This is the best answer. For services like elastic beanstalk, you can create a custom AMI as outlined here. https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.customenv.html – sakurashinken Oct 02 '18 at 22:09
  • Hi: I upgraded to nginx 1.17.1. I had to also modify nginx.service in /usr/lib/systemd/system to get it to execute the correct version (one just made...): ExecStartPre=/usr/sbin/nginx -t and ExecStart=/usr/sbin/nginx -- change both to /etc/nginx/sbin/nginx... – julius patta Jul 15 '19 at 17:46
1

Note, there is no 1.10 where you're looking. You can see the list here

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

After you yum update use yum search nginx to see the different versions you have and choose a specific one:

yum search nginx

on centos 6 gives

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

I have two versions to choose from, 1.6 and 1.8.

Harry
  • 11,298
  • 1
  • 29
  • 43
1

You're getting error because those nginx RPMs are built for RHEL7, not Amazon Linux. Amazon Linux is a weird hybrid of RHEL6, RHEL7, and Fedora. You should contact Amazon and ask them to create a proper nginx19 RPM specifically built for their distro.

carlwgeorge
  • 627
  • 5
  • 13