Nginx 安装(源码)

junlan
6
2025-02-21

一、官网地址

官方包安装方法: https://nginx.org/en/linux_packages.html#RHEL

官网二进制安装包: https://nginx.org/en/download.html

源码安装说明:https://nginx.org/en/docs/configure.html

https://www.foooor.com/Nginx/01-Nginx%E7%AE%80%E4%BB%8B.html

二、源码安装

1. 下载源码包

[root@localhost ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz

2. 安装编译的依赖包

gcc 用于编译C语言的程序,PCRE 是为了支持正则表达式,Nginx 的 Core 和 Rewrite 模块需要;zlib 用来支持标头压缩,Nginx 的 Gzip 模块需要;OpenSSL 用来支持 https 协议, Nginx 的 SSL 模块和其他模块需要。

[root@localhost ~]# dnf install gcc make zlib**-**devel pcre-devel openssl-devel **perl-ExtUtils-Embed -**y

3. 解压文件包

[root@localhost ~]# tar zxf nginx-1.26.2.tar.gz && cd nginx-1.26.2/

4. 开始编译

--prefix:指定安装的路径

[root@localhost sbin]# **./configure \**

**--prefix=/****opt****/nginx \**

**--with-http_ssl_module \**

**--with-stream \**

**--with-http_gzip_static_module**

5. 开始安装

[root@localhost nginx-1.26.2]# make && make install

6. 创建链接,可使用 nginx 命令启动服务

[root@localhost sbin]# ln -s /opt/nginx/sbin/nginx /usr/local/sbin/nginx

[root@localhost sbin]# nginx # 启动 nginx 服务

7. 放行 80 端口

[root@localhost sbin]# firewall-cmd --permanent --add-service=http && firewall-cmd --reload

9. 通过浏览器访问 http://IP 就可以访问了

三、设置可通过 systemctl 管理服务器

1. 创建一个 systemd 服务文件

[root@localhost ~]# **cat <<EOF > /etc/systemd/system/nginx.service**

[Unit]

**Description=The NGINX HTTP and reverse proxy server**

**After=network.target remote-fs.target nss-lookup.target**

 

**[Service]**

**Type=forking**

**ExecStart=/opt/nginx/sbin/nginx**

**ExecReload=/opt/nginx/sbin/nginx -s reload**

**ExecStop=/opt/nginx/sbin/nginx -s quit**

**PrivateTmp=true**

 

**[Install]**

**WantedBy=multi-user.target**

EOF

2. 重载配置文件

[root@localhost ~]# sudo systemctl daemon-reload

3. 启动 nginx 服务

[root@localhost ~]# systemctl enable --now nginx

四、查看状态

查看版本

[root@localhost ~]# nginx -V

nginx version: nginx/1.26.2

built by gcc 11.5.0 20240719 (Red Hat 11.5.0-2) (GCC)

built with OpenSSL 3.2.2 4 Jun 2024

TLS SNI support enabled

configure arguments: --prefix=/opt/nginx --with-http_ssl_module --with-stream --with-http_gzip_static_module

查看监听的端口

[root@localhost ~]# ss -ntl | grep 80

校验配置

-T显示所有配置文件

[root@localhost ~]# **nginx -t**

nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

查看 nginx 进程

[root@localhost ~]# ps -ef | grep nginx

root 44796 1 0 10:38 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx

nobody 44797 44796 0 10:38 ? 00:00:00 nginx: worker process

查看目录结构

[root@localhost ~]# tree /opt/nginx/

查看默认配置文件

[root@localhost ~]# grep -Ev '#|^$' /opt/nginx/conf/nginx.conf

查看帮助手册

[root@localhost ~]# man ./nginx-1.26.2/man/nginx.8