CentOS 7 上使用 wget 安装 Nginx 并设置开机自启

在 CentOS 7 上使用 wget 安装 Nginx 并设置开机自启,你可以按照以下步骤进行操作:

  1. 首先,确保你已经以 root 用户或者具有 sudo 权限的用户身份登录到 CentOS 7。

  2. 安装 Nginx 所需的依赖包。在终端中运行以下命令:

    |---|-----------------------------------------------------------------------------------------|
    | | sudo yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel |

  3. 使用 wget 下载 Nginx 的源代码包。在终端中运行以下命令(请检查是否有更新的版本):

    |---|------------------------------------------------------|
    | | wget http://nginx.org/download/nginx-1.22.1.tar.gz |

    注意:版本号可能随时间变化,请根据实际情况选择合适的版本。

  4. 解压下载的 Nginx 源代码包:

    |---|---------------------------------|
    | | tar -zxvf nginx-1.22.1.tar.gz |

  5. 进入解压后的目录:

    |---|--------------------|
    | | cd nginx-1.22.1/ |

  6. 编译和安装 Nginx:

    |---|------------------------------------------|
    | | ./configure --prefix=/usr/local/nginx |
    | | make |
    | | make install |

  7. 安装完成后,配置 Nginx 为系统服务以便开机自启。创建一个 systemd 服务文件,例如 /usr/lib/systemd/system/nginx.service,内容如下:

    |---|--------------------------------------------------------------------------|
    | | [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 |
    | | ExecStart=/usr/local/nginx/sbin/nginx |
    | | ExecReload=/bin/kill -s HUP $MAINPID |
    | | ExecStop=/bin/kill -s QUIT $MAINPID |
    | | PrivateTmp=true |
    | | |
    | | [Install] |
    | | WantedBy=multi-user.target |

    请确保 PIDFileExecStart 中的路径与你的 Nginx 安装路径一致。

  8. 重新加载 systemd 配置文件:

    |---|--------------------------------|
    | | sudo systemctl daemon-reload |

  9. 启动 Nginx 服务并设置为开机自启:

    |---|-------------------------------|
    | | sudo systemctl start nginx |
    | | sudo systemctl enable nginx |

  10. 检查 Nginx 服务状态:

    |---|-------------------------------|
    | | sudo systemctl status nginx |

相关推荐
fatcoder7 小时前
玩转Nginx 04 — 反向代理:给 nginx 接上后端
前端·后端·nginx
fatcoder9 小时前
玩转Nginx 03 — location 匹配规则:让不同的路径各回各家
前端·后端·nginx
王坊兴11 小时前
Ubuntu 部署产品原型静态服务器:SFTP + Nginx
服务器·nginx·ubuntu
烬羽12 小时前
nginx 里写 localhost 反而 502?一条请求带你彻底搞懂 Docker 端口映射与反向代理
nginx·docker·程序员
小林ixn12 小时前
Docker + Nginx 反向代理:从“我电脑能跑”到“哪里都能跑”
nginx·docker·容器
CDN36012 小时前
源站隐藏实操:出海网站 IP 暴露、被扫描打源,CDN+Nginx 白名单加固方案
网络·tcp/ip·nginx·源站隐藏·cdn防护
运维全栈笔记12 小时前
Ubuntu 24.04 Certbot 自动化SSL证书配置指南
linux·运维·服务器·nginx·ubuntu·自动化·ssl
ly768913 小时前
负载均衡详解:从四层、七层到 LVS、Keepalived、Nginx 与 HAProxy
nginx·负载均衡·lvs
用户938515635071 天前
Docker + Nginx + Node.js:从“我的电脑能跑,你的电脑跑不了”到一键部署
后端·nginx·docker
teak_on_my_way1 天前
外网通过 Nginx 访问 Doris Stream Load:一次 HTTP 307 疑难排查与完整代理方案
运维·数据库·nginx·http