在 Ubuntu 上安装与配置 Apache 的简明步骤

一 安装与启动
sudo apt updatesudo apt install apache2 -ysudo systemctl status apache2正常会看到 Active: active (running);如未运行可执行:sudo systemctl start apache2sudo systemctl enable apache2二 防火墙放行
sudo ufw allow 'Apache Full'sudo ufw status如仅放行 HTTP:sudo ufw allow 80/tcp注意:云服务器还需在云平台安全组放行 80/443 端口。三 部署网站与虚拟主机
sudo mkdir -p /var/www/example.com/public_htmlecho "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/public_html/index.htmlsudo chown -R www-data:www-data /var/www/example.comsudo nano /etc/apache2/sites-available/example.com.conf写入:<VirtualHost *:80>ServerName example.comServerAlias www.example.comDocumentRoot /var/www/example.com/public_html<Directory /var/www/example.com/public_html>Options -Indexes +FollowSymLinksAllowOverride AllRequire all granted</Directory>ErrorLog ${APACHE_LOG_DIR}/example.com-error.logCustomLog ${APACHE_LOG_DIR}/example.com-access.log combined</VirtualHost>sudo a2ensite example.com.confsudo apachectl configtest# 语法检查sudo systemctl reload apache2四 启用 HTTPS 与自动续期
sudo apt install certbot python3-certbot-apache -ysudo certbot --apache -d example.com -d www.example.com五 常用管理与卸载
sudo systemctl restart apache2# 重启sudo systemctl reloadapache2# 重新加载配置sudo apachectl configtest # 语法检查sudo a2enmod deflate cache cache_disk# 启用压缩与缓存模块(按需)sudo a2dissite example.com.conf && sudo systemctl reload apache2# 禁用站点sudo systemctl stop apache2sudo apt remove apache2 -ysudo apt purge apache2 -ysudo apt autoremove -y