Theodoros Emmanouilidis

Notes & Thoughts

nginx Installation On Amazon Linux AMI

September11

Since Amazon offers its Amazon Linux AMI for free if used with a micro instance, it is a nice idea to use such a micro instance as a reverse proxy front-end for your actual web server. This setup offers many advantages, with enhanced security and fail over capabilities being two of them.

1)Set up AMI

You have to select an Amazon Linux AMI to run as a micro instance. Store the Key Pair in order to be able to login to the machine and associate an elastic IP to it. Then alter the default security group opening port 80 and 443 to the world and 22 to the IP / IPs you will use to connect via ssh to the machine.

2)Login and become root

By default you can login to the machine via ssh using as user name “ec2-user” and the Key Pair that you downloaded upon AMI creation. In order to continue the installation it is convenient to became root. Type

1
sudo su

3)Update

1
yum update

4) Install needed packages

1
2
3
yum install pcre-devel zlib-devel openssl-devel
yum install gcc
yum install make

5) Download latest stable release of nginx from here

1
wget http://nginx.org/download/nginx-1.1.0.tar.gz

6) Extract

1
tar xzf nginx-1.1.0.tar.gz

7) Install

1
2
3
4
cd nginx-1.1.0
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
make
make install<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;"> </span>

8) Start nginx

1
/usr/local/sbin/nginx

9) Test

Navigate to your AMI’ s IP address and check if nginx responds showing it’ s default web page.

10) Make a start / stop script

1
nano /etc/init.d/nginx

Copy the following code to the newly created file.

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
#!/bin/sh
#
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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="/usr/local/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
lockfile=/var/lock/subsys/nginx
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    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

Make the file executable.

1
chmod 755 /etc/init.d/nginx

Test.

1
2
3
/etc/init.d/nginx stop
/etc/init.d/nginx start
/etc/init.d/nginx reload

Finally, make the service start automatically at every reboot.

1
/sbin/chkconfig nginx on

Now you can configure your newly installed server.

posted under Web Servers
17 Comments to

“nginx Installation On Amazon Linux AMI”

  1. Avatar February 19th, 2013 at 19:45 Brian McQuay Says:

    Great post but nginx is in yum so you could skip a lot of the steps and just do ‘yum install nginx’


  2. Avatar February 27th, 2013 at 11:52 theglassicon Says:

    That’ s true. It was not available when i wrote it.

    It is still useful if you want to install nginx manually.

    Thank you for your comment.


  3. Avatar March 1st, 2013 at 15:25 Dorin Says:

    Thank, it worked for me.


  4. Avatar July 12th, 2013 at 23:37 Nanyaks Says:

    True that nginx is in the yum ‘repositories’ now, it is somewhat dated, version 1.2.9 as of the time of writing while the latest stable from nginx.org is 1.4.1…

    The manual install could come in quite handy.


  5. Avatar September 4th, 2013 at 19:39 ThumbsUp Says:

    Awesome! This was extremely helpful as the amazon yum repository has an ancient version that does not support chunked transfer encoding, while 1.3.9+ does. Thank you.


  6. Avatar April 17th, 2015 at 09:02 Arjun Says:

    Hi
    I am getting some error in step 7, point 4.
    I am unable to start NGINX. Can you help me out please


  7. Avatar May 2nd, 2015 at 05:31 Coulter Says:

    I created an Amazon Linux server. How do I do step #9? I want to test the nginx from a web browser. I tried opening a port on the security group that governs the Amazon Linux server. What should I see when I type in the a URL (http://x.x.x.x where x.x.x.x is the IP address that I use with Putty to connect to the machine)?


  8. Avatar June 3rd, 2015 at 18:41 Anon Says:

    Just a note that for the last step- didn’t work for me, inorder to make nginx autorun, you need to add chkconfig settings to your init.d bash, see:
    https://groups.google.com/forum/#!topic/rhomobile/inh8wk9OlcM


  9. Avatar June 12th, 2015 at 22:16 Nicolai Says:

    in Amazon Linux : “sudo yum install nginx”


  10. Avatar June 15th, 2015 at 04:42 Vishal Thukral Says:

    After executing last line /sbin/chkconfig nginx on
    I m getting this service nginx does not support chkconfig
    I am using nginx 1.8.0


  11. Avatar July 4th, 2015 at 16:43 E.T Says:

    chkconfig will barf when you try to do nginx on. To solve that, just add the following to your /etc/init.d/nginx file (after the /bin/bash line):

    ### BEGIN INIT INFO
    # Provides: nginx
    # Required-Start: $local_fs $remote_fs
    # Required-Stop: $local_fs $remote_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: S 0 1 6
    # Short-Description: nginx initscript
    # Description: nginx
    ### END INIT INFO


  12. Avatar July 27th, 2015 at 10:57 Abdulhafeth Says:

    Really Great.


  13. Avatar December 29th, 2015 at 17:44 Nitin Says:

    Really helpful, sometimes the install from repo creates nuisance for me, Using this manual method for baking nginx based AMI came to my rescue


  14. Avatar May 27th, 2016 at 16:16 Jim Says:

    This in Step 7 isn’t working in OS X Terminal / Bash:

    make install

    bash: syntax error near unexpected token `<'

    Logged in as root@ip-172***ec2-user


  15. Avatar May 27th, 2016 at 17:20 Jim Says:

    Step 8 fails:

    /usr/local/sbin/nginx

    bash: /usr/local/sbin/nginx: No such file or directory

    I’ve run it in the project root and the server root. Nginx is installed in Step 6 in my project root but changing the URI to there isn’t working either.


  16. Avatar August 24th, 2016 at 04:41 CHEER Says:

    This in Step 7 isn’t working in OS X Terminal / Bash:

    make install

    bash: syntax error near unexpected token `<'

    JIM How did you fix it?


  17. Avatar December 20th, 2016 at 05:21 Russ Says:

    Use
    > make install
    instead of that long line. It’s a broken markdown showing you unnecessary HTML


Email will not be published

Website example

Your Comment: