Nginx 24.0.5 部署
文档资料
- 官方文档:Building nginx from Sources
- CSDN 文档:Nginx1.24源码安装与部署
介质路径
- 官方介质下载地址:https://nginx.org/en/download.html
安装 Nginx
1. 创建服务目录
mkdir -p /data/service/nginx/nginx_src
2. 下载并解压 Nginx 1.24.0
wget https://nginx.org/download/nginx-1.24.0.tar.gz -P /data/service/nginx
tar -xf /data/service/nginx/nginx-1.24.0.tar.gz -C /data/service/nginx/
3. 安装依赖服务
yum install make zlib zlib-devel pcre pcre-devel gcc* libtool openssl openssl-devel -y
4. 编译 Nginx
cd /data/service/nginx/nginx-1.24.0
# --prefix: 指定 Nginx 安装路径
./configure --prefix=/data/service/nginx/nginx_src
make && make install
5. 配置环境变量
配置环境变量主要用于方便运行 Nginx 命令,并不影响 Nginx 的部署和使用。若无此需求,可跳过此步骤
cat >> /etc/profile << EOF
# Nginx 1.24.0 2024-03-04
export NGINX_HOME=/data/service/nginx/nginx_src
export PATH=$PATH:$NGINX_HOME/bin:$NGINX_HOME/sbin:$NGINX_HOME/lib
EOF
source /etc/profile
- 验证
nginx -V
# 输出结果
# 能够输出相关信息,则表示环境变量配置成功
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/data/service/nginx/nginx_src
配置 Nginx
- 配置文件路径: $NGINX_HOME/conf/nginx.conf
# 全局块
# 服务进程数量。一般建议与 CPU 内核数保持一致
worker_processes 2;
# 事件块
events {
# 最大连接数
worker_connections 1024;
}
# http 块
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
# 客户端连接超时阈值。单位为秒
keepalive_timeout 65;
# 服务块
server {
# 指定 Nginx 监听端口
listen 80;
# 指定客户端连接时所使用的域名
server_name nginx-test.com;
# 位置块
# 定义根路径的访问规则
location / {
root html;
index index.html index.htm;
}
# 错误页返回的页面
error_page 500 502 503 504 /50x.html;
# 位置块
# 定义 /50x.html 路径的访问规则
location = /50x.html {
root html;
}
}
}
- 检查配置
nginx -t
# 输出结果
nginx: the configuration file /data/service/nginx/nginx_src/conf/nginx.conf syntax is ok
nginx: configuration file /data/service/nginx/nginx_src/conf/nginx.conf test is successful
启动 Nginx
nginx
检查 Nginx
- 查看进程
ps -ef | grep -v "grep" | grep nginx
# 输出结果
# 由于所配置的 worker_processes 为 2,所以生成 2 个 worker 进程
# 由于未指定 user,所以进程用户为默认用户 nobody
root 31709 1 0 10:33 ? 00:00:00 nginx: master process nginx
nobody 31710 31709 0 10:33 ? 00:00:00 nginx: worker process
nobody 31711 31709 0 10:33 ? 00:00:00 nginx: worker process
- 查看端口
netstat -tunlp | grep -w 80
# 输出结果
# 80 端口已处于监听状态
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31709/nginx: master
- 通过浏览器,访问地址: 10.10.10.131:80