linux 下安装Nginx

605 阅读2分钟

安装环境

在安装 nginx 前首先要确认系统中安装了 gcc、pcre-devel、zlib-devel、openssl-devel。

安装命令:

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

## 如果需要使用https协议,则需要安装相关依赖:
## yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

下载 gninx

我一般下载后放到 /usr/local/

外部下载

nginx 下载地址:nginx.org/download/

选择一个 ngxin 版本下载并上传到 linux 上

wget 命令下载

使用命令直接下载:

## 先到安装路径下
cd /usr/local

wget http://nginx.org/download/nginx-1.14.2.tar.gz

解压并配置

## 解压
tar -zxvf nginx-1.14.2.tar.gz

## 进入nginx目录
cd nginx-1.14.2
## 配置
./configure --prefix=/usr/local/nginx
## 如果要使用https协议,则要安装SSL模块
## ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

配置后生成 Makefile

安装

## 编译
make
## 安装
make install

测试

 /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

nginx 开机自启动

在 linux 系统的/etc/init.d/目录下创建 nginx 脚本文件

vim /etc/init.d/nginx

在脚本中添加如下命令

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# 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.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    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/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

  • 注: nginx的路径需要改成自己的 nginx 执行程序路径,NGINX_CONF_FILE的路径需要修改成自己安装的路径的配置。

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

chmod a+x /etc/init.d/nginx

通过该脚本启动停止 nginx 服务

/etc/init.d/nginx start
/etc/init.d/nginx stop

使用 chkconfig 进行管理,将 nginx 服务加入 chkconfig 管理列表

chkconfig --add /etc/init.d/nginx

使用 service 对 nginx 进行启动,停止。重启等操作

service nginx start
service nginx stop

设置终端模式开机启动

chkconfig nginx on

/etc/init.d/nginx: 没有那个文件或目录错误的解决方式

出现这种问题就是因为文件格式不同造成的,删除 nginx,使用 vi 编辑器新建文件把内容复制进去,然后设置权限即可。

rm -f ngxin
# 删除旧的ngixn

vi nginx
:wq
# 将脚本内容复制进去再保存

chmod a+x /etc/init.d/nginx
# 设置文件权限