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
3)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
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
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.
Great post but nginx is in yum so you could skip a lot of the steps and just do ‘yum install nginx’
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.
Thank, it worked for me.
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.
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.
Hi
I am getting some error in step 7, point 4.
I am unable to start NGINX. Can you help me out please
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)?
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
in Amazon Linux : “sudo yum install nginx”
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
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
Really Great.
Really helpful, sometimes the install from repo creates nuisance for me, Using this manual method for baking nginx based AMI came to my rescue
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
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.
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?
Use
> make install
instead of that long line. It’s a broken markdown showing you unnecessary HTML