当前位置: 首页 > 编程文章 >

CentOS 6.5 安装 Nginx 并配置服务自启

CentOS 6.5 安装 Nginx 并配置服务自启

作者:许宁时间:2026-01-16 11:11:05浏览:384

本文介绍在 CentOS 6.5 服务器上编译安装 Nginx 1.4.2 的方法,涵盖依赖包处理、SSL 模块支持及端口冲突注意事项。同时详细演示如何编写 init 脚本以实现 Nginx 服务的标准化管理和开机自动启动,解决 chkconfig 兼容性问题。

本文目录收起

本指南记录了在 CentOS 6.5 环境中部署 Nginx 的具体步骤,包括服务配置与开机自启动设置,供参考备忘。

安装 Nginx 及其依赖组件

第一步:安装 Nginx 及其相关依赖组件。

首先通过 SSH 连接至目标服务器,沿用常规的远程操作方式。


ssh -t 用户名@服务器IP或者域名 -p 22

ssh -t root@acheng1314.cn -p 22

执行连接命令后需输入密码,终端不会显示输入字符,务必确保密码准确无误。

成功登录服务器后,切换至常用的软件安装目录,例如 /usr/src,随后在终端中执行后续操作指令。



cd /usr/src

mkdir Nginx

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



wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz

tar -zxvf pcre-8.40.tar.gz

cd pcre-8.40

./configure

make

make install


cd ..

wget http://zlib.net/zlib-1.2.11.tar.gz

tar -zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure
make
make install



cd ..

wget http://www.openssl.org/source/openssl-fips-2.0.14.tar.gz

tar -zxvf openssl-fips-2.0.14.tar.gz

yum -y install openssl openssl-devel


wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2

./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make
make install

至此,Nginx 的基础安装工作已完成,但还需进一步进行服务器配置,包括添加 SSL 访问支持以及设置系统服务和开机自启动项。

第二步:配置服务器环境。

网络上关于服务器设置的资料繁多,但准确详尽的说明较少。以下提供经过验证的正确配置流程。



cd /opt/nginx/conf

vi nginx.conf

以下是 nginx.conf 配置文件的具体内容示例。


#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid    logs/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include    mime.types;
  default_type application/octet-stream;

  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #         '$status $body_bytes_sent "$http_referer" '
  #         '"$http_user_agent" "$http_x_forwarded_for"';

  #access_log logs/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  #keepalive_timeout 0;
  keepalive_timeout 65;

  #gzip on;
# 注意这里是设置本机的相关的东西,建议不要更改
  server {
    listen    80;
    server_name localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    root  html;
        index index.html index.htm;
    # proxy_pass http://localhost; 
    # proxy_set_header  Host  $host;
    # proxy_set_header  X-Real-IP  $remote_addr;
    # proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #error_page 404       /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #  proxy_pass  http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #  root      html;
    #  fastcgi_pass  127.0.0.1:9000;
    #  fastcgi_index index.php;
    #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    #  include    fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #  deny all;
    #}
  }


  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #  listen    8000;
  #  listen    somename:8080;
  #  server_name somename alias another.alias;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}

