问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Nginx生产环境部署最佳实践

创作时间:
2025-01-22 06:35:21
作者:
@小白创作中心

Nginx生产环境部署最佳实践

在生产环境中部署Nginx需要经过一系列的步骤和配置优化,以确保其稳定性和性能。本文将详细介绍从安装依赖到设置开机自启的完整过程,帮助读者掌握Nginx生产环境部署的最佳实践。

实验环境

  • 操作系统:Centos 7.9
  • 硬件配置:4核8G SSD 100G

步骤1:安装依赖

在开始部署Nginx之前,需要先安装一些必要的依赖包。这些依赖包包括:

  • gcc:GNU编译器集合,用于编译C、C++等语言的源代码
  • pcre:Perl兼容正则表达式库,用于模式匹配
  • zlib:数据压缩库
  • openssl:安全套接层协议工具包
  • make:构建自动化工具
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel make wget

步骤2:创建Nginx用户

为了安全考虑,需要为Nginx创建一个专用的用户和用户组。

groupadd nginx
useradd nginx -m -d /home/nginx -g nginx
echo nginx:nginx|chpasswd

步骤3:创建Nginx相关目录

创建Nginx所需的目录结构,并设置权限。

mkdir -p /usr/local/nginx/{cache,log,conf/conf.d}
chown -R nginx:nginx /usr/local/nginx

步骤4:下载Nginx包并解压

下载Nginx源码包并解压到指定目录。

mkdir /opt/software/
cd /opt/software/
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz

步骤5:编译并安装Nginx

配置Nginx的编译选项,并进行编译和安装。

cd nginx-1.20.1
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/log/error.log \
--http-log-path=/usr/local/nginx/log/access.log \
--pid-path=/usr/local/nginx/nginx.pid \
--lock-path=/usr/local/nginx/nginx.lock \
--http-client-body-temp-path=/usr/local/nginx/cache/client_temp \
--http-proxy-temp-path=/usr/local/nginx/cache/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/cache/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/cache/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/cache/scgi_temp \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module 

make && make install

步骤6:修改Nginx配置文件

根据业务需求调整Nginx的配置文件。

cat << EOF | tee /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes auto;
pid /usr/local/nginx/nginx.pid;
worker_cpu_affinity auto;
worker_rlimit_nofile 1024000;
events {
    use epoll;
    accept_mutex on;
    multi_accept on;
    worker_connections 65535;
}
http {   
    include mime.types;
    access_log off;
    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    gzip on;
    include /usr/local/nginx/conf/conf.d/*.conf;
}
EOF

步骤7:设置Nginx开机自启

创建systemd服务文件,使Nginx能够随系统启动自动运行。

cat << EOF |tee /etc/systemd/system/nginx.service
[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/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

启用并启动Nginx服务:

systemctl daemon-reload && systemctl enable --now nginx

检查Nginx服务状态:

systemctl status nginx

补充命令

  • 检查Nginx配置语法:

    nginx -t
    
  • 重新加载Nginx配置:

    systemctl reload nginx
    
  • 停止Nginx服务:

    systemctl stop nginx
    
  • 重启Nginx服务:

    systemctl restart nginx
    

通过以上步骤,你可以在生产环境中成功部署Nginx,并进行相应的配置优化。建议将具体的配置内容写在/usr/local/nginx/conf/conf.d目录下的.conf结尾的文件中。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号