Install Nginx On Amazon EC2

本文提供了一种解决因 EPEL 软件包存储库中的 bug 导致 Nginx 在 Amazon EC2 实例上运行失败的方法。通过详细步骤指导读者如何在 CentOS 和 RHEL 系统中从源码编译并创建 Nginx 的 RPM 包,最终实现在 Amazon EC2 上部署 Nginx。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This guide can be used in the case we need to run Nginx on Amazon due to a bug that affects the EPEL package repository and maybe some other repository.

That’s why I leave this “Step by Step” to compile and build the RPM for CentOS and RHEL. It is functional in Amazon EC2 instances.


The first thing you need to do is to install the “checkinstall” to generate the RPM.
If you’re going to do it on a 32bit system just do the following:

 

yum install checkinstall

To do that we have to have the RPMforge Repository in our system.


If we have a 64 bits system we are going to need to download the checkinstall 32bits RPM using the following link, and install it:

wget http://packages.sw.be/checkinstall/checkinstall-1.6.0-3.el5.rf.i386.rpm
rpm -Uvh checkinstall-1.6.0-3.el5.rf.i386.rpm

Then install the needed RPM packages to compile Nginx:

yum install rpm-devel rpm-build openssl-devel zlib-devel pcre pcre-devel gcc make

Now we have to download the Nginx code and generate the RPM:

mkdir -p /root/src
cd /root/src
wget http://nginx.org/download/nginx-0.8.54.tar.gz
tar xzf nginx-0.8.54.tar.gz
cd nginx-0.8.54

We are now ready to compile and generate the RPM:

./configure --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/  --prefix=/usr/share/nginx --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid
 
make
 
checkinstall --fstrans=no

We need to follow the steps asked by checkinstall and then install the generated RPM.
Finally we have to copy and paste in a file to add to “init.d” and then be able to run it as any service.
By the end of this “Step by Step” there is an example of how to do it.
As a last step we only have to start it like a normal service.

vim /etc/init.d/nginx  # Paste here the example named "Example to start service"
/etc/init.d/nginx start

I hope this to be usefull for you. If we want to use, for example, PHP 5.2 we can download PHP and Nginx compatible with Amazon Kernel(Xen Kernel) from the CentosALT Repository.

Example to start service.

#!/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="/usr/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
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
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest_q || configtest || return 6
    stop
    start
}
 
reload() {
    configtest_q || configtest || return 6
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    echo
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
configtest_q() {
    configtest >/dev/null 2>&1
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
# Upgrade the binary with no downtime.
upgrade() {
    local pidfile="/var/run/${prog}.pid"
    local oldbin_pidfile="${pidfile}.oldbin"
 
    configtest_q || configtest || return 6
    echo -n $"Staring new master $prog: "
    killproc $nginx -USR2
    retval=$?
    echo 
    sleep 1
    if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
        echo -n $"Graceful shutdown of old $prog: "
        killproc -p ${oldbin_pidfile} -QUIT
        retval=$?
        echo 
        return 0
    else
        echo $"Something bad happened, manual intervention required, maybe restart?"
        return 1
    fi
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    force-reload|upgrade) 
        rh_status_q || exit 7
        upgrade
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status|status_q)
        rh_$1
        ;;
    condrestart|try-restart)
        rh_status_q || exit 7
        restart
	    ;;
    *)
        echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart}"
        exit 2
esac

Source: Netman Blog

Tags: amazon apache centos checkinstall config ec2 EPEL nginx rhel rpmforge Server

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值