凌峰创科服务平台

CentOS如何快速搭建HTTP服务器?

使用 Nginx (推荐)

Nginx 是目前最受欢迎的 Web 服务器之一,尤其适合处理静态文件、反向代理和负载均衡。

CentOS如何快速搭建HTTP服务器?-图1
(图片来源网络,侵删)

步骤 1:更新系统

确保您的系统软件包是最新的。

sudo yum update -y

步骤 2:安装 EPEL 仓库

EPEL (Extra Packages for Enterprise Linux) 是一个为 RHEL 和 CentOS 提供高质量额外软件包的项目,Nginx 通常在 EPEL 仓库中。

sudo yum install epel-release -y

步骤 3:安装 Nginx

使用 yum 安装 Nginx。

sudo yum install nginx -y

步骤 4:启动并设置开机自启

安装完成后,启动 Nginx 服务,并设置为系统启动时自动运行。

CentOS如何快速搭建HTTP服务器?-图2
(图片来源网络,侵删)
# 启动 Nginx 服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx

您可以使用以下命令检查 Nginx 的运行状态:

sudo systemctl status nginx

如果看到 active (running),说明服务已成功启动。

步骤 5:配置防火墙

CentOS 默认使用 firewalld 防火墙,需要开放 HTTP (80) 和 HTTPS (443) 端口。

# 永久开放 HTTP 端口
sudo firewall-cmd --permanent --add-service=http
# 永久开放 HTTPS 端口
sudo firewall-cmd --permanent --add-service=https
# 重新加载防火墙规则以应用更改
sudo firewall-cmd --reload

步骤 6:测试 Nginx 默认页面

您可以在浏览器中访问服务器的公网 IP 地址。

CentOS如何快速搭建HTTP服务器?-图3
(图片来源网络,侵删)
# 查看服务器的公网 IP
curl ifconfig.me

在浏览器中输入 http://<您的服务器公网IP>,您应该能看到 Nginx 的欢迎页面,内容为 "Welcome to nginx!"。

步骤 7:部署您的网站

Nginx 的默认网站根目录是 /usr/share/nginx/html,您可以替换其中的内容来部署您的网站。

示例:部署一个简单的静态网页

  1. 创建一个简单的 HTML 文件 进入默认网站目录,并创建一个 index.html 文件。

    cd /usr/share/nginx/html
    sudo vi index.html

    vi 编辑器中,按 i 进入插入模式,然后输入以下内容:

    <!DOCTYPE html>
    <html>
    <head>
        <title>My First Nginx Page</title>
    </head>
    <body>
        <h1>Hello from Nginx on CentOS!</h1>
        <p>This is my custom web page.</p>
    </body>
    </html>

    Esc 键,然后输入 wq 保存并退出。

  2. 重新加载 Nginx 配置

    修改网站文件后,为了让 Nginx 重新加载文件(而不是重启整个服务),可以执行:

    sudo nginx -s reload
  3. 再次访问

    刷新浏览器,您应该能看到刚刚创建的 "Hello from Nginx on CentOS!" 页面了。


使用 Apache (httpd)

Apache 是一个功能非常强大和灵活的 Web 服务器,拥有悠久的历史和海量的文档。

步骤 1:更新系统

sudo yum update -y

步骤 2:安装 Apache

CentOS 的官方软件包仓库中已经包含了 Apache,可以直接安装。

sudo yum install httpd -y

步骤 3:启动并设置开机自启

# 启动 Apache 服务
sudo systemctl start httpd
# 设置开机自启
sudo systemctl enable httpd

检查服务状态:

sudo systemctl status httpd

步骤 4:配置防火墙

同样,需要为 Apache 开放 HTTP 和 HTTPS 端口。

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

步骤 5:测试 Apache 默认页面

在浏览器中访问服务器的公网 IP 地址 http://<您的服务器公网IP>,您应该能看到 Apache 的测试页面,内容为 "Apache HTTP Server Test Page"。

步骤 6:部署您的网站

Apache 的默认网站根目录是 /var/www/html

示例:部署一个简单的静态网页

  1. 创建一个简单的 HTML 文件

    cd /var/www/html
    sudo vi index.html

    vi 编辑器中输入以下内容:

    <!DOCTYPE html>
    <html>
    <head>
        <title>My First Apache Page</title>
    </head>
    <body>
        <h1>Hello from Apache on CentOS!</h1>
        <p>This is my custom web page.</p>
    </body>
    </html>

    保存并退出 (wq)。

  2. 重新加载 Apache 配置

    sudo systemctl reload httpd
  3. 再次访问

    刷新浏览器,您应该能看到 "Hello from Apache on CentOS!" 页面。


Nginx vs Apache:如何选择?

特性 Nginx Apache (httpd)
核心模型 事件驱动 (异步),非阻塞 I/O 进程/线程驱动 (同步),每个连接一个进程/线程
性能 非常高,尤其在处理高并发静态文件时 性能良好,但在高并发场景下不如 Nginx
内存占用 相对较高
反向代理 非常出色,是 Nginx 的强项 功能强大,但配置相对复杂
配置 相对简洁,基于指令块 非常灵活,但配置文件可能更复杂
模块 模块化,但第三方模块较少 海量的第三方模块,非常成熟
兼容性 对老旧 CGI 脚本支持不如 Apache 极佳,几乎兼容所有 Web 标准

总结建议:

  • 新项目、追求高性能和高并发强烈推荐 Nginx,它是现代 Web 应用的首选。
  • 需要运行老旧应用、依赖特定 Apache 模块:可以选择 Apache。
  • 不确定从 Nginx 开始,因为它在大多数场景下都表现出色,并且资源消耗更低。

后续常用操作

  • 查看日志
    • Nginx: /var/log/nginx/access.log (访问日志), /var/log/nginx/error.log (错误日志)
    • Apache: /var/log/httpd/access_log (访问日志), /var/log/httpd/error_log (错误日志)
  • 停止服务
    • sudo systemctl stop nginxsudo systemctl stop httpd
  • 重启服务
    • sudo systemctl restart nginxsudo systemctl restart httpd
  • 禁用开机自启
    • sudo systemctl disable nginxsudo systemctl disable httpd
分享:
扫描分享到社交APP
上一篇
下一篇