nginx安裝及自啟動

weixin_34292287發表於2017-06-30

安裝過程參考 http://www.nginx.cn/install
nginx相關文件 http://www.nginx.cn/doc/index.html

安裝過程

  • 準備環境

    • 安裝make:yum -y install gcc automake autoconf libtool make
    • 安裝g++:yum install gcc gcc-c++
  • 1、選定原始碼目錄 /usr/local/src,儲存原始碼

  • 2、安裝PCRE庫注意:若pcre-8.40路徑找不到,到ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/下選取版本號

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz 
tar -zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure
make
make install
cd /usr/local/src
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
  • 4、安裝ssl
cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar -zxvf openssl-1.0.1t.tar.gz
  • 5、安裝nginx
cd /usr/local/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.40 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.0.1t
make
make install

<br />

  • 6、啟動nginx, 安裝成功後nginx在 /usr/local/nginx目錄下
    保證80埠未被佔用,執行/usr/local/nginx/nginx,啟動nginx,訪問ip,出現Welcome to nginx!

<br /><br />

設定nginx為服務,開機自啟動###

  • /etc/init.d目錄下新建nginx啟動指令碼,其中指令碼內容中的modify部分需要根據自己的安裝目錄來設定:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.1.12.0 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
#modify
nginxd=/usr/local/nginx/nginx                                   #nginx的啟動檔案
nginx_config=/usr/local/nginx/nginx.conf                        #
nginx_pid=/usr/local/nginx/nginx.pid                            #
#modify
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

<br />

  • 新增執行許可權 chmod +x nginx,設為服務chkconfig --add nginx,即可開機自啟動。

相關文章