温馨提示×

如何用Apache2搭建网站

小樊
44
2025-11-27 04:01:43
栏目: 云计算

用 Apache2 搭建网站的标准流程

一 准备与安装

  • 更新系统并安装 Web 服务器:
    • Ubuntu/Debian:
      sudo apt update
      sudo apt install apache2 -y
      sudo systemctl start apache2
      sudo systemctl enable apache2
      
    • CentOS/RHEL(httpd 即 Apache):
      sudo yum install httpd -y    # CentOS 7
      # 或
      sudo dnf install httpd -y    # CentOS 8+/RHEL 8+
      sudo systemctl start httpd
      sudo systemctl enable httpd
      
  • 访问测试:在浏览器打开服务器 IP 或域名,看到默认欢迎页即表示安装成功。

二 部署静态网站

  • 准备站点目录与示例页面:
    # Ubuntu/Debian 常用目录
    sudo mkdir -p /var/www/mywebsite
    echo "<h1>Hello, Apache2</h1>" | sudo tee /var/www/mywebsite/index.html
    
    # CentOS/RHEL 建议将属主设为 Apache 运行用户
    sudo chown -R apache:apache /var/www/mywebsite
    sudo chmod -R 755 /var/www/mywebsite
    
  • 访问测试:打开 http://服务器IP/http://域名/ 查看页面内容。

三 配置虚拟主机与目录权限

  • Ubuntu/Debian(基于 sites-available/sites-enabled):
    1. 新建站点配置:
      sudo nano /etc/apache2/sites-available/mywebsite.conf
      
      示例内容(将 mywebsite.com 替换为你的域名):
      <VirtualHost *:80>
          ServerAdmin webmaster@mywebsite.com
          ServerName mywebsite.com
          ServerAlias www.mywebsite.com
          DocumentRoot /var/www/mywebsite
          ErrorLog ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      
          <Directory /var/www/mywebsite>
              Options Indexes FollowSymLinks
              AllowOverride All
              Require all granted
          </Directory>
      </VirtualHost>
      
    2. 启用站点并(可选)禁用默认站点:
      sudo a2ensite mywebsite.conf
      sudo a2dissite 000-default.conf    # 可选
      sudo systemctl reload apache2
      
  • CentOS/RHEL(基于 conf.d/*.conf):
    1. 新建站点配置:
      sudo nano /etc/httpd/conf.d/mywebsite.conf
      
      示例内容:
      <VirtualHost *:80>
          ServerName mywebsite.com
          DocumentRoot /var/www/mywebsite
      
          <Directory /var/www/mywebsite>
              Options Indexes FollowSymLinks
              AllowOverride All
              Require all granted
          </Directory>
      </VirtualHost>
      
    2. 使配置生效:
      sudo systemctl reload httpd
      
  • 要点:为站点目录设置 Require all granted,并在需要时开启 AllowOverride All 以支持 .htaccess

四 防火墙与端口放行

  • Ubuntu/Debian(UFW):
    sudo ufw allow 'Apache Full'    # 同时放行 80/443
    
  • CentOS/RHEL(firewalld):
    sudo firewall-cmd --permanent --zone=public --add-service=http
    sudo firewall-cmd --permanent --zone=public --add-service=https
    sudo firewall-cmd --reload
    
  • 云服务器还需在控制台安全组放行 80/443 端口。

五 启用 HTTPS 与常见问题排查

  • 启用 HTTPS(Let’s Encrypt/Certbot):
    # Ubuntu/Debian
    sudo apt install certbot python3-certbot-apache -y
    sudo certbot --apache -d mywebsite.com -d www.mywebsite.com
    
    # CentOS/RHEL(如系统默认 python 为 Python 2,使用 python2-certbot-apache)
    sudo yum install certbot python2-certbot-apache -y    # 或 dnf
    sudo certbot --apache -d mywebsite.com -d www.mywebsite.com
    
    按提示选择是否重定向 HTTP→HTTPS,证书将自动续期。
  • 常见问题快速排查:
    • 检查服务状态:sudo systemctl status apache2(或 httpd
    • 查看错误日志:sudo tail -f /var/log/apache2/error.log(Ubuntu/Debian),或 /var/log/httpd/error_log(CentOS/RHEL)
    • 403 权限问题:确认目录 Require all granted,属主与权限正确(如 apache:apache755
    • 域名访问异常:确认 DNS A/CNAME 解析正确、云安全组与防火墙放行 80/443

0