阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS编译安装Nginx

148次阅读
没有评论

共计 3603 个字符,预计需要花费 10 分钟才能阅读完成。

CentOS 编译安装 Nginx

下载

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

解压缩

tar zxvf nginx-1.9.6.tar.gz

进入 nginx 目录

cd nginx-1.9.6

设置,编译,安装(configure 过程可能出现的情况,见文章最尾)

./configure –prefix=/data/server/nginx
make && make install && make clean

修改 nginx 配置文件

vi /data/server/nginx/conf/nginx.conf1

将根目录修改至 /data/webroot/localhost

找到
location / {
    root    html;              # 根目录
    index index.html index.htm; # 默认文档
}
改为
location / {
    root /data/webroot/localhost;  # 站点根目录路径
    index index.html index.htm;    # 目前只放静态页,所以不需要改
}

创建 nginx 系统服务脚本

vi /etc/init.d/nginx

填入如下内容

#!/bin/sh
#
#nginx – this script starts and stops the nginx daemin
#
# chkconfig:  – 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#              proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:    /data/webroot/logs/localhost/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=”/data/server/nginx/sbin/nginx” # nginx 启动文件
prog=$(basename $nginx)

NGINX_CONF_FILE=”/data/server/nginx/conf/nginx.conf” # 配置文件路径

lockfile=/var/lock/subsys/nginx

start() {
    [-x $nginx] || exit 5
    [-f $NGINX_CONF_FILE] || exit 6
    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
    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

保存

:wq 对脚本添加可执行权限

chmod +x /etc/init.d/nginx

添加 nginx 到自启动服务

chkconfig –add nginx

设置开机启动

chkconfig nginx on

开启访问端口
vi /etc/sysconfig/iptables

添加如下

-A INPUT –m state –state NEW –m tcp –p tcp –dport 80 –j ACCEPT

重启防火墙以应用规则

/etc/init.d/iptables restart1

 
此后,执行命令脚本即可,无需执行原路径的执行文件

/etc/init.d/nginx start    # 启动 nginx
/etc/init.d/nginx stop      # 停止 nginx
/etc/init.d/nginx restart  # 重启 nginx
/etc/init.d/nginx reload    # 重新加载配置文件

原操作命令

/data/server/nginx/sbin/nginx              # start server
/data/server/nginx/sbin/nginx -s stop      # fast shutdown
/data/server/nginx/sbin/nginx -s quit      # graceful shutdown
/data/server/nginx/sbin/nginx -s reload    # reloading the configuration file
/data/server/nginx/sbin/nginx -s reopen    # reopening the log files

至此,nginx 的安装与最基本的配置已完成

configure 过程可能出现的情况

CentOS 编译安装 Nginx

缺少 C 编译器,需要安装 gcc
运行安装命令,yum install -y gcc

CentOS 编译安装 Nginx

未安装 PCRE
运行安装命令,yum install -y pcre pcre-devel

CentOS 编译安装 Nginx

缺少安全库

运行安装命令,yum install -y openssl openssl-devel

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

Ubuntu 16.04 LTS 上安装 Nginx、MariaDB 和 HHVM 运行 WordPress http://www.linuxidc.com/Linux/2016-10/136435.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Linux(RHEL7.0)下安装 Nginx-1.10.2 http://www.linuxidc.com/Linux/2016-10/136484.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/137563.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3603字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中