首页主机资讯centos lamp怎样配置虚拟主机

centos lamp怎样配置虚拟主机

时间2026-01-19 07:19:03发布访客分类主机资讯浏览971
导读:CentOS LAMP 虚拟主机配置步骤 一 环境准备与基础服务 安装组件(以 CentOS 7/8 为例,使用 yum/dnf): 安装:httpd、mariadb-server、php、php-mysqlnd(可按需增加 php-g...

CentOS LAMP 虚拟主机配置步骤

一 环境准备与基础服务

  • 安装组件(以 CentOS 7/8 为例,使用 yum/dnf):
    • 安装:httpd、mariadb-server、php、php-mysqlnd(可按需增加 php-gd、php-xml、php-mbstring、php-opcache
    • 启动与开机自启:
      • systemctl start httpd & & systemctl enable httpd
      • systemctl start mariadb & & systemctl enable mariadb
    • 数据库安全初始化:执行 mysql_secure_installation 并按向导设置 root 密码、移除匿名用户、禁止 root 远程登录等。

二 创建目录与权限

  • 为每个站点创建目录(示例使用 /var/www/example.com/public_html):
    • 创建:mkdir -p /var/www/example.com/public_html
    • 授权(让 Apache 读取,且允许部署用户写入):
      • chown -R apache:apache /var/www/example.com
      • chmod -R 755 /var/www
  • 放置测试页(用于验证站点是否可达):
    • echo “

      Success! example.com

      ” > /var/www/example.com/public_html/index.html。

三 配置基于域名的虚拟主机

  • /etc/httpd/conf.d/ 下新建站点配置文件(文件名以 .conf 结尾,例如 example.com.conf):
    • 示例:
      <
          VirtualHost *:80>
          
          ServerName www.example.com
          ServerAlias 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 /var/log/httpd/example.com-error.log
          CustomLog /var/log/httpd/example.com-access.log combined
      <
          /VirtualHost>
          
      
  • 语法检查与生效:
    • apachectl configtest(或 httpd -t)
    • systemctl reload httpd(或 restart)

四 防火墙与 SELinux 放行

  • 防火墙(firewalld)放行 HTTP/HTTPS
    • firewall-cmd --permanent --add-service=http
    • firewall-cmd --permanent --add-service=https
    • firewall-cmd --reload
  • SELinux(如启用):
    • 排查时可临时:setenforce 0(仅测试用途)
    • 生产环境不建议永久关闭,应为自定义目录设置正确的 httpd 上下文(如 semanage fcontext/restorecon),避免权限被 SELinux 拦截。

五 域名解析与测试

  • 域名解析:在域名注册商控制台添加 A 记录example.comwww.example.com 指向服务器 公网 IP
  • 本地测试(无公网 DNS 时):在本地电脑的 hosts 文件添加
    • 服务器IP example.com
    • 服务器IP www.example.com
  • 访问验证:
    • 浏览器访问 http://example.comhttp://www.example.com
    • 或命令行:curl -I http://example.com
  • PHP 连通性测试(可选):在站点目录创建 info.php
    • echo “” > /var/www/example.com/public_html/info.php
    • 访问 http://example.com/info.php 查看 PHP 信息(测试后删除该文件)。

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


若转载请注明出处: centos lamp怎样配置虚拟主机
本文地址: https://pptw.com/jishu/785389.html
centos lamp怎样进行数据备份 centos分卷可以删除吗

游客 回复需填写必要信息