1. 程式人生 > >centos下nginx的安裝及啟動

centos下nginx的安裝及啟動

1、安裝前必備

安裝前確保確保進行了安裝了linux常用必備支援庫。檢查是否安裝了g++、gcc、openssl。rpm -qa | grep gcc 之後需要出現3個包如下圖所示。如果沒有出現。需要安裝g++、gcc。

# yum install gcc-c++

pcre-8.12.tar.gz

    # cd /usr/local/src/nginx

    # tar zxvf pcre-8.12.tar.gz

    (1)進入解壓後的目錄

    # cd pcre-8.12    

    (2)配置

    #  ./configure

    (3)編譯

    #  make

    (4)安裝

    #  make install

2、nginx安裝

(1)解壓

# cd /usr/local/src/nginx

# tar zxvf nginx-1.12.2.tar.gz

(2)進入目錄

# cd nginx-1.12.2

(3)配置

  # ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

(4)編譯

# make

  (5) 安裝

# make install

(6)檢查是否安裝成功

    # cd  /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

(7)做成服務 

    新建nginx檔案:vim /etc/init.d/nginx 

   檔案內容:

#!/bin/bash # nginx Startup script for the Nginx HTTP Server # this script create it by caffreyxin at 2007.10.15. # it is v.0.0.1 version. # if you find any errors on this scripts, please contact caffreyxin. # and send mail to xinyflove at sina dot com. # # 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 修改檔案許可權:chmod 755 /etc/init.d/nginx

設定開機啟動: chkconfig nginx on

啟動服務:service nginx start

停止服務:service nginx stop

重啟服務:service nginx reload