【推荐 - 源码安装】nginx - 安装

准备

  1. 查看操作系统的版本信息
[root@lab10 ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
  1. 查看操作系统的网卡地址
[root@lab10 ~]# ip address show ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ec:f2:1b brd ff:ff:ff:ff:ff:ff
    inet 10.1.1.10/24 brd 10.1.1.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feec:f21b/64 scope link 
       valid_lft forever preferred_lft forever

创建nginx的源码目录

mkdir -p /opt/nginx

下载

  1. 访问https://nginx.org/en/download.html,下载 nginx

下载链接:https://nginx.org/download/nginx-1.26.2.tar.gz
文件全称:nginx-1.26.2.tar.gz
文件md5:1588676BE2A01A63D3A150FAE6C3F4A9

上传

将上述 下载 的5个安装包上传至 /opt/nginx

安装编译环境

yum -y install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed

创建 nginx 用户

useradd -s /sbin/nologin -r nginx

进入nginx源码目录

cd /opt/nginx

解压

tar -zxvf nginx-1.26.2.tar.gz

进入解压目录

cd /opt/nginx/nginx-1.26.2

执行配置

./configure \
  --user=nginx \
  --group=nginx \
  --with-stream \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --with-pcre \
  --with-stream_ssl_module \
  --with-stream_realip_module \
  --with-file-aio

编译

make

安装

make install

添加脚本

echo "/usr/local/nginx/sbin:${PATH}" > /etc/profile.d/nginx.sh

配置服务

vim /lib/systemd/system/nginx.service

内容如下:

[Unit]
Description=The nginx HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PidFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s hup $mainpid
ExecStop=/bin/kill -s quit $mainpid
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重载

systemctl daemon-reload

设置自动启动与启动nginx服务

systemctl enable --now nginx

访问

浏览器访问 https://10.1.1.10 ,效果如下

参考链接:

  1. 官网安装文档
  2. nginx编译 make 和 make install区别 nginx编译安装所有模块
posted @ 2024-09-15 00:04  chan_炽烽  阅读(1)  评论(0编辑  收藏  举报