Theodoros Emmanouilidis » Blog Archive Install nginx Τo Amazon Linux AMI – Theodoros Emmanouilidis

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

sudo su

3)Update

1

yum update

yum update

4) Install needed packages

1
2
3

yum install

pcre-devel zlib-devel openssl-devel

yum install

gcc

yum install

make

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

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

6) Extract

1

tar

xzf nginx-1.1.0.tar.gz

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

>

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

/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

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

#!/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

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

/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

/sbin/chkconfig nginx on

Now you can configure your newly installed server.