首页主机资讯Apache在Ubuntu上如何安装

Apache在Ubuntu上如何安装

时间2025-12-19 13:40:04发布访客分类主机资讯浏览614
导读:在 Ubuntu 上安装与配置 Apache 的简明步骤 一 安装与启动 更新软件包索引并安装 Apache2(Ubuntu 中包名为 apache2):sudo apt update sudo apt install apache2 -...

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

一 安装与启动

  • 更新软件包索引并安装 Apache2(Ubuntu 中包名为 apache2):
    sudo apt update
    sudo apt install apache2 -y
    
  • 安装完成后会自动启动,检查状态:
    sudo systemctl status apache2
    
    正常会看到 Active: active (running);如未运行可执行:
    sudo systemctl start apache2
    sudo systemctl enable apache2
    
  • 验证:在浏览器访问 http://服务器IP/,出现 Apache 欢迎页即表示安装成功。

二 防火墙放行

  • 如使用 UFW,放行 HTTP/HTTPS:
    sudo ufw allow 'Apache Full'
    sudo ufw status
    
    如仅放行 HTTP:
    sudo ufw allow 80/tcp
    
    注意:云服务器还需在云平台安全组放行 80/443 端口。

三 部署网站与虚拟主机

  • 准备目录与示例页面:
    sudo mkdir -p /var/www/example.com/public_html
    echo "<
        h1>
        Welcome to example.com<
        /h1>
        " | 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.conf
    
    写入:
    <
        VirtualHost *:80>
        
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public_html
        <
        Directory /var/www/example.com/public_html>
        
            Options -Indexes +FollowSymLinks
            AllowOverride All
            Require all granted
        <
        /Directory>
    
        ErrorLog ${
    APACHE_LOG_DIR}
    /example.com-error.log
        CustomLog ${
    APACHE_LOG_DIR}
        /example.com-access.log combined
    <
        /VirtualHost>
        
    
  • 启用站点并生效:
    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
    

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Apache在Ubuntu上如何安装
本文地址: https://pptw.com/jishu/776204.html
Debian系统LibOffice更新日志在哪看 Debian LibOffice性能优化技巧有哪些

游客 回复需填写必要信息