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
结尾的文件中。
热门推荐
世界公认的七种高效思维方法
汽修技能的学习路径是什么?汽修行业的就业前景和发展趋势有哪些?
如何克服失恋的痛苦:五个步骤让你重新站起来!
VR大模型及案例分享
铜陵游玩攻略:从景点到美食的全方位旅游指南
皮肤麻木背后的医学真相:从气血失调到神经损伤
港澳旅游市场加速复苏,2024年有望恢复至疫前水平
港澳游新政策,你get了吗?
谷爱凌的冰雪奇缘:八字与星座揭秘天才少女的成功密码
八字预测:揭秘你的未来人生关键事件
2024年办理房产证所需证件及流程详解
证据复印件模糊不清?这些解决方案帮你提升复制效果
浙江丽水四日自驾游攻略:从杭州出发,深度体验丽水山水人文之美
衣服油漬也能輕鬆「搞掂」?編輯實測5大平價去污祕技!落妝濕紙巾成最強急救法寶
大肠癌患者杨先生:中医理疗助他找回感觉
秋冬养生新趋势:中医理疗助你身心平衡
黑木耳+蜂蜜+红糖,轻松告别皮肤麻木!
三年级寒假阅读挑战:提升写作灵感
《哈利·波特》亲子共读:提升三年级孩子阅读兴趣
《好饿的毛毛虫》:亲子共读新体验
《丽声北极星分级绘本》:提升孩子语言能力的秘密武器
生态监测:构建生物多样性智慧监测体系的五大核心环节
秘鲁"国宝"植物牵起中拉生物多样性保护合作
华南植物园:珍稀濒危植物的“诺亚方舟”
李贺诗歌特色分析
古代没有“0”这个数字?那古人是如何计算和表达“0”的?
拉伸+瑜伽,告别手脚麻木烦恼!
你也有过腿麻到怀疑人生的时刻吗?
铜陵5A级景区探秘:入冬5日游,美食与美景的完美融合
玩具背后的故事:一场关于想象与创新的探索之旅