首页 > 系统相关 >CentOS7安装Nginx并添加成服务【转】

CentOS7安装Nginx并添加成服务【转】

时间:2023-10-27 15:22:06浏览次数:50  
标签:nginx sudo CentOS7 Nginx 添加 usr conf local

当我们使用Nginx作为 Web 服务器时,将其添加为系统服务可以更方便地管理它的启动和停止。下面是 CentOS 7 将 Nginx 添加系统服务的方法步骤:

创建nginx服务配置文件

首先,创建一个 nginx 服务配置文件,并将以下内容复制到文件中:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target 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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • [Unit] 段是服务的元数据和运行级别配置。
  • [Service] 段是服务的主要操作和启动命令,包括PID 文件声明、预启动命令、启动命令、重载命令、停止命令和进程命名空间声明。
  • [Install] 段是运行级别的依赖项声明。

将配置文件保存为 /usr/lib/systemd/system/nginx.service

将上述配置保存为 nginx.service,并将其移动到 CentOS 7 系统服务的目录(/usr/lib/systemd/system/):

$ sudo mv nginx.service /usr/lib/systemd/system/

启动Nginx

要启动 nginx 服务,可以使用以下命令:

$ sudo systemctl start nginx

配置为开机自启

要将 nginx 服务配置为开机自启,可以运行以下命令:

$ sudo systemctl enable nginx

其他命令

要停止 nginx 服务,可以使用以下命令:

$ sudo systemctl stop nginx

要重新加载 nginx 配置文件,可以使用以下命令:

$ sudo systemctl reload nginx

以上是 CentOS 7 将 Nginx 添加系统服务的方法步骤,下面是两条实际的示例说明:

示例1:安装Nginx

  1. 安装依赖包:
$ sudo yum install gcc pcre-devel zlib-devel openssl-devel -y
  1. 下载 Nginx:
$ cd /usr/local/src
$ sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
  1. 解压并编译:
$ sudo tar xvf nginx-1.18.0.tar.gz
$ cd nginx-1.18.0/
$ sudo ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-file-aio
$ sudo make && sudo make install
  1. 测试 nginx 是否正常运行:
$ sudo /usr/local/nginx/sbin/nginx -t

如果没有错误,将输出 “nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful”。

  1. 启动 nginx:
$ sudo /usr/local/nginx/sbin/nginx
  1. 访问网站,将显示nginx欢迎页面。

示例2:添加Nginx系统服务

在安装 nginx 后,我们可以将其添加为系统服务。请参照上面的 CentOS 7 将 Nginx 添加系统服务的方法步骤。

$ sudo nano nginx.service

输出nginx的系统服务文本。

保存文件后,将文件移动到 /usr/lib/systemd/system/ 目录:

$ sudo mv nginx.service /usr/lib/systemd/system/

启动 Nginx 系统服务:

$ sudo systemctl start nginx

通过输入计算机的 IP 地址即可访问 nginx 首页。

转自

CentOS7将Nginx添加系统服务的方法步骤 - Python技术站
https://pythonjishu.com/pspgqqaindjliur/

标签:nginx,sudo,CentOS7,Nginx,添加,usr,conf,local
From: https://www.cnblogs.com/paul8339/p/17792435.html

相关文章

  • 19. 从零开始编写一个类nginx工具, 配置数据的热更新原理及实现
    wmproxywmproxy是由Rust编写,已实现http/https代理,socks5代理,反向代理,静态文件服务器,内网穿透,配置热更新等,后续将实现websocket代理等,同时会将实现过程分享出来,感兴趣的可以一起造个轮子法项目地址gite:https://gitee.com/tickbh/wmproxygithub:https://github.com/tickbh......
  • CentOS7系统放行TCP/UDP端口教程
    在使用CentOS7操作系统时,您需要放行某些端口,以便应用程序能够正常运行。下面是如何放行TCP/UDP端口的步骤。步骤1:SSH连接服务器使用SSH方式连接服务器,如果您不知道如何SSH连接服务器,可以查看该教程:SSH远程连接Linux服务器教程步骤2:确定要放行的端口在放行端口之前,您需要确定要......
  • Centos7系统,关闭了防火墙,但是有些端口仍然不能telnet通,解决方案
    问题:虚拟机centos7系统,已经关闭firewalld,但是除了22端口,其余端口无法被宿主机访问,本地访问正常解决步骤:1.查看防火墙状态:systemctlstatusfirewalldActive:inactive(dead)–防火墙已关闭Active:active(running)–防火墙已启动2.先开启firewalld:systemctlstartfirewall......
  • Jquery 将 JSON 列表的 某个属性值,添加到数组中,并判断一个值,在不在数据中
    jquery将JSON列表的某个属性值,添加到数组中如果你有一个JSON列表,并且想要将每个对象的某个属性值添加到数组中,你可以使用jQuery的$.each()函数来遍历JSON列表,并获取所需的属性值。以下是一个示例代码:varjsonList=[{"name":"John","age":30,"city":"NewYork"}......
  • Civil 3d 创建装配并添加、镜像部件
    英文论坛中有人提的问题,问题我也看不懂,就截图回复了一下,后来他进一步描述了问题,我大致清楚了,虽然我之前使用代码创建装配并添加过部件,但之前的操作仅添加一个部件,如何添加更多的部件并镜像部件,确实没有尝试过,于是试着写了以下测试代码,分享给大家。publicvoidC_AssTe......
  • 在以太坊区块链上添加一个区块
    包括json库的相关读取,proof-of-work算法的实现,MerkelTree的构建,使用hash创建新块等内容,使用本地json文件模拟mempool和blockchain,C++编写。#include<iostream>#include<fstream>#include<string>#include<nlohmann/json.hpp>#include<zlib.h>#include<openssl/......
  • CentOS7 设置防火墙操作、开放指定端口操作
    CentOS7设置防火墙操作、开放指定端口操作1,查看防火墙状态:firewall-cmd--statesystemctlstatusfirewalld.service2,开启防火墙:systemctlstartfirewalld.service(注意:开放防火墙后需要重启防火墙:firewall-cmd--reload)3,设置开机自启:systemctlenablefirewalld.servic......
  • .Net6添加Swagger
    .Net6添加Swagger一.添加Swagger引用添加Swashbuckle.AspNetCore包二.使用Swagger1.新建TestController控制器[ApiController][Route("[controller]/[action]")]publicclassTestController:Controller{///<summary>///获取信息///</summary>......
  • 好用的图片懒加载,图片可动态添加
    js: /* *LazyLoad-jQuerypluginforlazyloadingimages * *Copyright(c)2007-2013MikaTuupola * *LicensedundertheMITlicense: * http://www.opensource.org/licenses/mit-license.php * *Projecthome: * http://www.appelsiini......
  • 使用centos7搭建NTP服务器
    一、安装NTP服务yum-yinstallntpntpdata二、设置防火墙策略firewall-cmd--zone=public--add-port=123/udp--permanent #开放UDP123端口firewall-cmd--reload三、编辑NTP配置文件vim/etc/ntp.conf添加:server210.72.145.44prefer   #优先使用国家授时服务器serve......