阅读量:3
在 Ubuntu 上安装与配置 Apache 的简明步骤
一 安装与启动
- 更新软件包索引并安装 Apache2(Ubuntu 中包名为 apache2):
sudo apt update sudo apt install apache2 -y - 安装完成后会自动启动,检查状态:
正常会看到 Active: active (running);如未运行可执行:sudo systemctl status apache2sudo systemctl start apache2 sudo 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_html echo "Welcome to example.com
" | sudo tee /var/www/example.com/public_html/index.html sudo chown -R www-data:www-data /var/www/example.com - 创建虚拟主机配置:
写入:sudo nano /etc/apache2/sites-available/example.com.confServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html Options -Indexes +FollowSymLinks AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined - 启用站点并生效:
sudo a2ensite example.com.conf sudo apachectl configtest # 语法检查 sudo systemctl reload apache2 - 访问测试:浏览器打开 http://example.com(或服务器 IP)。
四 启用 HTTPS 与自动续期
- 安装 Certbot 并获取并配置证书(将域名替换为你的实际域名):
sudo apt install certbot python3-certbot-apache -y sudo certbot --apache -d example.com -d www.example.com - Certbot 会自动修改虚拟主机并添加 自动续期 的定时任务,证书即将到期会自动续签。
五 常用管理与卸载
- 常用管理命令:
sudo systemctl restart apache2 # 重启 sudo systemctl reload apache2 # 重新加载配置 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 apache2 sudo apt remove apache2 -y - 彻底卸载(连同配置与数据):
sudo apt purge apache2 -y sudo apt autoremove -y
以上就是关于“Apache在Ubuntu上如何安装”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm