CentOS Apache2 虚拟主机如何配置

在 CentOS 系统中配置 Apache2 虚拟主机,你需要遵循以下步骤:

  1. 安装 Apache 服务:
sudo yum install httpd
  1. 启动 Apache 服务并设置开机启动:
sudo systemctl start httpd
sudo systemctl enable httpd
  1. 创建一个新的虚拟主机配置文件。以虚拟主机的域名或项目目录为例,创建一个名为 yourdomain.conf 的文件:
sudo vi /etc/httpd/conf.d/yourdomain.conf
  1. yourdomain.conf 文件中,添加以下虚拟主机配置:
:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain

    
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    

    ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined

yourdomain.com 替换为你的域名,将 /var/www/yourdomain 替换为你的项目目录。

  1. 创建项目目录并设置正确的权限:
sudo mkdir -p /var/www/yourdomain
sudo chown apache:apache /var/www/yourdomain
  1. 重启 Apache 服务以应用更改:
sudo systemctl restart httpd
  1. 更新 /etc/hosts 文件(可选),以便在本地计算机上解析你的域名:
sudo vi /etc/hosts

在文件末尾添加以下行:

127.0.0.1 yourdomain.com www.yourdomain.com

yourdomain.com 替换为你的域名。

现在,你应该可以通过访问 http://yourdomain.com(或 http://www.yourdomain.com)在浏览器中查看你的项目。

注意:如果你打算使用 SSL/TLS 加密通信,请确保已安装并配置了 Certbot 以及相应的证书。在这种情况下,你需要为虚拟主机配置文件添加 SSL 配置,并监听 443 端口。