牛骨文教育服务平台(让学习变的简单)
博文笔记

centos7.2编译安装nginx-1.10.2

创建时间:2016-11-26 投稿人: 浏览次数:3818

  • centos72编译安装nginx-1102
    • 1- 编译环境安装
    • 2- 下载安装文件并解压
    • 3- 用户和目录准备
    • 4- 编译nginx-1102
    • 5- 配置nginx启动脚本
    • 6- 启动nginx


1- 编译环境安装

安装gcc编译环境,安装下载工具wget。

yum -y groupinstall "Development Tools"
yum -y install wget

2- 下载安装文件并解压

从官方网站下载nginx,以及依赖的pcre,openssl和zlib,需要注意版本,不要使用新版的pcre2。

#切换目录
cd /usr/local/src
#下载依赖文件pcre,openssl,zlib
wget  -c   http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.bz2
wget  -c https://www.openssl.org/source/openssl-1.0.2j.tar.gz
wget -c http://zlib.net/zlib-1.2.8.tar.gz
#下载nginx
wget -c http://nginx.org/download/nginx-1.10.2.tar.gz

解压文件。

tar -zxvf nginx-1.10.2.tar.gz
tar -jxvf pcre-8.38.tar.bz2
tar -zxvf zlib-1.2.8.tar.gz
tar -zxvf openssl-1.0.2j.tar.gz

3- 用户和目录准备

#新建系统账号nginx
useradd -r  nginx -s /sbin/nologin -M 
#新建nginx需要的目录
cd /var/tmp/ 
mkdir -p /var/tmp/nginx/{client_body,proxy,fastcgi,uwsgi,scgi}
chown -R nginx /var/tmp/nginx

4- 编译nginx-1.10.2

具体编译参数,需要依据实际情况修改。

cd /usr/local/src/nginx-1.10.2
./configure 
--prefix=/usr/local/nginx         
--sbin-path=/usr/sbin/nginx            
--conf-path=/etc/nginx/nginx.conf             
--error-log-path=/var/log/nginx/error.log     
--http-log-path=/var/log/nginx/access.log     
--pid-path=/var/run/nginx.pid          
--lock-path=/var/lock/nginx.lock 
--user=nginx                          
--group=nginx                         
--with-http_ssl_module                    
--with-http_realip_module 
--with-http_stub_status_module 
--with-http_gzip_static_module    
--with-pcre=../pcre-8.38              
--with-zlib=../zlib-1.2.8    
--with-openssl=../openssl-1.0.2j         
--with-debug        
--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
make
make install

5- 配置nginx启动脚本

vi /etc/init.d/nginx 
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

nginx脚本内容如下,可根据实际情况修改nginx和NGINX_CONF_FILE参数。

#! /bin/bash
#
# 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
# 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/nginx.lock

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
    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

6- 启动nginx

#启动nginx服务
systemctl start nginx.service
#查看端口监听
ss -tlnp|grep :80

参考文章:
[1].https://typecodes.com/web/centos7compilenginx.html
[2].https://typecodes.com/web/nginxserviceoptshell.html
[3].http://nginx.org/en/docs/configure.html

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。