nginx安装--源码&YUM

NGINX安装

环境:CentOS-7.9-x86_64-Everything-2207-02

nginx---yum安装

安装先决条件:

yum install yum-utils

设置 yum 存储库,设置/etc/yum.repo.d/nginx.repo

[nginx-stable]

name=nginx stable repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=1

enabled=1

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

[nginx-mainline]

name=nginx mainline repo

baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/

gpgcheck=1

enabled=0

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

更新缓存,查询安装包

默认情况下,使用稳定的 nginx 包的存储库。

安装nginx(稳定版本)

yum install nginx

删除nginx

yum remove nginx –y

如果您想使用主线 nginx 包, 运行以下命令:

sudo yum-config-manager --enable nginx-mainline #关闭用disable参数

nginx源码安装

nginx下载:

wget https://nginx.org/download/nginx-1.26.2.tar.gz

创建nginx目录

mkdir –p /usr/local/nginx

解压:

tar –xvzf nginx-1.26.2.tar.gz –C /usr/local/nginx

安装编译、依赖环境

yum install –y gcc-c++ openssl openssl-devel prce prce-devel zlib

#gcc-c++ 为编译器,因nginx使用C语言开发,需要安装相关的编译环境来支持编译安装;

#openssl 开放式安全套接层协议,常用于安全通信,避免窃听,同时确认另一端连接者的身份。

# PCRE(Perl Compatible Regular Expressions)是一个用C语言编写的正则表达式函数库,nginx 用于配置rewrite等功能时对其有依赖。

# zlib 是一个提供数据压缩的函数库,nginx 开启gzip功能时对其有依赖。

注:某些应用对pcre,zlib,等有特定版本要求时,需要进行源码安装。

编译和安装:

cd /usr/local/nginx/nginx/nginx-1.26.2

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-pcre

make -j$(nproc) #多线程编译,具体以CPU核数决定。

make install -j$(nproc)

检查没报错,安装完成。

查看和核验:

nginx –v #查看版本

建一个软链接:ln -sf /usr/local/nginx/sbin/nginx /usr/sbin/nginx

启动服务

cd /usr/local/nginx/sbin

./nginx #重启 ./nginx -s reload

默认为nobody角色运行,可以在配置文件中 加 user=nginx 。

关闭防火墙,或在防火墙中添加端口,访问浏览器。

补充:启动配置

配置systemd管理nginx服务,并开启自动启动

vim /usr/lib/systemd/system/nginx.service

[Unit]

Description=Nginx Server

After=network.target

[Service]

#User=root

#Group=root

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecStop=/usr/local/nginx/sbin/nginx -s stop

ExecReload=/usr/local/nginx/sbin/nginx -s reload

PrivateTmp=true

[Install]

WantedBy=multi-user.target

systemctl enable nginx

systemctl is-enabled nginx #查询是否开启nginx开机自动开启。

--》 预编译说明:

nginx预编译时可以指定nginx配置文件、二进制执行文件、PID的路径,也可以根据企业实际应用需求配置PCRE、zlib的版本。如下所示:

./configure

--sbin-path=/usr/local/nginx/nginx

--conf-path=/usr/local/nginx/nginx.conf

--pid-path=/usr/local/nginx/nginx.pid

--with-http_ssl_module

--with-pcre=../pcre2-10.39

--with-zlib=../zlib-1.3

posted on 2025-03-07 10:32  MaxConn  阅读(47)  评论(0)    收藏  举报

导航