Centos7.x编译安装Nginx教程

 Molet   2019-09-20 09:37   610 人阅读  0 条评论

安装Nginx

Nginx安装需要安装一系列支持,其中包含gcc、pcre、zlib、openssl

Nginx是C语言开发,所以需要gcc支持

[root@localhost ~]#     yum install -y gcc gcc-c++

[root@localhost ~]#    yum install -y pcre pcre-devel

[root@localhost ~]#    yum install -y zlib zlib-devel

[root@localhost ~]#     yum -y install openssl openssl-devel

[root@localhost ~]#     wget http://nginx.org/download/nginx-1.16.0.tar.gz #版本自己挑选

[root@localhost ~]#     tar -zxvf nginx-1.16.0.tar.gz               #解压           

[root@localhost ~]#     cd nginx-1.16.0/                                #进入nginx目录

[root@localhost ~]#     ./configure                                       

[root@localhost ~]#     make

[root@localhost ~]#     make install                                      安装完成

[root@localhost ~]#     /usr/local/nginx/conf/nginx               #启动nginx

[root@localhost ~]#     pkill -9 nginx                                    强制关闭nginx

浏览页面测试成功

 

下面将nginx写入系统服务

[root@localhost ~]#     cd /etc/init.d/

[root@localhost ~]#    vim nginx                                          填写代码

#! /bin/sh

# chkconfig: 2345 55 25

# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and

# run 'update-rc.d -f nginx defaults', or use the appropriate command on your

# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

 

### BEGIN INIT INFO

# Provides:          nginx

# Required-Start:    $all

# Required-Stop:     $all

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: starts the nginx web server

# Description:       starts nginx using start-stop-daemon

### END INIT INFO

 

# Author:   licess

# website:  http://www.94ip.com

 

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin    #注意路径

NAME=nginx

NGINX_BIN=/usr/local/nginx/sbin/nginx    #注意路径

CONFIGFILE=/usr/local/nginx/conf/nginx.conf    #注意路径

PIDFILE=/usr/local/nginx/logs/nginx.pid    #注意路径

ulimit -n 8192

case "$1" in

    start)

        echo -n "Starting $NAME... "

              if [ -f $PIDFILE ];then

                     mPID=`cat $PIDFILE`

                     isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`

                     if [ "$isStart" != '' ];then

                            echo "$NAME (pid `pidof $NAME`) already running."

                            exit 1

                     fi

              fi

 

        $NGINX_BIN -c $CONFIGFILE

 

        if [ "$?" != 0 ] ; then

            echo " failed"

            exit 1

        else

            echo " done"

        fi

        ;;

 

    stop)

        echo -n "Stoping $NAME... "

              if [ -f $PIDFILE ];then

                     mPID=`cat $PIDFILE`

                     isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`

                     if [ "$isStart" = '' ];then

                            echo "$NAME is not running."

                            exit 1

                     fi

              else

                     echo "$NAME is not running."

                     exit 1

        fi

        $NGINX_BIN -s stop

 

        if [ "$?" != 0 ] ; then

            echo " failed. Use force-quit"

            exit 1

        else

            echo " done"

        fi

        ;;

 

    status)

              if [ -f $PIDFILE ];then

                     mPID=`cat $PIDFILE`

                     isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`

                     if [ "$isStart" != '' ];then

                            echo "$NAME (pid `pidof $NAME`) already running."

                            exit 1

                     else

                            echo "$NAME is stopped"

                            exit 0

                     fi

              else

                     echo "$NAME is stopped"

                     exit 0

        fi

        ;;

    restart)

        $0 stop

        sleep 1

        $0 start

        ;;

 

    reload)

        echo -n "Reload service $NAME... "

              if [ -f $PIDFILE ];then

                     mPID=`cat $PIDFILE`

                     isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`

                     if [ "$isStart" != '' ];then

                            $NGINX_BIN -s reload

                            echo " done"

                     else

                            echo "$NAME is not running, can't reload."

                            exit 1

                     fi

              else

                     echo "$NAME is not running, can't reload."

                     exit 1

              fi

        ;;

 

    configtest)

        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t

        ;;

 

    *)

        echo "Usage: $0 {start|stop|restart|reload|status|configtest}"

        exit 1

        ;;

Esac

 

 

 

保存,退出

[root@localhost ~]#     chmod + x /etc/init.d/nginx (或chmod 755 /etc/init.d/nginx) 

#加权限

[root@localhost ~]#     chkconfig --add nginx        #注册为服务

[root@localhost ~]#     chkconfig nginx on             #设置为开机启动

[root@localhost ~]#     service nginx start              #启动

[root@localhost ~]#     service nginx stop              #停止

[root@localhost ~]#     service nginx restart           #重启

[root@localhost ~]#     service nginx reload            #重新载入   命令使用正常

 


本文地址:http://www.94ip.com/post/915.html
版权声明:本文为原创文章,版权归 Molet 所有,欢迎分享本文,转载请保留出处!

评论已关闭!