最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Apache在Ubuntu上安装方法
时间:2026-07-24 08:47:48 编辑:袖梨 来源:一聚教程网
在 Ubuntu 上安装与配置 Apache 的简明步骤

一 安装与启动
- 更新软件包索引并安装 Apache2(Ubuntu 中包名为 apache2):
sudo apt updatesudo apt install apache2 -y - 安装完成后会自动启动,检查状态:
正常会看到 Active: active (running);如未运行可执行:sudo systemctl status apache2sudo systemctl start apache2sudo systemctl enable apache2 - 验证:在浏览器访问 http://服务器IP/,出现 Apache 欢迎页即表示安装成功。
二 防火墙放行
- 如使用 UFW,放行 HTTP/HTTPS:
如仅放行 HTTP:sudo ufw allow 'Apache Full'sudo ufw status
注意:云服务器还需在云平台安全组放行 80/443 端口。sudo ufw allow 80/tcp
三 部署网站与虚拟主机
- 准备目录与示例页面:
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.com - 创建虚拟主机配置:
写入:sudo 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 - 访问测试:浏览器打开 http://example.com(或服务器 IP)。
四 启用 HTTPS 与自动续期
- 安装 Certbot 并获取并配置证书(将域名替换为你的实际域名):
sudo apt install certbot python3-certbot-apache -ysudo certbot --apache -d example.com -d www.example.com - Certbot 会自动修改虚拟主机并添加 自动续期 的定时任务,证书即将到期会自动续签。
五 常用管理与卸载
- 常用管理命令:
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# 禁用站点 - 日志路径:
- 访问日志:/var/log/apache2/access.log
- 错误日志:/var/log/apache2/error.log
- 卸载(保留配置与数据):
sudo systemctl stop apache2sudo apt remove apache2 -y - 彻底卸载(连同配置与数据):
sudo apt purge apache2 -ysudo apt autoremove -y
相关文章
- hbase压缩对性能的影响 07-24
- hbase snappy怎样配置 07-24
- hbase压缩怎样选择算法 07-24
- hbase有哪些压缩方法 07-24
- hadoop与flink性能对比 07-24
- hadoop与flink集成方式 07-24