# 这里是设置本机的https访问的,这里必须设置才能正确时https
  # HTTPS server
  #
  server {
    listen    443;
    server_name localhost acheng1314.cn www.acheng1314.cn;

    ssl         on;
    # 这里是你申请的签名,扔到conf下面的cert目录中
    ssl_certificate   cert/214217283570796.pem;
    ssl_certificate_key cert/214217283570796.key;

    ssl_session_timeout 5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers  on;

    location / {
    #  root  html;
    #  index index.html index.htm;
  proxy_pass http://localhost;
  proxy_set_header  Host  $host;
  proxy_set_header  X-Real-IP  $remote_addr;
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
# 这里是设置域名跳转的,转发这些域名到本机的8080端口,
server { 
  listen    80; 
  server_name *.acheng1314.cn acheng1314.cn; 
  location / { 
    proxy_pass http://localhost:8080/; 
    proxy_set_header  Host  $host; 
    proxy_set_header  X-Real-IP  $remote_addr; 
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
  } 
}   

}

编写配置时需特别注意应用程序端口不能冲突。例如,若 Nginx 绑定 80 端口,而 Tomcat 也设定为 80 端口,即使绑定到 localhost 也会导致转发失败,因为网络端口同一时间只能被一个应用程序占用。



/opt/nginx/sbin/nginx -t

/opt/nginx/sbin/nginx

/opt/nginx/sbin/nginx -t



/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT

/etc/rc.d/init.d/iptables save

/etc/init.d/iptables status

完成上述步骤后,可测试服务器状态。正常情况下,此时服务器应已完全支持 HTTP 和 HTTPS 协议。

第三步:设置系统服务与开机自启动。

此部分操作相对简单,关键在于确保 Nginx 的路径设置正确。


#!/bin/sh
# Name:nginx4comex
# nginx - this script starts and stops the nginx daemon
#
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
#        proxy and IMAP/POP3 proxy server
# processname: nginx
# config:   /opt/nginx/conf/nginx.conf
# pidfile:   /comexHome/nginx/nginx.pid
#
# Created By http://comexchan.cnblogs.com/

# 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_DAEMON_PATH="/opt/nginx/sbin/nginx"
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
NGINX_LOCK_FILE="/var/lock/subsys/nginx4comex"
prog=$(basename $NGINX_DAEMON_PATH)

start() {
  [ -x $NGINX_DAEMON_PATH ] || exit 5
  [ -f $NGINX_CONF_FILE ] || exit 6
  echo -n $"Starting $prog: "
  daemon $NGINX_DAEMON_PATH -c $NGINX_CONF_FILE
  retval=$?
  echo
  [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE
  return $retval
}

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

restart() {
  configtest || return $?
  stop
  start
}

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

force_reload() {
  restart
}

configtest() {
 $NGINX_DAEMON_PATH -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

上文代码用于创建服务脚本,将其保存为 nginx4comex 文件(存放于 /opt/nginx 目录下,可使用 vim 编辑)。注意脚本中的路径需与实际配置保持一致。


NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

接下来继续在终端中执行相关指令以完成服务注册。



chmod u+x nginx4comex

cp nginx4comex /etc/init.d

service nginx4comex status

vim /etc/rc.local

/etc/init.d/nginx4comex start


reboot

最终实现了 Nginx 作为 Tomcat 代理的功能,并配置了服务器程序的自启动机制。

注意:nginx4comex 脚本可能无法直接被 chkconfig 识别,具体原因不明,但原作者文章中使用了 chkconfig 方法加入启动列表,对 Linux 感兴趣的用户可自行尝试验证。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

上一篇
Linux resolv.conf 配置详解与参数说明

resolv.conf是Linux系统中域名系统解析器(DNS Resolver)的核心配置文件,用于指导程序将域名转换为IP地址。该文件主要包含nameserver、search、domain、sortlist及options等关键指令,其中nameserver指定DNS服务器地址,search和domain处理短域名补全,options可调整超时时间、重试次数及名称校验规则。正确配置这些参数能有效提升域名解析效率并解决特定网络环境下的解析问题。

下一篇
CentOS 6.9 NFS服务安装与配置详解

本文介绍在CentOS 6.9环境下部署NFS服务的完整流程。通过安装nfs-utils包并配置rpcbind,实现服务端与客户端的基础连接。重点涵盖固定端口(2049、111)及随机端口的定义方法,结合iptables规则确保网络通畅。同时详细解析exports文件中的rw、ro等权限参数,以及Linux客户端使用mount命令或fstab进行持久化挂载的具体操作。

相关文章

更多
Nginx与Keepalived高可用主从架构配置指南

本文详细阐述了基于CentOS 6.6环境搭建Nginx与Keepalived高可用集群的配置流程。通过安装依赖、编译部署Nginx并配置upstream负载均衡,结合Keepalived实现VIP漂移与故障自动切换。文中涵盖了防火墙策略、开机自启设置、状态检测脚本编写及脑裂问题的解决方案,确保服务在主服务器宕机时由备用节点无缝接管。

2025.08.24 955
CentOS 6.5通过YUM安装Nginx及配置指南

在CentOS 6.5系统中,直接使用yum install nginx可能提示No package available。本文介绍如何通过安装EPEL扩展源来解决此问题,并执行nginx的安装与启动。同时涵盖检查防火墙80端口、查看配置文件路径以及根据CPU核数调整worker_processes等基础运维操作,帮助快速搭建Web服务环境。

2026.09.27 686
CentOS 6.7部署Nginx SSL证书配置指南

本文介绍在CentOS 6.7系统中为Nginx 1.8.1版本部署SSL证书以实现HTTPS访问的具体步骤。主要内容包括准备公钥和私钥文件,修改nginx.conf配置文件中的监听端口、域名及证书路径参数,并通过命令检查配置语法正确性后重载服务使生效。

2025.09.17 589
CentOS 6 使用 Nginx 搭建 Web 网站服务的方法

本文介绍在 CentOS 6 系统下利用 Nginx 搭建简易 Web 服务的流程。操作涵盖从 Xshell 连接云服务器、安装 GCC 及 PCRE 等依赖库,到下载编译 Nginx 源码并启动服务。文中提供基础配置文件参考,包含 Gzip 压缩与日志设置,并说明如何通过 IP 或域名访问验证服务状态。

2025.01.17 306
CentOS/RHEL下配置Nginx自启动脚本实例

在CentOS或RHEL系统中,通过编写位于/etc/init.d/的Shell脚本并配合chkconfig命令,可实现Nginx服务的标准化管理与开机自动启动。该方法需明确指定Nginx可执行文件路径、锁文件目录及配置文件位置,赋予脚本执行权限后,利用chkconfig将服务注册至系统运行级别,从而实现便捷的启停控制与状态查询。

2025.06.26 232
Nginx配置二级域名的方法实现

在Nginx中为同一IP配置多个项目时,可通过设置不同的server_name实现二级域名访问。以baidu.com为例,将主服务指向localhost,新增一个监听80端口、server_name为asurplus.baidu.com的server块,并通过proxy_pass转发至后端8081端口。修改配置后执行nginx -s reload命令重载即可生效。

2026.03.03 999

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。