Linux.Centos6编译安装nginx

环境

系统环境:CentOS release 6.7 (Final)

需求

centos6.7编译安装nginx1.x

准备

安装依赖

yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel

下载安装包

cd /home/download

#download nginx

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

#download pcre

wget -c https://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz

#download zlib

wget -c http://zlib.net/zlib-1.2.8.tar.gz

#download openssl

wget -c http://www.openssl.org/source/openssl-1.0.1i.tar.gz

解压包

tar zxvf nginx-1.8.1.tar.gz

tar zxvf pcre-8.35.tar.gz

tar zxvf zlib-1.2.8.tar.gz

tar zxvf openssl-1.0.1i.tar.gz

添加用户

groupadd -r nginx

useradd -r -g nginx nginx

编译安装

cd /home/download/nginx-1.8.1

./configure 
--prefix=/usr/local/nginx 
--user=nginx 
--group=nginx 
--with-http_ssl_module 
--with-http_gzip_static_module 
--with-http_stub_status_module 
--with-http_realip_module 
--pid-path=/var/run/nginx.pid 
--with-pcre=/home/download/nginx/pcre-8.39 
--with-zlib=/home/download/nginx/zlib-1.2.11 
--with-openssl=/home/download/nginx/openssl-1.0.1i

make

make install

启动nginx

正确性检查

#每次修改nginx配置文件后都要进行检查

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

启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

下面添加一个脚本, 以方便控制nginx启动与停止

vi /etc/init.d/nginx

在文件中插入以下内容

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;

*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

注意红色加粗部分,需要将路径改为自己机器的相应路径。
接着,设置文件的访问权限:

chmod a+x /etc/init.d/nginx                                                         (a+x参数表示 ==> all user can execute  所有用户可执行)

最后将ngix加入到rc.local文件中,这样开机的时候nginx就默认启动了

vi /etc/rc.local

添加

/etc/init.d/nginx start   

保存并退出

下次重启就会生效,实现nginx的自启动。

原文地址:https://www.cnblogs.com/visionsl/p/8184647.html