centos6.9编译安装nginx1.12.2

切换到root用户
sudo -i

创建站点目录

mkdir -p /www/wwwroot

进入下载文件的目录

cd /usr/local/src

下载文件

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.20.tar.gz
wget http://cn2.php.net/distributions/php-7.2.0.tar.gz
wget https://nginx.org/download/nginx-1.12.2.tar.gz

解压压缩文件

tar zxvf nginx-1.12.2.tar.gz 

安装相关依赖库(非常重要)

yum -y install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel openssl-perl openssl-static perl-ExtUtils-Embed libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data

创建Nginx用户和组

# 创建一个组
groupadd www

# 创建一个用户,不允许登陆和不创主目录 
useradd -s /sbin/nologin -g www -M www

# 更改目录权限
chown www:www /www/wwwroot
chmod -R 755 /www/wwwroot

查看创建的账号

cat /etc/passwd

查看创建的目录

ls /www

去到nginx解压的目录进行编译

cd /usr/local/src/nginx-1.12.2

接下来执行预编译脚本,如下:

# 需要编译的功能模块如下

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-select_module \
--with-poll_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_perl_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_auth_request_module \
--with-http_degradation_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-pcre \
--with-pcre-jit \
--with-mail \
--with-mail_ssl_module \
--with-threads \
--with-compat

再次执行

make && make install

查看安装目录文件

ls /usr/local/nginx

如下:

conf  html  logs  sbin

查看是否安装完成

/usr/local/nginx/sbin/nginx -t

如下:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

添加启动脚本

vim /etc/init.d/nginx

脚本内容如下:

#!/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/usr/local/nginx/logs/${prog}.pid"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f $sysconfig ] && . $sysconfig
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 -p $pidfile $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest_q || return 6
    stop
    start
}
reload() {
    configtest_q || return 6
    echo -n $"Reloading $prog: "
    killproc -p $pidfile $prog -HUP
    echo
}
configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
    $nginx -t -q -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
# Upgrade the binary with no downtime.
upgrade() {
    local oldbin_pidfile="${pidfile}.oldbin"
    configtest_q || return 6
    echo -n $"Upgrading $prog: "
    killproc -p $pidfile $prog -USR2
    retval=$?
    sleep 1
    if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
        killproc -p $oldbin_pidfile $prog -QUIT
        success $"$prog online upgrade"
        echo 
        return 0
    else
        failure $"$prog online upgrade"
        echo
        return 1
    fi
}
# Tell nginx to reopen logs
reopen_logs() {
    configtest_q || return 6
    echo -n $"Reopening $prog logs: "
    killproc -p $pidfile $prog -USR1
    retval=$?
    echo
    return $retval
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest|reopen_logs)
        $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|reopen_logs}"
        exit 2
esac

附nginx脚本介绍
nginx启动脚本

附nginx官方启动脚本地址:
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit
适用于RHEL, Fedora, CentOS。

添加自启动权限,启动服务

#附加执行权限
chmod +x /etc/init.d/nginx

#开机自启
chkconfig --add nginx
chkconfig nginx on

#启动服务
/etc/init.d/nginx start

#其他相对应的服务命令(可选 )
# (start | stop | restart | reload | status |  help)
service nginx restart  

你可能感兴趣的:(centos6.9编译安装nginx1.12.2)