Centos7 Apache配置文件解析以及配置虚拟主机vhost

apache服务器配置文件部分参数解析:

ServerRoot “/etc/httpd“                        //服务配置文件目录
PidFile run/httpd.pid		                   //PID文件
Listen 80			                           //默认监听端口
Include conf.modules.d/*.conf		           //包含模块目录的配置文件
User apache			                           //启动用户
Group apache		        	               //启动组
ServerAdmin root@localhost	                   //管理员邮箱
ServerName www.example.com:80	               //域名主机名
DocumentRoot “/var/www/html“	               //默认主页存放目录
DirectoryIndex index.html index.html.var	   //索引文件
<Directory "/var/www">                         //规定网站根目录的位置
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
ErrorLog logs/error_log			               //错误日志
CustomLog logs/access_log combined	           //访问日志
AddDefaultCharset UTF-8                        //默认字符集
IncludeOptional conf.d/*.conf                  //包含了 conf.d/*.conf 的配置文件
 
<IfModule dir_module>
    DirectoryIndex index.html                  //默认索引
</IfModule>

配置Apache虚拟主机:

配置目标:

虚拟机1:www.gg.com
虚拟机2:www.bing2.com

1.创建虚拟主机文件目录
  根据自己在Apache配置文件里设定的目录下创建你的虚拟主机目录
没做修改默认一般在 /var/www/html/
2.创建虚拟主机配置文件(把2个网站的配置文件写入vhost)
   vim /etc/httpd/conf.d/vhost.conf(目录根据实际情况)
网站www.gg.com配置文件如下:

 <VirtualHost *:80>
DocumentRoot /var/www/html/mysql
ServerName www.gg.com
<Directory /var/www/html/mysql>
 Options Indexes FollowSymLinks
 AllowOverride All
 Order Allow,Deny
 Allow from all
</Directory>
</VirtualHost>

网站www.bing2.com配置文件如下:

 <VirtualHost *:80>
DocumentRoot /var/www/html/wordpress
ServerName www.gg.com
<Directory /var/www/html/ wordpress >
 Options Indexes FollowSymLinks
 AllowOverride All
 Order Allow,Deny
 Allow from all
</Directory>
</VirtualHost>

提示:可以用apachectl -t 检查下语法是否有误。
systemctl restart httpd #重启apache
完成上面配置后就可以了
3.修改客户端主机的hosts文件,以便能解析域名
  在Centos下访问需要修改/etc/hosts的文件写入:

192.168.31.197(自己虚拟机的ip)  www.gg.com
192.168.31.197(自己虚拟机的ip)  www.pp.com

在windows下访问也需要修改hosts文件,hosts在windows环境下的路径为C:\Windows\System32\drivers\etc\。添加上面相同的内容:然后就可以输入域名访问啦
遇到的问题:
如果遇到设置好虚拟主机后在Centos下无法访问自己 127.0.0.1 那么请在vhost添加

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "你的apache根目录"
</VirtualHost>

并且在主配置文件注释掉ServerName 就可以正常访问了
  文章是在自己入门很久以后把自己的笔记总结起来最后归纳出来自己遇到的问题,希望能帮到需要的人
            
                宝剑锋从磨砺出

                梅花香自苦寒来

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章