CentOS7.6编译安装nginx

配置阿里云yum源
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
安装nginx依赖环境
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install gcc-c++
yum install -y openssl openssl-devel

从官网下载nginx1.4.0

wget -c https://nginx.org/download/nginx-1.14.0.tar.gz

解压

.tar.gz
cd nginx-

这里使用nginx默认的配置

./configure
make & make install
whereis nginx

[root@master ~]# whereis nginx
nginx: /usr/local/nginx

进入sbin目录,可以看到有一个可执行文件nginx,直接./执行就OK了。

运行起来之后访问服务器ip,可以看到nginx的欢迎页面

此时已经初步完成

接下来将对nginx配置开机自启动

首先,在linux系统的/etc/init.d/目录下创建nginx文件

vim /etc/init.d/nginx

从官网复制启动脚本http://wiki.nginx.org/RedHatNginxInitScript保存至/etc/init.d/nginx中

如果你是自定义编译安装的nginx,需要根据您的安装路径修改下面这两项配置:

nginx_pid=/usr/local/nginx/logs/nginx.pid #如果没有请单独添加一条

nginx=”/usr/local/nginx/sbin/nginx” #修改成nginx执行程序的路径

NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” #修改成配置文件的路径

如果你是完全按照我的步骤进行安装可以不做修改

官网nginx默认脚本如下:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   -
# 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.
[ 

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V >& | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V >& | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=``
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit
    [ -f $NGINX_CONF_FILE ] || exit
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq  ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq  ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop

    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/>&
}

case "$1" in
    start)
        rh_status_q && exit
        $
        ;;
    stop)
        rh_status_q || exit
        $
        ;;
    restart|configtest)
        $
        ;;
    reload)
        rh_status_q || exit
        $
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit
esac
接着在以下路径中创建一个nginx文本,路径同上vim /etc/sysconfig/nginx
NGINX_CONF_FILE=/usr/local/nginx/conf/nginx.conf

保存脚本文件后设置文件的执行权限:

chmod +x /etc/init.d/nginxsystemctl daemon-reload
/etc/init.d/nginx start
/etc/init.d/nginx stop
先将nginx服务加入chkconfig管理列表chkconfig --add /etc/init.d/nginx
可以使用service对nginx进行启动,重启等操作了service nginx start
service nginx stopcentos7.x请使用以下命令进行操作

systemctl start nginx
systemctl stop nginx
systemctl status nginx

 
加入开机启动项chkconfig nginx on

参考文档:https://www.cnblogs.com/123cn/p/5740753.html

https://blog.csdn.net/u013870094/article/details/52463026

上一篇:log4j定义某个类的日志级别


下一篇:用icas下载文件报错