centos7 安装Apache2.4配置多站点目录

安装apache

 
  
  1. $ yum install httpd -y

启动apache

 
  
  1. $ systemctl start httpd.service

查看是否开启成功

 
  
  1. [root@centos7-1 ~] $ ps -ef|grep httpd
  2. root 1739 1 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  3. apache 1740 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  4. apache 1741 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  5. apache 1742 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  6. apache 1743 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  7. apache 1744 1739 0 18:34 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
  8. root 1749 1112 0 18:37 pts/0 00:00:00 grep --color=auto httpd

查看apache端口

 
  
  1. $ netstat -lntup|grep httpd

修改hosts解析

 
  
  1. $ vi /etc/hosts

改成如下内容

 
  
  1. 192.168.56.101 centos7.com www.centos7.com bbs.centos7.com blog.centos7.com

测试访问

 
  
  1. $ curl www.centos7-1.com

配置apache

备份文件

 
  
  1. $ cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.back

配置httpd文件

因为在apache2.4中变化挺大,和nginx一样,可以自定义.conf文件。

在主配置文件中启用虚拟主机

 
  
  1. $ mkdir /etc/httpd/vhost.d/
  2. $ echo "include vhost.d/*.conf"
  3. $ tail -1 /etc/httpd/conf/httpd.conf

配置多站点目录

 
  
  1. $ vi /etc/httpd/vhost.d/name.conf

写入下面的内容

 
  
  1. *:80>
  2. ServerAdmin [email protected]
  3. DocumentRoot "/var/html/www"
  4. ServerName www.centos7.com
  5. ErrorLog "/var/httpd/logs/www-error_log"
  6. CustomLog "/var/httpd/logs/www-access_log" common
  7. /var/html/www/>
  8. Require all granted
  9. *:80>
  10. ServerAdmin [email protected]
  11. DocumentRoot "/var/html/bbs"
  12. ServerName bbs.centos7.com
  13. ErrorLog "/var/httpd/logs/bbs-error_log"
  14. CustomLog "/var/httpd/logs/bbs-access_log" common
  15. /var/html/bbs/>
  16. Require all granted
  17. *:80>
  18. ServerAdmin [email protected]
  19. DocumentRoot "/var/html/blog"
  20. ServerName blog.centos7.com
  21. ErrorLog "/var/httpd/logs/blog-error_log"
  22. CustomLog "/var/httpd/logs/blog-access_log" common
  23. /var/html/blog/>
  24. Require all granted

重启Apache服务

 
  
  1. $ systemctl restart httpd.service

测试web访问

 
  
  1. [root@centos7-1 httpd] $ for name in www bbs blog;do curl $name.centos7.com;done;
  2. http://www.centos7.com
  3. http://bbs.centos7.com
  4. http://blog.centos7.com

你可能感兴趣的:(centos7 安装Apache2.4配置多站点目录)