CentOS7安装Nginx
-
-
- [Nginx 1.28.0 源码编译安装详细过程](#Nginx 1.28.0 源码编译安装详细过程)
-
- [1. 准备工作](#1. 准备工作)
- [2. 解压源码包](#2. 解压源码包)
- [3. 配置编译选项](#3. 配置编译选项)
- [4. 编译和安装](#4. 编译和安装)
- [5. 创建 Nginx 用户和组](#5. 创建 Nginx 用户和组)
- [6. 创建 Nginx 配置文件](#6. 创建 Nginx 配置文件)
- [7. 创建 mime.types 文件](#7. 创建 mime.types 文件)
- [8. 创建默认首页](#8. 创建默认首页)
- [9. 创建 Systemd 服务文件](#9. 创建 Systemd 服务文件)
- [10. 创建缓存目录](#10. 创建缓存目录)
- [11. 启动 Nginx 服务](#11. 启动 Nginx 服务)
- [12. 配置防火墙](#12. 配置防火墙)
- [13. 验证安装](#13. 验证安装)
- [14. 创建管理脚本](#14. 创建管理脚本)
- [15. 创建环境变量](#15. 创建环境变量)
- [16. 最终验证](#16. 最终验证)
- [17. 目录结构说明](#17. 目录结构说明)
- [18. 常用管理命令](#18. 常用管理命令)
- [19. 卸载方法(如果需要)](#19. 卸载方法(如果需要))
-
Nginx 1.28.0 源码编译安装详细过程
1. 准备工作
bash
# 切换到安装目录
cd /opt/nginx
# 安装编译依赖
sudo yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 创建必要的目录
sudo mkdir -p /opt/nginx/{bin,conf,logs,html,cache,ssl}
2. 解压源码包
bash
# 解压 Nginx 源码
sudo tar -xzf nginx-1.28.0.tar.gz
# 进入源码目录
cd nginx-1.28.0
3. 配置编译选项
bash
# 配置编译参数
sudo ./configure \
--prefix=/opt/nginx \
--sbin-path=/opt/nginx/bin/nginx \
--conf-path=/opt/nginx/conf/nginx.conf \
--error-log-path=/opt/nginx/logs/error.log \
--http-log-path=/opt/nginx/logs/access.log \
--pid-path=/opt/nginx/logs/nginx.pid \
--lock-path=/opt/nginx/logs/nginx.lock \
--http-client-body-temp-path=/opt/nginx/cache/client_temp \
--http-proxy-temp-path=/opt/nginx/cache/proxy_temp \
--http-fastcgi-temp-path=/opt/nginx/cache/fastcgi_temp \
--http-uwsgi-temp-path=/opt/nginx/cache/uwsgi_temp \
--http-scgi-temp-path=/opt/nginx/cache/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_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-threads \
--with-stream \
--with-stream_ssl_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_slice_module \
--with-compat
4. 编译和安装
bash
# 编译(使用多核加速)
sudo make -j$(nproc)
# 安装
sudo make install
5. 创建 Nginx 用户和组
bash
# 创建 nginx 用户和组
sudo groupadd nginx
sudo useradd -r -g nginx -s /bin/false nginx
# 设置目录权限
sudo chown -R nginx:nginx /opt/nginx
sudo chmod -R 755 /opt/nginx
6. 创建 Nginx 配置文件
bash
# 创建主配置文件
sudo vi /opt/nginx/conf/nginx.conf
配置文件内容:
nginx
user nginx nginx;
worker_processes auto;
error_log /opt/nginx/logs/error.log warn;
pid /opt/nginx/logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
include /opt/nginx/conf/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 /opt/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 虚拟主机配置
server {
listen 80;
server_name localhost;
root /opt/nginx/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
# 包含其他配置文件
include /opt/nginx/conf/conf.d/*.conf;
}
7. 创建 mime.types 文件
bash
# 复制 mime.types 文件
sudo cp conf/mime.types /opt/nginx/conf/
8. 创建默认首页
bash
# 创建默认 HTML 页面
sudo tee /opt/nginx/html/index.html <<'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Nginx 1.28.0</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; }
</style>
</head>
<body>
<h1>Welcome to Nginx 1.28.0</h1>
<p>If you see this page, the nginx web server is successfully installed and working.</p>
<p>Thank you for using nginx.</p>
<hr>
<p><em>Server: Nginx/1.28.0</em></p>
</body>
</html>
EOF
# 设置权限
sudo chown nginx:nginx /opt/nginx/html/index.html
9. 创建 Systemd 服务文件
bash
sudo vi /etc/systemd/system/nginx.service
服务文件内容:
ini
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/bin/nginx -t
ExecStart=/opt/nginx/bin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
User=nginx
Group=nginx
[Install]
WantedBy=multi-user.target
10. 创建缓存目录
bash
# 创建缓存目录
sudo mkdir -p /opt/nginx/cache/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
# 设置缓存目录权限
sudo chown -R nginx:nginx /opt/nginx/cache
sudo chmod -R 755 /opt/nginx/cache
11. 启动 Nginx 服务
bash
# 重新加载 systemd
sudo systemctl daemon-reload
# 测试配置文件
sudo /opt/nginx/bin/nginx -t
# 启动 Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 检查服务状态
sudo systemctl status nginx
12. 配置防火墙
bash
# 开放 HTTP 端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
13. 验证安装
bash
# 检查进程
ps -ef | grep nginx
# 检查端口
netstat -tulnp | grep nginx
# 测试访问
curl -I http://localhost
# 检查版本
/opt/nginx/bin/nginx -v
# 查看编译参数
/opt/nginx/bin/nginx -V
14. 创建管理脚本
bash
# 创建管理脚本
sudo tee /opt/nginx/nginx-manager.sh <<'EOF'
#!/bin/bash
NGINX_BIN="/opt/nginx/bin/nginx"
NGINX_CONF="/opt/nginx/conf/nginx.conf"
case "$1" in
start)
$NGINX_BIN -c $NGINX_CONF
;;
stop)
$NGINX_BIN -s stop
;;
reload)
$NGINX_BIN -s reload
;;
restart)
$NGINX_BIN -s stop
sleep 2
$NGINX_BIN -c $NGINX_CONF
;;
test)
$NGINX_BIN -t -c $NGINX_CONF
;;
status)
ps -ef | grep nginx | grep -v grep
;;
*)
echo "Usage: $0 {start|stop|reload|restart|test|status}"
exit 1
;;
esac
EOF
sudo chmod +x /opt/nginx/nginx-manager.sh
15. 创建环境变量
bash
# 添加到 ~/.bashrc 或 /etc/profile
echo 'export PATH=$PATH:/opt/nginx/bin' | sudo tee -a /etc/profile
source /etc/profile
16. 最终验证
bash
#!/bin/bash
echo "=== Nginx 1.28.0 安装验证 ==="
echo "1. 服务状态:"
sudo systemctl status nginx --no-pager -l
echo -e "\n2. 进程检查:"
ps -ef | grep nginx | grep -v grep
echo -e "\n3. 端口监听:"
netstat -tulnp | grep nginx
echo -e "\n4. 版本信息:"
/opt/nginx/bin/nginx -v
echo -e "\n5. 目录结构:"
tree /opt/nginx -L 2
echo -e "\n6. 测试访问:"
curl -s http://localhost | grep -o "<title>.*</title>"
echo -e "\n7. 状态页面:"
curl -s http://localhost/status
17. 目录结构说明
安装完成后,目录结构如下:
/opt/nginx/
├── bin/
│ └── nginx # Nginx 可执行文件
├── conf/
│ ├── nginx.conf # 主配置文件
│ └── mime.types # MIME 类型文件
├── html/
│ └── index.html # 默认首页
├── logs/
│ ├── access.log # 访问日志
│ ├── error.log # 错误日志
│ ├── nginx.pid # 进程ID文件
│ └── nginx.lock # 锁文件
└── cache/
├── client_temp/ # 客户端临时文件
├── proxy_temp/ # 代理临时文件
├── fastcgi_temp/ # FastCGI 临时文件
├── uwsgi_temp/ # uWSGI 临时文件
└── scgi_temp/ # SCGI 临时文件
18. 常用管理命令
bash
# 启动服务
sudo systemctl start nginx
# 停止服务
sudo systemctl stop nginx
# 重启服务
sudo systemctl restart nginx
# 重新加载配置
sudo systemctl reload nginx
# 查看服务状态
sudo systemctl status nginx
# 测试配置文件
sudo /opt/nginx/bin/nginx -t
# 查看版本和编译参数
/opt/nginx/bin/nginx -V
19. 卸载方法(如果需要)
bash
# 停止服务
sudo systemctl stop nginx
sudo systemctl disable nginx
# 删除服务文件
sudo rm -f /etc/systemd/system/nginx.service
sudo systemctl daemon-reload
# 删除安装目录
sudo rm -rf /opt/nginx
# 删除用户和组
sudo userdel nginx
sudo groupdel nginx
通过以上详细步骤,您已成功在 /opt/nginx 目录下编译安装了 Nginx 1.28.0。现在可以通过 http://服务器IP 访问您的 Nginx 服务器。