Skip to content

Centos 和 RockyLinux 安装 nginx

在 CentOS 和 Rocky Linux 上安装 Nginx 并使其在后台启动的过程相对简单,可以通过几个简单的步骤完成。这里,我将提供适用于 CentOS 7/8 和 Rocky Linux 的详细指导。我们将使用 yum(对于 CentOS 7)或 dnf(对于 CentOS 8 和 Rocky Linux)来进行安装。

步骤 1: 添加 EPEL 仓库

Nginx 不包含在 CentOS 或 Rocky Linux 的默认仓库中,但它可在 Extra Packages for Enterprise Linux (EPEL) 仓库中找到。首先,需要安装 EPEL 仓库:

bash
sudo yum install epel-release

对于使用 dnf 的系统(如 Rocky Linux 和 CentOS 8),这个命令仍然有效,因为 yum 命令在这些系统上是 dnf 的别名。

步骤 2: 安装 Nginx

一旦添加了 EPEL 仓库,就可以安装 Nginx:

bash
sudo yum install nginx

或者,如果你在使用 dnf

bash
sudo dnf install nginx

步骤 3: 启动 Nginx 服务

安装完 Nginx 后,需要启动 Nginx 服务:

bash
sudo systemctl start nginx

步骤 4: 配置 Nginx 自动启动

为了确保在系统启动时自动启动 Nginx,需要启用 Nginx 服务:

bash
sudo systemctl enable nginx

步骤 5: 检查 Nginx 状态

可以检查 Nginx 服务的状态,以确认它是否正在运行:

bash
sudo systemctl status nginx

步骤 6: 端口放行 - 配置防火墙(如果需要)

云服务器端口放行

忽略这里详情的:

Details

如果你的系统正在运行防火墙,如 firewalld,并且你想从外部访问 Nginx,需要开放 HTTP 和 HTTPS 端口:

bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

步骤 7: 测试 Nginx 安装

打开你的网络浏览器,访问 http://your_server_ip/ ,你应该能看到 Nginx 的默认欢迎页面。这表明 Nginx 已经正确安装并正在运行。

以上步骤详细介绍了如何在 CentOS 和 Rocky Linux 系统上安装和启动 Nginx,使其成为后台运行的服务。这使得 Nginx 成为处理你的网站和应用程序请求的理想选择。

nginx 默认的 nginx.conf 以及如何使用 ssh scp 拷贝文件到远程和本地修改 nginx.conf 拷贝到远程的 nginx.conf

默认的 nginx.conf

在 CentOS 或 Rocky Linux 安装 Nginx 后,默认的 nginx.conf 文件通常位于 /etc/nginx/nginx.conf。这个文件包含了 Nginx 服务器的基本配置,如工作进程数、错误日志路径、HTTP 服务器的配置等。默认的 nginx.conf 通常看起来是这样的:

nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

使用 SSH 和 SCP 拷贝文件

若要使用 SSH 和 SCP 在本地和远程服务器之间拷贝文件,你需要有远程服务器的 SSH 访问权限。以下是一些基本的命令,用于拷贝文件和修改 nginx.conf

拷贝本地文件到远程服务器

假设你在本地对 nginx.conf 进行了修改,现在需要将这个文件拷贝到远程服务器的相应位置。可以使用以下命令:

bash
scp /path/to/local/nginx.conf username@remote_server:/etc/nginx/nginx.conf

这里的 /path/to/local/nginx.conf 是你本地修改过的 nginx.conf 文件的路径,username 是你的远程服务器用户名,remote_server 是远程服务器的地址。

拷贝远程文件到本地

如果你需要将远程服务器上的 nginx.conf 文件拷贝到本地进行编辑,可以使用以下命令:

bash
scp username@remote_server:/etc/nginx/nginx.conf /path/to/local/nginx.conf

修改 nginx.conf 并重启 Nginx

完成文件的编辑和拷贝后,你需要重新加载或重启 Nginx 以应用更改:

bash
sudo systemctl restart nginx

或者,如果你不想完全重启服务,可以仅重新加载配置:

bash
sudo systemctl reload nginx

这样,你就能确保更改被正确应用而不会导致服务中断。

以上步骤说明了如何使用 SSH 和 SCP 在本地和远程服务器之间拷贝 nginx.conf 文件,以及如何重启 Nginx 服务以应用配置更改。

如何通过 curl 判断 nginx 是否启动生效

要通过 curl 命令检查 Nginx 是否已启动并生效,你可以简单地对 Nginx 服务器发起一个 HTTP 请求并观察响应。curl 是一个强大的工具,用于从命令行或脚本传输数据,并且它支持多种协议,包括 HTTP、HTTPS、FTP 等。以下是如何使用 curl 来检查 Nginx 服务器状态的一些步骤和技巧:

基本检查

  1. 发送请求到服务器: 打开命令行终端,并输入以下命令:

    bash
    curl -I http://your_server_ip_or_domain/

    这里的 -I 选项让 curl 只获取 HTTP 响应头,而不下载内容。

  2. 分析响应: 查看返回的状态码和头信息。成功的响应通常显示状态码 200 OK。例如:

    HTTP/1.1 200 OK
    Server: nginx/1.18.0 (Ubuntu)
    Date: Mon, 04 Apr 2022 10:00:00 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Mon, 04 Apr 2022 09:30:00 GMT
    Connection: keep-alive
    ETag: "5e1a-5c1b92a3b6400"
    Accept-Ranges: bytes

    如果你看到类似上面的响应,其中 Server 头明确指示了 Nginx 作为服务器,这意味着 Nginx 已经启动并且正在处理请求。

进一步的检查

如果需要进一步验证 Nginx 是否正常运行,你还可以:

  • 检查特定页面或资源: 如果你的 Nginx 服务器托管了特定的页面或资源,你可以直接请求那些资源来看它们是否返回预期内容:

    bash
    curl -s http://your_server_ip_or_domain/somepage.html

    这里的 -s 选项让 curl 在输出中不显示进度和错误信息,使输出更清洁。

  • 使用自定义头部进行请求: 有时候,根据 Nginx 的配置,某些页面或 API 可能要求特定的 HTTP 头才能正确响应。你可以使用 -H 选项来添加这些头部:

    bash
    curl -I -H "Accept-Language: en-US" http://your_server_ip_or_domain/
  • 检查错误代码: 如果服务器配置有误或服务未启动,curl 请求可能会返回如 500 Internal Server Error502 Bad Gateway 等错误代码。这些信息可以帮助你诊断问题。

通过上述方法,你可以有效地使用 curl 来检测和确认 Nginx 服务器的运行状态。这种检查对于确保配置更改已正确应用或排除服务器问题非常有用。