本文将使用 CentOS 7/8/9 作为操作系统,并假设你已经拥有一个具有 sudo 权限的非 root 用户。

使用 Nginx (推荐)
Nginx 以其高并发、低内存消耗和反向代理功能而闻名,是目前网站和 Web 应用的首选。
安装 Nginx
CentOS 官方源中默认包含 Nginx,我们可以直接使用 yum (或 dnf 在 CentOS 8/9 上) 安装。
# 对于 CentOS 7 sudo yum install -y nginx # 对于 CentOS 8/9 sudo dnf install -y nginx
安装完成后,Nginx 不会自动启动,我们需要手动启动它,并设置为开机自启。
# 启动 Nginx 服务 sudo systemctl start nginx # 设置 Nginx 开机自启 sudo systemctl enable nginx
配置防火墙
CentOS 默认有 firewalld 防火墙,我们需要开放 HTTP (80) 和 HTTPS (443) 端口。

# 永久开放 80 (HTTP) 和 443 (HTTPS) 端口 sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https # 重新加载防火墙配置使更改生效 sudo firewall-cmd --reload
测试 Nginx 是否运行成功
你可以通过服务器的 IP 地址在浏览器中访问 Nginx 的默认欢迎页面。
-
查看你服务器的 IP 地址:
ip addr
找到类似
eth0或ens33的网卡,其下的inet后面的就是你的 IP 地址。 -
在浏览器中输入
http://你的服务器IP地址。
(图片来源网络,侵删)
如果看到 "Welcome to nginx!" 的欢迎页面,说明 Nginx 已经成功安装并运行了!
部署你的网站文件
Nginx 的默认网站根目录是 /usr/share/nginx/html,你可以将你的网站文件(index.html)放在这里。
# 进入网站根目录 cd /usr/share/nginx/html # 创建一个简单的测试页面 sudo echo "<h1>我的第一个 Nginx 网站!</h1>" > index.html
再次访问你的 IP 地址,应该就能看到你自定义的页面了。
配置虚拟主机 (Virtual Host)
如果你想在一台服务器上托管多个网站,就需要配置虚拟主机,这通常通过修改 Nginx 的配置文件来实现。
示例:为 example.com 创建一个虚拟主机
-
创建网站目录和配置文件
# 创建网站根目录 sudo mkdir -p /var/www/example.com/html # 创建一个测试页面 sudo echo "<h1>这是 example.com 的首页</h1>" > /var/www/example.com/html/index.html # 创建 Nginx 配置文件 sudo nano /etc/nginx/conf.d/example.com.conf
-
编辑配置文件 在打开的文件中,输入以下内容,请将
your_server_ip替换为你的服务器 IP。server { listen 80; server_name example.com www.example.com; # 你的域名 # 网站根目录 root /var/www/example.com/html; index index.html index.htm; # 访问日志 access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; location / { try_files $uri $uri/ =404; } } -
测试并重载 Nginx 配置
# 检查配置文件语法是否正确 sudo nginx -t # 如果输出 "syntax is ok" 和 "test is successful",则重载配置 sudo systemctl reload nginx
-
配置域名解析 在你的域名注册商处,将
example.com和www.example.com的 A 记录指向你的服务器 IP 地址,等待 DNS 生效后,通过浏览器访问你的域名即可。
使用 Apache (传统选择)
Apache 是历史最悠久、用户最广泛的 Web 服务器,功能非常强大,配置灵活。
安装 Apache
# 对于 CentOS 7 sudo yum install -y httpd # 对于 CentOS 8/9 sudo dnf install -y httpd
启动 Apache 并设置开机自启:
# 启动 Apache 服务 sudo systemctl start httpd # 设置 Apache 开机自启 sudo systemctl enable httpd
配置防火墙
同样需要开放 HTTP 和 HTTPS 端口。
# 永久开放 80 (HTTP) 和 443 (HTTPS) 端口 sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https # 重新加载防火墙配置 sudo firewall-cmd --reload
测试 Apache 是否运行成功
在浏览器中访问 http://你的服务器IP地址,如果看到 "Apache 2 Test Page" 的页面,说明 Apache 已经成功运行。
部署你的网站文件
Apache 的默认网站根目录是 /var/www/html。
# 进入网站根目录 cd /var/www/html # 创建一个简单的测试页面 sudo echo "<h1>我的第一个 Apache 网站!</h1>" > index.html
再次访问 IP 地址,即可看到新页面。
配置虚拟主机
Apache 的虚拟主机配置文件通常位于 示例:为 创建网站目录和配置文件 编辑配置文件
在文件中输入以下内容: 测试并重载 Apache 配置 配置域名解析
同样,在你的域名注册商处配置 A 记录,等待 DNS 生效后访问域名。 对于绝大多数新项目,从 Nginx 开始是一个更现代、更具前瞻性的选择。/etc/httpd/conf.d/ 目录下,以 .conf
example.com 创建一个虚拟主机
# 创建网站根目录
sudo mkdir -p /var/www/example.com/html
# 创建一个测试页面
sudo echo "<h1>这是 example.com 的首页</h1>" > /var/www/example.com/html/index.html
# 创建 Apache 配置文件
sudo nano /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com_error.log
CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>
# 检查配置文件语法
sudo apachectl configtest
# 如果输出 "Syntax OK",则重载配置
sudo systemctl reload httpd
Nginx vs Apache 对比
特性
Nginx
Apache
模型
事件驱动、异步
进程驱动、同步
性能
极高,尤其在处理静态文件和高并发连接时
性能良好,但高并发时通常不如 Nginx
内存占用
低
相对较高
反向代理
非常出色,是其核心优势之一
功能强大,但配置通常比 Nginx 复杂
URL 重写
功能强大,但语法较复杂
非常强大,
.htaccess 文件使用方便
通常通过
fastcgi_pass 将请求传递给 PHP-FPM 等处理通过
mod_php 模块直接在进程中处理,配置简单
配置
配置文件结构清晰,但学习曲线稍陡
配置文件灵活,
.htaccess 对用户非常友好
社区与生态
社区活跃,是现代 Web 应用的首选
社区巨大,历史悠久,资料极其丰富
总结与建议
.htaccess 功能对很多应用非常友好。
