搭建LNAMP环境(二)- 源码安装Nginx1.10

上一篇:搭建LNAMP环境(一)- 源码安装MySQL5.6

1.yum安装编译nginx需要的包

yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel

软件包说明:
zlib: 为nginx提供gzip模块,需要zlib库支持
pcre: 为支持地址重写rewrite功能
openssl:为nginx提供ssl功能

2.创建nginx用户组和用户

groupadd nginx
useradd -r -g nginx -s /sbin/nologin -M nginx

3.下载nginx源码包,将源码包放到/usr/local/src/目录下
下载页面:http://nginx.org/en/download.html
这里用的是 nginx-1.10.2.tar.gz
下载地址:http://nginx.org/download/nginx-1.10.2.tar.gz

4.进入src/目录

cd /usr/local/src/

5.解压nginx源码包

tar -zxf nginx-1.10.2.tar.gz

6.进入nginx源码目录,编译安装(配置参数:http://nginx.org/en/linux_packages.html

cd nginx-1.10.2/

./configure \
--prefix=/usr/local/nginx \
--with-pcre \
--with-http_sub_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--user=nginx \
--group=nginx

make && make install

7.修改nginx目录权限

chown -R nginx:nginx /usr/local/nginx

8.将nginx服务脚本加入到init.d/目录

vim /etc/init.d/nginx

9.加入下面内容,保存退出

#!/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:     /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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
    sleep 1
    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
View Code

10.为nginx添加可执行权限

chmod +x /etc/init.d/nginx

11.将nginx加入系统服务

chkconfig --add nginx

12.修改服务的默认启动等级

chkconfig nginx on

13.启动nginx

service nginx start

访问URL,如:http://192.168.8.9/
页面显示正常,则配置成功

Nginx安装完毕!

下一篇:搭建LNAMP环境(三)- 源码安装Apache2.4

posted @ 2016-11-18 10:39  成九  阅读(1306)  评论(0编辑  收藏  举报