nginx部署安装

1. 环境准备

1.1 系统配置优化

# 修改主机名-(有需求的可以修改下主机名-nginx-prod-01)
hostnamectl set-hostname nginx-prod-01
​
# 更新系统
yum update -y && reboot

1.2 防火墙配置(生产环境建议)

# 永久开放80/443端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload
​
# 查看开放端口
firewall-cmd --list-ports

1.3 安装依赖组件

yum install -y gcc-c++ \
               pcre pcre-devel \
               zlib zlib-devel \
               openssl openssl-devel \
               wget \
               tree \
               lsof
依赖包功能说明
gcc-c++C/C++编译环境
pcre/pcre-devel正则表达式支持
zlib/zlib-devel数据压缩支持
openssl-develSSL/TLS支持
wget文件下载工具

2. Nginx 安装部署

2.1 创建专用用户

groupadd nginx -g 2000
useradd nginx -u 2000 -g 2000 -s /sbin/nologin -M

2.2 下载与解压

# 推荐长期支持版本
wget https://nginx.org/download/nginx-1.24.0.tar.gz --no-check-certificate
​
# 校验压缩包
sha256sum nginx-1.24.0.tar.gz | grep a2d764e12d1e45bf9ff239e1d652da2a
​
tar xf nginx-1.24.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.24.0

2.3 编译安装

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_v2_module \
--with-threads \
--with-file-aio
​
make -j $(nproc) && make install

推荐编译参数说明

  • --with-http_v2_module:启用HTTP/2支持

  • --with-threads:启用线程池特性

  • --with-file-aio:启用异步文件I/O

3. 服务管理配置

3.1 Systemd 服务配置

cat > /etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
​
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
Restart=on-failure
RestartSec=3
​
[Install]
WantedBy=multi-user.target
EOF

3.2 服务管理命令

# 重新加载服务配置
systemctl daemon-reload
​
# 启动服务
systemctl start nginx
​
# 设置开机自启
systemctl enable nginx
​
# 查看状态
systemctl status nginx
​
# 热重载配置
systemctl reload nginx

4. 环境变量配置

echo 'export PATH=$PATH:/usr/local/nginx/sbin' > /etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh

5. 部署后验证

5.1 基础检查

# 版本验证
nginx -v
​
# 配置文件语法检查
nginx -t
​
# 监听端口验证
ss -tunlp | grep nginx

5.2 访问测试

curl -I 127.0.0.1
# 应返回 200 OK

6. 安全加固建议

6.1 基础安全设置

# 在nginx.conf中添加:
server_tokens off;
keepalive_timeout 60;
​
# 默认server段配置
server {
    listen 80 default_server;
    server_name _;
    return 444;
}

6.2 文件权限设置

chown -R nginx:nginx /usr/local/nginx
find /usr/local/nginx -type d -exec chmod 750 {} \;
find /usr/local/nginx -type f -exec chmod 640 {} \;

附录:常用维护命令

# 日志切割(需配合crontab使用)
mv /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access_$(date +%F).log
nginx -s reopen
​
# 性能调优建议
worker_processes auto;
worker_rlimit_nofile 65535;
​
events {
    worker_connections 10240;
    use epoll;
}

注意事项

  1. 生产环境应保持防火墙开启状态,仅开放必要端口

  2. 建议配置logrotate进行日志轮转

  3. 敏感配置文件应设置600权限

  4. 长期运行需配置监控和健康检查

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值