首页主机资讯Linux Apache2如何安装配置

Linux Apache2如何安装配置

时间2025-11-19 09:19:05发布访客分类主机资讯浏览1075
导读:Linux Apache2 安装与配置 一 安装与启动 在 Debian/Ubuntu 上安装与启动: 安装:sudo apt update && sudo apt install apache2 启动与开机自启:sud...

Linux Apache2 安装与配置

一 安装与启动

  • Debian/Ubuntu 上安装与启动:
    • 安装:sudo apt update & & sudo apt install apache2
    • 启动与开机自启:sudo systemctl start apache2;sudo systemctl enable apache2
  • CentOS/RHEL/Fedora 上安装与启动:
    • 安装:sudo dnf install httpd(或 sudo yum install httpd
    • 启动与开机自启:sudo systemctl start httpd;sudo systemctl enable httpd
  • 验证:浏览器访问 http://服务器IP/http://localhost/,看到默认欢迎页即表示安装成功。

二 配置文件结构与常用命令

  • 配置文件结构(Debian/Ubuntu 常见):
    • 主配置:/etc/apache2/apache2.conf
    • 端口监听:/etc/apache2/ports.conf
    • 可用/已启用模块:/etc/apache2/mods-available//etc/apache2/mods-enabled/
    • 可用/已启用站点:/etc/apache2/sites-available//etc/apache2/sites-enabled/
    • 其他配置片段:/etc/apache2/conf-available//etc/apache2/conf-enabled//etc/apache2/conf.d/
  • 常用管理命令(Debian/Ubuntu):
    • 启用站点:sudo a2ensite 站点名.conf
    • 禁用站点:sudo a2dissite 站点名.conf
    • 启用模块:sudo a2enmod 模块名
    • 禁用模块:sudo a2dismod 模块名
    • 检查配置语法:sudo apache2ctl configtest
    • 重新加载:sudo systemctl reload apache2;重启:sudo systemctl restart apache2
  • 日志位置:/var/log/apache2/access.log/var/log/apache2/error.log

三 基本站点与虚拟主机配置

  • 示例虚拟主机(Debian/Ubuntu,文件:/etc/apache2/sites-available/mysite.conf):
    <
        VirtualHost *:80>
    
        ServerAdmin webmaster@mysite.com
        ServerName mysite.com
        ServerAlias www.mysite.com
        DocumentRoot /var/www/mysite
        ErrorLog ${
    APACHE_LOG_DIR}
    /error.log
        CustomLog ${
    APACHE_LOG_DIR}
        /access.log combined
    
        <
        Directory /var/www/mysite>
        
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        <
        /Directory>
        
    <
        /VirtualHost>
    
    
  • 启用站点并生效:
    • sudo a2ensite mysite.conf
    • sudo systemctl reload apache2
  • 目录权限要点:确保 DocumentRoot 目录及父目录对 www-data(Debian/Ubuntu)或 apache(CentOS/RHEL)可读,必要时设置:
    • sudo chown -R www-data:www-data /var/www/mysite
    • sudo find /var/www/mysite -type d -exec chmod 755 { } ;
    • sudo find /var/www/mysite -type f -exec chmod 644 { } ;
  • 说明:在 CentOS/RHEL/Fedora 中,站点通常直接放在 /etc/httpd/conf.d/ 下(如 mysite.conf),无需 a2ensite,修改后重启 httpd 即可。

四 启用 HTTPS 与防火墙

  • 启用 SSL 模块(Debian/Ubuntu):sudo a2enmod ssl
  • 申请证书(示例):使用 Let’s Encrypt/certbot 获取并自动配置证书(按向导执行,完成后自动启用 HTTPS 虚拟主机)。
  • 手动配置要点(在对应 VirtualHost 中):
    SSLEngine on
    SSLCertificateFile /path/to/certificate.crt
    SSLCertificateKeyFile /path/to/private.key
    
  • 防火墙放行(若启用 UFW):sudo ufw allow 80,443/tcp;其他防火墙(firewalld/iptables)请放行 80/443 端口。

五 性能与安全优化

  • 启用压缩(mod_deflate):
    <
        IfModule mod_deflate.c>
        
        SetOutputFilter DEFLATE
        AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
        DeflateCompressionLevel 5
    <
        /IfModule>
        
    
  • 启用长连接(Keep-Alive):
    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 5
    
  • 设置浏览器缓存(mod_expires):
    <
        IfModule mod_expires.c>
        
        ExpiresActive On
        ExpiresDefault "access plus 1 month"
        ExpiresByType image/jpg "access plus 1 year"
        ExpiresByType image/jpeg "access plus 1 year"
        ExpiresByType image/png "access plus 1 year"
        ExpiresByType text/css "access plus 1 month"
        ExpiresByType application/javascript "access plus 1 month"
    <
        /IfModule>
        
    
  • 安全建议:
    • 隐藏版本信息:在 apache2.conf 或对应虚拟主机中设置 ServerTokens ProdServerSignature Off
    • 限制目录访问:对包含敏感信息的目录使用 Require all denied 或仅允许必要方法
    • 使用 .htaccess 时设置 AllowOverride 为所需的最小集合,避免过度暴露
    • 定期更新系统与软件包,启用日志轮转(如 logrotate)。

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


若转载请注明出处: Linux Apache2如何安装配置
本文地址: https://pptw.com/jishu/750866.html
Debian Java报错怎么解决 HDFS在Ubuntu上如何进行故障排查

游客 回复需填写必要信息