温馨提示×

如何在debian部署apache

小樊
43
2026-10-27 00:13:11
栏目: 智能运维

下面以 Debian 11 / 12 为例,介绍如何安装和部署 Apache HTTP Server。


一、更新系统

sudo apt update
sudo apt upgrade -y

二、安装 Apache

sudo apt install apache2 -y

安装完成后,Apache 会自动启动。


三、检查 Apache 状态

sudo systemctl status apache2

如果看到 active (running) 说明运行正常。

常用命令:

sudo systemctl start apache2    # 启动
sudo systemctl stop apache2     # 停止
sudo systemctl restart apache2  # 重启
sudo systemctl enable apache2   # 开机自启

四、防火墙配置(如有启用)

如果使用了 ufw:

sudo ufw allow 80
sudo ufw allow 443

放行后测试:

sudo ufw status

五、测试访问

在浏览器中访问:

http://服务器IP

看到 Apache2 Debian Default Page 即部署成功。


六、网站目录说明

  • 默认网站根目录:
/var/www/html
  • 默认配置文件:
/etc/apache2/sites-available/000-default.conf

七、部署自己的网站

1. 放置网页文件

sudo rm /var/www/html/index.html
sudo cp -r your_project/* /var/www/html/

2. 修改权限(可选)

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

八、配置虚拟主机(可选)

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.conf
sudo nano /etc/apache2/sites-available/example.conf

示例:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example

    <Directory /var/www/example>
        AllowOverride All
    </Directory>
</VirtualHost>

启用站点:

sudo a2ensite example.conf
sudo systemctl reload apache2

九、启用 HTTPS(推荐)

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

如果你需要:

  • 部署 PHP / Python / Node
  • 配置 反向代理
  • 使用 Docker 部署 Apache

可以继续告诉我你的具体需求。

0 踩