Files
chill_notes/Linux/AliYun/阿里云配置Apache.md
2026-04-21 20:33:24 +08:00

154 lines
2.4 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 阿里云配置 Apache
> Ubuntu Apache2 配置指南
---
## 安装 Apache
```bash
sudo apt-get update
sudo apt-get install apache2 -y
```
---
## 配置文件
### 重要文件位置
| 文件 | 说明 |
|------|------|
| `/etc/apache2/apache2.conf` | 主配置文件 |
| `/etc/apache2/sites-available/` | 站点配置目录 |
| `/etc/apache2/sites-enabled/` | 已启用站点 |
| `/etc/apache2/ports.conf` | 端口配置 |
> ⚠️ **注意**Apache2 用 `apache2.conf`,不是传统的 `httpd.conf`
---
## 基本配置
### 1. 配置 apache2.conf
```bash
sudo vim /etc/apache2/apache2.conf
```
添加:
```apache
ServerName localhost
DirectoryIndex index.html index.htm index.php
```
### 2. 设置默认字符集
```apache
AddDefaultCharset UTF-8
```
### 3. 设置监听端口
```apache
Listen 80 # 监听 80 端口
Listen 8080 # 监听 8080 端口
```
### 4. 创建虚拟目录
```apache
Alias /down "/software/download"
Alias /ftp "/var/ftp"
```
### 5. 设置目录权限
```apache
<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
```
---
## 配置站点
### 1. 编辑默认站点
```bash
sudo vim /etc/apache2/sites-available/000-default.conf
```
```apache
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```
### 2. 启用站点
```bash
sudo a2ensite 000-default.conf
sudo a2enmod rewrite
```
---
## 常用命令
```bash
# 启动/停止/重启
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
# 或
sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 restart
# 查看状态
sudo systemctl status apache2
# 启用/禁用站点
sudo a2ensite site-name
sudo a2dissite site-name
```
---
## 测试配置
```bash
# 测试配置文件语法
sudo apache2ctl configtest
# 查看已启用模块
apache2ctl -M
```
---
## 安全建议
1. 禁用不必要的模块
2. 隐藏 Apache 版本信息
3. 启用 HTTPS (Let's Encrypt)
4. 配置防火墙只开放 80/443
---
> 参考:[百度经验 - Ubuntu Apache2 配置](https://jingyan.baidu.com/article/9158e0006581d1a2541228b5.html)