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

Linux 6下编译安装Nginx完整指南

Linux 6下编译安装Nginx完整指南

作者:陈言时间:2025-08-10 11:29:43浏览:132

本文演示在Red Hat Enterprise Linux Server release 6.3环境下,从源码编译安装Nginx 1.8.0的完整流程。内容涵盖关闭防火墙与SELinux、安装pcre-devel等依赖包、配置编译参数及安装路径,并将Nginx注册为chkconfig系统服务以实现开机自启。同时整理了configure阶段常见的PCRE和zlib缺失错误,以及启动时用户组和目录权限不足的解决方案。

本文目录展开

Linux 6下编译安装Nginx完整指南

在Linux 6系统中进行Nginx的编译与安装操作指南

前言说明

Nginx作为高性能的HTTP、反向代理服务器,同时也支持IMAP/POP3/SMTP协议。在高并发连接场景下,它是Apache服务器的优秀替代方案,深受美国虚拟主机服务商青睐。其优势在于能响应高达50,000个并发连接且内存占用极低,这也是其广受欢迎的关键原因。本教程旨在展示如何在Linux 6平台上通过源码方式编译安装Nginx,供读者参考实践。

一、确定安装环境


# cat /etc/issue
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel \r on an \m
# nginx -v
nginx version: nginx/1.8.0

二、配置基础运行环境


###为简化安装及配置,此处关闭了防火墙,生产环境建议开启
# service iptables stop
# chkconfig iptables off

# vi /etc/selinux/config 
SELINUX=disabled

###创建用户及组
#groupadd -r nginx
#useradd -s /sbin/nologin -g nginx -r nginx

###安装环境依赖包 http://nginx.org/en/linux_packages.html
# yum install pcre-devel zlib-devel openssl openssl-devel gcc gcc-c++

三、执行Nginx的编译及安装


# cd /tmp/
# tar -xvf nginx-1.8.0.tar.gz
# cd /nginx-1.8.0
# ./configure           \
--prefix=/etc/nginx                     \
--sbin-path=/usr/sbin/nginx                 \
--conf-path=/etc/nginx/nginx.conf              \
--error-log-path=/var/log/nginx/error.log          \
--http-log-path=/var/log/nginx/access.log          \
--pid-path=/var/run/nginx.pid                \
--lock-path=/var/run/nginx.lock               \
--http-client-body-temp-path=/var/cache/nginx/client_temp  \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp      \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp    \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp      \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp       \
--user=nginx                         \
--group=nginx                        \
--with-http_ssl_module                    \
--with-http_realip_module                  \
--with-http_addition_module                 \
--with-http_sub_module                    \
--with-http_dav_module                    \
--with-http_flv_module                    \
--with-http_mp4_module                    \
--with-http_gunzip_module                  \
--with-http_gzip_static_module                \
--with-http_random_index_module               \
--with-http_secure_link_module                \
--with-http_stub_status_module                \
--with-http_auth_request_module               \
--with-mail                         \
--with-mail_ssl_module                    \
--with-file-aio                       \
--with-http_spdy_module                   \
--with-ipv6                         

Configuration summary
 + using system PCRE library
 + using system OpenSSL library
 + md5: using OpenSSL library
 + sha1: using OpenSSL library
 + using system zlib library

 nginx path prefix: "/etc/nginx"
 nginx binary file: "/usr/sbin/nginx"
 nginx configuration prefix: "/etc/nginx"
 nginx configuration file: "/etc/nginx/nginx.conf"
 nginx pid file: "/var/run/nginx.pid"
 nginx error log file: "/var/log/nginx/error.log"
 nginx http access log file: "/var/log/nginx/access.log"
 nginx http client request body temporary files: "/var/cache/nginx/client_temp"
 nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
 nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
 nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
 nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

###如果apache httpd服务启动,建议先停止或更改端口号
# service httpd stop
# mkdir -p /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
# make && make install

###启动nginx
# /usr/sbin/nginx -c /etc/nginx/nginx.conf

# ps -ef|grep nginx|grep -v grep
root   33412   1 0 10:18 ?    00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx   33413 33412 0 10:18 ?    00:00:00 nginx: worker process

[root@orasrv1 cache]# netstat -nltp|grep 80
tcp    0   0 0.0.0.0:80         0.0.0.0:*          LISTEN   33412/nginx     
[root@orasrv1 cache]# 

四、将Nginx配置为系统服务


vi /etc/init.d/nginx 

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
#        proxy and IMAP/POP3 proxy server
# Author : Leshami
# Blog  : http://blog.csdn.net/leshami      
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /etc/nginx/nginx.conf

#path for nginx binary
nginxd=/usr/sbin/nginx

#path for nginx configuration
nginx_config=/etc/nginx/nginx.conf

#path for nginx pid
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
  echo "nginx already running...."
  exit 1
