Linux下安装Nginx服务及systemctl方式管理nginx详情

1.安装依赖包

yum一件安装所需的依赖包

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.下载nginx压缩包

在Nginx官网下载nginx(Index of /download/),更具自己想要的版本下载相应的tar.gz包

3.创建目录

在Linux环境下创建一个安装目录,推荐创建在/usr/local/下

mkdir -p /usr/local/nginx

------[也可以在此目录下直接下载使用(确保安装了wget 没有安装可以 yum install -y wget 下载一个)

wget http://nginx.org/download/nginx-1.19.9.tar.gz

下载完成后解压就可以了]------

4.上传并解压

将下载好的nginx压缩包上传至刚才所建的目录中/usr/local/nginx(我习惯用CRT这个工具,大家可以根据自己的喜好)

在linux中进入上传nginx的文件夹/usr/local/nginx并解压

cd /usr/local/nginx/

ls查看上传到nginx压缩包然后解压

tar -xvf nginx-1.19.9.tar.gz

5.编译安装

进入刚解压的nginx目录

cd /usr/local/nginx/nginx-1.19.9

编译:(依次执行下面三条命令)

./configure --with-http_stub_status_module --with-http_ssl_module

./configure
  • 这是一个用于准备编译的软件包的脚本。它会检查你的系统环境,并生成一个 Makefile,以便你可以使用 make 命令来编译软件。
--with-http_stub_status_module
  • 这个选项启用 Nginx 的 http_stub_status_module 模块。该模块提供了一个简单的状态监控接口,允许用户查看 Nginx 服务器的状态信息,例如当前的活动连接数、请求数等。
--with-http_ssl_module
  • 这个选项启用 Nginx 的 http_ssl_module 模块。此模块支持 SSL 和 TLS,使得 Nginx 可以处理 HTTPS 请求,从而提供安全的网络连接。

make

????make命令用于根据 Makefile 文件中的指令编译源代码。Makefile 指定了如何构建程序的目标(通常是可执行文件或库),以及这些目标之间的依赖关系。

make install

???make install 命令将编译完成的程序和相关文件复制到系统的适当位置,使其可以被系统和用户访问

6.配置nginx文件nginx.conf

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
cd /usr/local/nginx/conf/
vim/vi nginx.conf


在 Nginx 中,location 块用于定义如何处理特定请求的配置。以下是一些基本的 location 配置示例:

精确匹配

location = /exact-path {

对特定路径的处理

proxy_pass http://backend;

}

前缀匹配

location /prefix {

处理以 /prefix 开头的请求

proxy_pass http://backend; }

正则匹配

location ~ .php$ {

处理以 .php 结尾的请求

include fastcgi_params;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

}

忽略大小写的正则匹配

location ~* .(jpg|jpeg|png|gif)$ {

处理图片文件请求

expires 30d;

}

结合其他配置

location / {

try_files uri uri/ =404;

}


7.启动nginx

进入编译后的目录

cd /usr/local/nginx/sbin

启动

./nginx

没有返回值则表示启动成功

查看进程是否启动

ps -ef | grep nginx

在浏览器用 IP:端口 的形式访问

这样表示已经安装成功了


9.用systemctl方式管理nginx

01.创建配置文件

源码安装的nginx没有nginx.service这个文件,所以要创建

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

02.写入以下内容


Unit

Description=nginx - high performance web server

Documentation=http://nginx.org/en/docs/

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

Wants=network-online.target

Service

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

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

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s TERM $MAINPID

Install

WantedBy=multi-user.target

写完后wq保存退出


03.设置开机自启

systemctl enable nginx.service

现在就可以使用systemctl来管理nginx了

启动

systemctl start nginx

停止

systemctl stop nginx

状态

systemctl statusnginx

相关推荐
旖旎夜光1 天前
Linux(13)(中)
linux·网络
威迪斯特1 天前
CentOS图形化操作界面:理论解析与实践指南
linux·运维·centos·组件·图形化·桌面·xserver
一方热衷.1 天前
在线安装对应版本NVIDIA驱动
linux·运维·服务器
独自归家的兔1 天前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu
ONE_SIX_MIX1 天前
ubuntu 24.04 用rdp连接,桌面黑屏问题,解决
linux·运维·ubuntu
龙飞051 天前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
*小海豚*1 天前
在linux服务器上DNS正常,但是java应用调用第三方解析域名报错
java·linux·服务器
June`1 天前
muduo项目排查错误+测试
linux·c++·github·muduo网络库
春日见1 天前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
DARLING Zero two♡1 天前
告别 Docker 命令行!Portainer+cpolar 让容器管理从局域网走向公网
运维·docker·容器