nginx源码安装并设置开机自启

NGINX源码安装

安装编译器和依赖包

openssl 软件包是用于提供网站加密证书服务的程序文件

pcre供 Perl 语言兼容的正则表达式库的软件包

[root@localhost ~]# yum -y install gcc pcre-devel openssl-devel

在这里插入图片描述

上传nginx源码包并解压

在这里插入图片描述

切换到解压之后的nginx目录检测编译环境

[root@bogon ~]# cd nginx-1.18.0/

在这里插入图片描述

编译并安装

在这里插入图片描述

开启NGINX服务并查看端口验证

在这里插入图片描述

浏览器输入IP验证:

在这里插入图片描述

查看参数

在这里插入图片描述

设置开机自启

编写service脚本:

在 /usr/lib/systemd/system/ 目录下编写脚本

[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service
脚本内容为
[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
   
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
   
[Install]
WantedBy=multi-user.target

启动nginx

systemctl start nginx.service  

停止nginx

systemctl stop nginx.service  

设置开机启动:

systemctl enable nginx

重启

systemctl restart nginx

重载nginx服务配置文件,类似于重启,但服务不会中止

systemctl reload nginx 

关闭开机启动

systemctl disable nginx.service 

如果报错

如果出现报错内容"Failed to execute operation: Access denied",输入"systemctl daemon-reexec"即可解决。

参考大佬文章

链接:>> https://blog.csdn.net/wsdc0521/article/details/106306209

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Nginx自动源码安装的Shell脚本,仅供参考: ```bash #!/bin/bash NGINX_VERSION="1.20.1" #Nginx版本号 NGINX_INSTALL_DIR="/usr/local/nginx" #Nginx安装路径 #安装依赖库 yum -y install pcre-devel openssl-devel zlib-devel gcc make #下载Nginx源码包 wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz #解压源码包 tar -zxvf nginx-$NGINX_VERSION.tar.gz #进入源码目录 cd nginx-$NGINX_VERSION #编译安装Nginx ./configure --prefix=$NGINX_INSTALL_DIR --with-http_ssl_module make && make install #添加Nginx服务 cat > /usr/lib/systemd/system/nginx.service <<EOF [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=$NGINX_INSTALL_DIR/logs/nginx.pid ExecStartPre=$NGINX_INSTALL_DIR/sbin/nginx -t -c $NGINX_INSTALL_DIR/conf/nginx.conf ExecStart=$NGINX_INSTALL_DIR/sbin/nginx -c $NGINX_INSTALL_DIR/conf/nginx.conf ExecReload=$NGINX_INSTALL_DIR/sbin/nginx -s reload ExecStop=$NGINX_INSTALL_DIR/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF #重新加载systemd服务 systemctl daemon-reload #启动Nginx服务 systemctl start nginx #设置Nginx开机自启动 systemctl enable nginx ``` 该脚本主要实现了以下功能: 1. 安装Nginx所需的依赖库,包括pcre-devel、openssl-devel、zlib-devel、gcc和make。 2. 下载Nginx源码包,并解压到指定目录。 3. 进入源码目录,执行configure命令配置Nginx安装路径和编译选项,然后执行make和make install命令进行编译和安装。 4. 添加Nginx服务,并启动Nginx服务。 5. 设置Nginx开机自启动。 需要注意的是,该脚本仅供参考,具体情况需要根据实际需求进行修改。同时,安全性也需要考虑,例如对安装路径和Nginx服务进行权限控制等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值