温馨提示×

如何用Apache搭建网站服务器

小樊
32
2025-12-16 20:23:43
栏目: 云计算

用 Apache 搭建网站服务器的简明步骤

一 安装与启动

  • Ubuntu/Debian 上:
    • 安装:sudo apt update && sudo apt install apache2 -y
    • 启动与开机自启:sudo systemctl start apache2 && sudo systemctl enable apache2
  • CentOS/RHEL 上:
    • 安装:sudo yum install httpd -y
    • 启动与开机自启:sudo systemctl start httpd && sudo systemctl enable httpd
  • 验证:浏览器访问服务器 IP,出现 Apache 欢迎页即成功。

二 防火墙与网络放行

  • CentOS/RHEL(firewalld)
    • 放行 HTTP/HTTPS:sudo firewall-cmd --permanent --add-service=http;sudo firewall-cmd --permanent --add-service=https
    • 重载:sudo firewall-cmd --reload
  • Ubuntu/Debian(ufw)
    • 放行 Apache:sudo ufw allow ‘Apache Full’
  • 虚拟机场景:将网络设为 桥接模式,并在宿主机/网关放行 80/443,以便外部访问。

三 部署网站与虚拟主机

  • 创建站点目录与示例首页(以域名 example.com 为例):
    • Ubuntu/Debian:
      • sudo mkdir -p /var/www/example.com/public_html
      • echo “

        Hello from Apache

        ” | sudo tee /var/www/example.com/public_html/index.html
      • sudo chown -R $USER:$USER /var/www/example.com/public_html && sudo chmod -R 755 /var/www
    • CentOS/RHEL:
      • sudo mkdir -p /var/www/example.com/public_html
      • echo “

        Hello from Apache

        ” | sudo tee /var/www/example.com/public_html/index.html
      • sudo chown -R apache:apache /var/www/example.com/public_html && sudo chmod -R 755 /var/www
  • 配置虚拟主机:
    • Ubuntu/Debian:
      • 新建:sudo nano /etc/apache2/sites-available/example.com.conf
      • 内容:
        <VirtualHost *:80>
          ServerAdmin webmaster@example.com
          ServerName example.com
          ServerAlias www.example.com
          DocumentRoot /var/www/example.com/public_html
          ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
          CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
          <Directory /var/www/example.com/public_html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
          </Directory>
        </VirtualHost>
        
      • 启用站点与重载:sudo a2ensite example.com.conf && sudo systemctl reload apache2
    • CentOS/RHEL:
      • 新建:sudo nano /etc/httpd/conf.d/example.com.conf(与上段配置相同,注意 DocumentRoot 与 Directory 路径)
      • 语法检查与重启:sudo apachectl configtest && sudo systemctl restart httpd
  • 访问测试:浏览器打开 http://example.comhttp://服务器IP

四 启用 HTTPS 与常用模块

  • 启用 SSL/TLS(Let’s Encrypt + Certbot):
    • Ubuntu/Debian:sudo apt install certbot python3-certbot-apache -y
    • CentOS/RHEL:sudo yum install epel-release && sudo yum install certbot python2-certbot-apache -y
    • 申请并自动配置证书:sudo certbot --apache -d example.com -d www.example.com
    • 自动续期已默认配置(certbot 会添加定时任务)
  • 常用模块:
    • URL 重写:sudo a2enmod rewrite(Debian/Ubuntu);CentOS 编辑 /etc/httpd/conf/httpd.conf 取消注释 LoadModule rewrite_module modules/mod_rewrite.so
    • 压缩与缓存:sudo a2enmod deflate;按需启用 mod_cache/mod_cache_disk
    • 修改后重载:sudo systemctl reload apache2(或 restart httpd

五 安全与性能优化

  • 安全加固:
    • 隐藏版本信息:在配置中加入 ServerTokens ProdServerSignature Off
    • 目录访问:在 中使用 Require all granted 或更细粒度控制;必要时禁用目录列表(Options -Indexes
    • SELinux(CentOS):若网站目录不在默认路径,使用 semanage fcontextrestorecon 设置正确的 httpd_sys_content_t 上下文
  • 性能优化:
    • 长连接:启用 KeepAlive On,合理设置 MaxKeepAliveRequestsKeepAliveTimeout
    • MPM 调优:根据应用选择 prefork/worker/event,并调整 StartServers/MinSpareThreads/MaxRequestWorkers 等参数
    • 压缩与缓存:启用 mod_deflate 对文本资源压缩;按需启用 mod_cache/mod_expires 提升命中率
  • 日志与维护:
    • 访问/错误日志路径:CentOS 默认 /var/log/httpd/,Ubuntu 默认 /var/log/apache2/;可用 tail -f 实时排查
    • 备份与更新:定期备份 /etc/httpd|apache2/var/www,并执行系统/软件包更新

0