centos7 编译安装 nginx 1.9.2

一笑奈何 提交于 2019-12-01 15:32:10

安装 nginx 依赖库

yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed

下载 nginx 源码

wget -c http://nginx.org/download/nginx-1.9.2.tar.gz
tar -zxvf nginx-1.9.2.tar.gz

下载 pcre/zlib 包

下载pcre的tar包并解压,以便支持Nginx的Rewrite功能

下载zlib的tar包并解压,以便支持Nginx的Gzip压缩功能

wget -c http://git.typecodes.com/libs/php/pcre-8.36.tar.gz && tar -zxf pcre-8.36.tar.gz
wget -c http://git.typecodes.com/libs/nginx/zlib-1.2.8.tar.gz && tar -zxf zlib-1.2.8.tar.gz

将两个文件夹放入 nginx 的文件夹里

目录

mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
mkdir -p /var/run/nginx

==========================================================================

安装 Nginx

切换到 nginx 源码目录,执行 configure 文件

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/run/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre=pcre-8.36 \
--with-zlib=zlib-1.2.8 \
--with-debug \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \
--with-ld-opt="-Wl,-E"

各参数释意

./configure \
--prefix=/usr/local/nginx \                     [Nginx安装目录]
--sbin-path=/usr/local/sbin/nginx \             [Nginx的 nginx sbin 命令路径]
--conf-path=/usr/local/nginx/etc/nginx.conf \   [Nginx的配置文件]
--error-log-path=/var/log/nginx/error.log \     [Nginx的错误日志]
--http-log-path=/var/log/nginx/access.log \     [Nginx的访问日志]
--pid-path=/run/nginx.pid  \                    [Nginx的进程ID]
--lock-path=/var/lock/nginx.lock \
--user=nginx \                                  [Nginx所属用户]
--group=nginx \                                 [Nginx所属用户组]
--with-http_ssl_module \                        [Nginx的ssl模块]
--with-http_spdy_module \                       [Nginx的Google spdy模块 反正我是编译不通过去掉了]
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \               [Nginx的gzip压缩模块]
--with-http_perl_module \
--with-pcre=~/pcre-8.36 \                      [你解压后的pcre的文件包路径]
--with-zlib=~/zlib-1.2.8 \                     [你解压后的zlib的文件包路径]
--with-debug \                                 [允许DEBUG]
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \                                [Nginx1.9.0+特有的stream模块]
--with-ld-opt="-Wl,-E"                         [gcc的编译优化]

编译安装

注意现在已经不需要编译安装 pcre 和 zlib 了,直接指定他们的文件路径,即可将其编译到 nginx 内部模块

一般是不会报错的 只要你把依赖包和组件都安装完整

make && make install

注册为系统服务

#!/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:     /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 命令路径
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/nginx.lock

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
}

# killproc nginx -QUIT
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 3
    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

根据自己的实际安装的配置做一定的修改:

# config:      /usr/local/nginx/conf/nginx.conf 配置文件路径
# pidfile:     /run/nginx.pid pid 路径

nginx="/usr/local/nginx/sbin/nginx"  bin路径

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 配置文件路径

lockfile=/var/lock/nginx.lock 锁路径

保存为 /etc/init.d/nginx 并赋予执行权限即可 chmod a+x /etc/init.d/nginx

便可使用 service nginx start|stop|status|restart 等命令控制 nginx 服务

相关文章:

PHP7.0.0正式版编译安装LAMP/LNMP

nginx 隐藏 index.php 和 开启pathinfo 模式的配置

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!