fi
  echo -n $"Starting $prog: "
  daemon $nginxd -c ${nginx_config}
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
  return $RETVAL
}
# Stop nginx daemons functions.
stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
  echo -n $"Reloading $prog: "
  #kill -HUP `cat ${nginx_pid}`
  killproc $nginxd -HUP
  RETVAL=$?
  echo
}
# See how we were called.
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload)
    reload
    ;;
restart)
    stop
    start
    ;;
status)
    status $prog
    RETVAL=$?
    ;;
*)
    echo $"Usage: $prog {start|stop|restart|reload|status|help}"
    exit 1
esac
exit $RETVAL

# chmod u+x /etc/init.d/nginx 

# service nginx start
Starting nginx:                      [ OK ]

# ps -ef|grep nginx |grep -v grep
root   33534   1 0 10:33 ?    00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx   33535 33534 0 10:33 ?    00:00:00 nginx: worker process  

# service nginx stop
Stopping nginx:                      [ OK ]

# chkconfig --add nginx
# chkconfig nginx on

五、解决安装过程中的常见故障


./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib= option.
### 以上2个错误,请安装相应的依赖包,见本文第二部分:配置安装环境

# /usr/sbin/nginx 
nginx: [emerg] getpwnam("nginx") failed
### 需要创建nginx用户组及用户

# /usr/sbin/nginx
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
### 需要创建对应的目录

若在阅读或操作中遇到疑问,欢迎留言或在本站社区交流讨论。感谢阅读,希望本文能为您提供帮助,谢谢大家对本站的支持!

上一篇
Nginx负载均衡轮询方式配置详解

本文详细解析Nginx upstream模块支持的四种主要负载均衡分配算法:默认的轮询(round-robin)、加权轮询(weight)、IP哈希(ip_hash)以及热备(backup)。文章通过具体代码示例展示了各算法的配置方法,并解释了down、max_fails、fail_timeout等关键参数的作用机制,帮助管理员根据服务器性能差异和会话保持需求选择合适的负载策略。

下一篇
PHP register_shutdown_function 函数详解

register_shutdown_function() 用于在 PHP 脚本执行完毕后触发回调,适用于处理超时、强制停止或正常结束时的后续操作。该机制可替代默认的不友好提示,提供自定义的错误展示及临时文件清理等功能。需注意其执行顺序、exit() 的影响以及 Header 发送后的行为限制。

相关文章

更多
Nginx一键安装部署静态网页的过程详解

本文详细记录了在Linux服务器上通过源码编译方式安装Nginx并部署静态网页的全过程。主要步骤包括安装GCC、OpenSSL等依赖包,下载解压Nginx源码,执行configure、make及make install命令进行编译安装,修改默认80端口配置,启动服务后验证运行状态,最后将HTML文件上传至/usr/local/nginx/html目录完成页面展示。

2026.10.04 967
Aliyun Linux 编译安装 Tengine 2.3.2 详解

本文详细介绍在 Aliyun Linux 环境下编译安装 Tengine(Nginx 分支)的完整流程。涵盖编译环境准备,以及 PCRE、OpenSSL、Zlib、jemalloc 等核心依赖组件的安装配置。同时包含添加专用运行用户、执行 configure 编译参数设置,以及通过 systemd 配置开机自启和常用服务管理命令。

2025.12.13 852
Linux环境下安装与启动Nginx的详细步骤

本文介绍在Linux系统中安装和启动Nginx的方法。由于Nginx依赖于pcre和zlib库,需先分别安装这两个依赖项。随后下载并编译安装Nginx源码包,通过指定路径和模块参数完成配置。最后使用nginx命令启动服务,并可通过配置文件调整端口及反向代理等设置。

2026.11.17 842
Linux下Nginx安装、配置及日志监控指南

本文介绍在Linux系统中通过yum命令安装Nginx,并详细解析nginx.conf核心配置以启用自定义访问日志。通过创建测试资源并配合tail命令实时查看日志文件,可实现对Nginx服务访问情况的即时监控。

2025.01.29 781
Linux下Nginx负载均衡搭建指南

本文介绍在Linux环境下使用Nginx实现负载均衡的完整流程。首先解析四层与七层负载均衡的区别及常见软硬件方案,随后演示如何通过EPEL源安装Nginx,并详细展示upstream模块与服务块的具体配置方法,最后强调防火墙端口开放等关键注意事项。

2025.03.17 665
Nginx 编译安装与核心配置详解

本文整理 Nginx 在 CentOS 环境下的编译安装流程及常用第三方模块(如 sticky、upstream_check)的集成方法。详细解析 nginx.conf 配置文件结构,涵盖 main、http、server 和 location 四大指令上下文,深入说明 worker 进程优化、HTTP 压缩、反向代理参数及访问控制等关键配置项。

2026.09.08 578

专题推荐

更多
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 文本,也可将编码内容还原为文件,方便接口传输与调试。