栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

CentOS编译安装Nginx

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CentOS编译安装Nginx

CenOS安装Nginx 安装编译工具及库文件
#安装gcc环境
yum install -y gcc-c++

#安装PCRE库,用于解析正则表达式
yum install -y pcre pcre-devel

#zlib压缩和解压缩依赖
yum install -y zlib zlib-devel

#SSL 安全的加密的套接字协议层,用于HTTPS
yum install -y openssl openssl-devel
下载并解压缩

下载地址:http://nginx.org/en/download.html

#用命令下载
wget -c https://nginx.org/download/nginx-1.20.2.tar.gz

#解压缩
tar -zxf nginx-1.20.2.tar.gz
新增账号
#新增账号
useradd -s /sbin/nologin nginx
创建临时目录
#创建临时目录
mkdir -p /var/tmp/nginx
修改代码
#修改代码,去掉Header中的Server信息
#解压后的nginx目录
vim src/http/ngx_http_header_filter_module.c

#第49行至第51行修改前:
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;

#第49行至第51行修改后:
static u_char ngx_http_server_string[] = "" CRLF;
static u_char ngx_http_server_full_string[] = "" NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "" NGINX_VER_BUILD CRLF;
配置
#使用默认配置
./configure

#使用自定义配置
./configure 
--prefix=/usr/local/nginx 
--conf-path=/usr/local/nginx/conf/nginx.conf 
--pid-path=/run/nginx.pid 
--lock-path=/run/lock/nginx.lock 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--with-http_gzip_static_module 
--http-client-body-temp-path=/var/tmp/nginx/client 
--http-proxy-temp-path=/var/tmp/nginx/proxy 
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi 
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi 
--http-scgi-temp-path=/var/tmp/nginx/scgi 
--user=nginx 
--group=nginx 
--with-http_ssl_module 
--with-pcre
编译和安装
#编译
make

#安装
make install
创建系统服务
#创建文件
vim /usr/lib/systemd/system/nginx.service

#内容如下
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
#不拿掉会开不起来
#PrivateTmp=true

[Install]
WantedBy=multi-user.target
启动停止
#启动
systemctl start nginx

#停止
systemctl stop nginx

#重启
systemctl restart nginx
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/740802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号