在 CentOS 上使用 Apache 以下是完整的 Apache 部署方案

完全可以!在 CentOS 上使用 Apache 替代 Nginx 是一个很好的选择。以下是完整的 Apache 部署方案:

## 1. 安装 Apache (httpd)

```bash
# 安装 Apache
sudo yum install httpd -y
# 启动 Apache
sudo systemctl start httpd
sudo systemctl enable httpd
# 检查状态
sudo systemctl status httpd
```

## 2. 配置防火墙

```bash
# 启动防火墙(如果未启动)
sudo systemctl start firewalld
sudo systemctl enable firewalld
# 开放 HTTP 和 HTTPS 端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# 验证
sudo firewall-cmd --list-all
```

## 3. 部署你的 dist 文件

```bash
# 创建网站目录
sudo mkdir -p /var/www/html/mysite
# 上传你的 dist 文件
sudo cp -r /path/to/your/dist/* /var/www/html/mysite/
# 设置权限
sudo chown -R apache:apache /var/www/html/mysite
sudo chmod -R 755 /var/www/html/mysite
```

## 4. 配置 Apache 虚拟主机

### 创建虚拟主机配置
```bash
sudo vim /etc/httpd/conf.d/mysite.conf
```
```apache
<VirtualHost *:80>
ServerName inthost.cdwgsbyy.com
DocumentRoot /var/www/html/mysite

# 日志文件
ErrorLog /var/log/httpd/mysite_error.log
CustomLog /var/log/httpd/mysite_access.log combined

# 目录权限
<Directory "/var/www/html/mysite">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted

# 前端路由支持
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</Directory>

# 静态资源缓存
<FilesMatch ".(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$">
Header set Cache-Control "max-age=31536000, public, immutable"
</FilesMatch>
</VirtualHost>
```

## 5. 启用必要的模块

```bash
# 启用 rewrite 模块(前端路由需要)
sudo sed -i 's/AllowOverride None/AllowOverride All/g' /etc/httpd/conf/httpd.conf
# 或者手动启用模块
sudo vim /etc/httpd/conf/httpd.conf
```
确保以下行没有被注释:
```apache
LoadModule rewrite_module modules/mod_rewrite.so
```

## 6. 测试并重启 Apache

```bash
# 测试配置文件语法
sudo httpd -t
# 重启 Apache
sudo systemctl restart httpd
# 检查状态
sudo systemctl status httpd
```

## 7. 配置 SSL/HTTPS(可选)

```bash
# 安装 mod_ssl
sudo yum install mod_ssl -y
# 创建 SSL 虚拟主机
sudo vim /etc/httpd/conf.d/mysite-ssl.conf
```
```apache
<VirtualHost *:443>
ServerName inthost.cdwgsbyy.com
DocumentRoot /var/www/html/mysite

# SSL 配置
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/inthost.cdwgsbyy.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/inthost.cdwgsbyy.com.key
SSLCertificateChainFile /etc/pki/tls/certs/inthost.cdwgsbyy.com.ca-bundle

# 安全头部
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Header always set X-Content-Type-Options nosniff
Header always set X-Frame-Options DENY
Header always set X-XSS-Protection "1; mode=block"

# 目录权限
<Directory "/var/www/html/mysite">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted

RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</Directory>

# 静态资源缓存
<FilesMatch ".(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$">
Header set Cache-Control "max-age=31536000, public, immutable"
</FilesMatch>
</VirtualHost>
```

## 8. 创建 .htaccess 文件(前端路由)

如果上面的重写规则不工作,可以在网站根目录创建 `.htaccess` 文件:
```bash
sudo vim /var/www/html/mysite/.htaccess
```
```apache
RewriteEngine On
RewriteBase /
# 如果请求的不是真实文件
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 重写到 index.html
RewriteRule . /index.html [L]
```

## 9. 验证部署

```bash
# 测试 Apache 是否运行
curl -I http://localhost
# 测试你的网站
curl http://139.155.84.244
# 查看 Apache 日志
sudo tail -f /var/log/httpd/mysite_access.log
sudo tail -f /var/log/httpd/mysite_error.log
```

## 10. Apache 常用管理命令

```bash
# 启动
sudo systemctl start httpd
# 停止
sudo systemctl stop httpd
# 重启
sudo systemctl restart httpd
# 重新加载配置
sudo systemctl reload httpd
# 查看状态
sudo systemctl status httpd
# 测试配置
sudo httpd -t
```

## 11. 重大目录和文件

- **配置文件**: `/etc/httpd/conf/httpd.conf`
- **虚拟主机配置**: `/etc/httpd/conf.d/`
- **网站文件**: `/var/www/html/`
- **日志文件**: `/var/log/httpd/`
- **模块目录**: `/etc/httpd/conf.modules.d/`

## 12. 与 Nginx 的对比

**Apache 优势:**
- `.htaccess` 文件支持,更灵活的目录级配置
- 模块系统更成熟
- 对 PHP 支持更好(如果你需要)
- 配置语法对新手更友善
**Nginx 优势:**
- 高并发性能更好
- 内存占用更少
- 反向代理配置更简洁

## 13. 如果同时有 Nginx 和 Apache

如果系统中已经有 Nginx,确保它们使用不同端口:
```bash
# 修改 Apache 端口
sudo vim /etc/httpd/conf/httpd.conf
```
将:
```apache
Listen 80
```
改为:
```apache
Listen 8080
```
然后重启 Apache:
```bash
sudo systemctl restart httpd
```
目前你可以通过不同端口访问:
- **Nginx**: `http://139.155.84.244:80`
- **Apache**: `http://139.155.84.244:8080`

Apache 是一个成熟稳定的选择,特别适合传统的 Web 应用部署。按照以上步骤配置,你的 dist 文件应该可以正常访问了。

© 版权声明

相关文章

暂无评论

none
暂无评论...