Nginx is a fast open source web server and reverse proxy server. Nginx recently surpassed Apache as the most popular web server among the top 1000 web sites.
You can yum install Nginx from software repositories with just a few simple commands but on a production server it's better to compile and install from source for a number of reasons.
-
Software repositories are often out of date and might not have the latest greatest version of Nginx.
-
If you want to run an HTTPS server then you'll need to build with the http_ssl_module.
-
At some point you'll most likely need to do some fancy URL routing which requires the pcre module to match routes via regular expressions.
-
You might want to include some 3rd party modules such as Google PageSpeed or the GeoIP module.
-
Building from source usually gives you better optimization for your specific hardware environment.
-
Other security reasons beyond the scope of this tutorial.
For this tutorial I'm assuming you've already launched an AWS EC2 instance using the latest Amazon Linux AMI and can SSH into the instance.
SSH into the instance.
$ ssh ec2-user@ip.address.goes.here
Update the server.
$ sudo yum -y update
Install some necessary packages.
$ sudo yum install -y pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
Let's use /tmp as the working directory for our downloads and installation.
$ cd /tmp
Visit the Nginx web site to find the latest stable version.
http://nginx.org/en/download.html
At the time of this writing it's version 1.8.0. Replace with the current version in the commands below.
Download Nginx.
$ curl -O http://nginx.org/download/nginx-1.8.0.tar.gz
Decompress and cleanup.
$ tar -xvf nginx-1.8.0.tar.gz
$ rm nginx-1.8.0.tar.gz
Configure, compile and install.
$ cd /tmp/nginx-1.8.0/
$ ./configure --with-http_ssl_module --with-pcre --with-http_spdy_module
$ make
$ sudo make install
Create an init script for easier management of Nginx.
$ sudo nano /etc/init.d/nginx
The command above opens the nano text editor with a blank document. Copy/Paste the text below into it.
#!/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: /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/nginx/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
Type ctrl+o
to WriteOut the file to disk. Then crtl+x
to exit the nano text editor.
The init file you created is a shell script, it needs to have executable permissions.
$ sudo chmod +x /etc/init.d/nginx
Configure the server to automatically startup Nginx when the server restarts/reboots.
$ sudo /sbin/chkconfig nginx on
Startup Nginx and make sure it's working.
$ sudo service nginx start
Navigate to the public IP of your AWS instance and you should see...
If you don't see the welcome screen make sure you have port 80 open in the security group that your AWS instance is running in.
Some control commands:
$ sudo service nginx stop
$ sudo service nginx start
$ sudo service nginx restart
$ sudo service nginx reload