Apache和PHP结合,httpd的虚拟主机配置

httpd配置支持php

上次安装httpd2.4对应的配置文件:/usr/local/apache2.4/conf/httpd.conf
编辑配置文件,修改以下4个地方
ServerName
Require all denied
AddType application/x-httpd-php .php
DirectoryIndex index.html index.php

  • 编辑配置文件,去掉ServerName前面的#,启动httpd
[root@test-a ~]# /usr/local/apache2.4/bin/apachectl start
httpd (pid 6328) already running
  • 防火墙打开80端口,测试访问
# Windows终端: telnet 192.168.77.134  80
正在连接192.168.77.134...无法打开到主机的连接。 在端口 80: 连接失败

[root@test-a ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

# 再次telnet可以访问了
  • 如果打开80端口,访问出现403错误,编辑配置把Require all denied 改为 Require all granted
# 改完配置需要重新加载配置,加载前最好检测配置是否正确
[root@test-a ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK

# 加载配置
[root@test-a ~]# /usr/local/apache2.4/bin/apachectl graceful
  • 配置支持PHP,搜AddType,在"AddType application/x-gzip .gz .tgz"这行下面添加"AddType application/x-httpd-php .php"
  • 增加索引页,搜索DirectoryIndex,找到如下内容,增加了index.php
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
  • 在目录/usr/local/apache2.4/htdocs/下编写php文件,尝试访问,例如1.php,访问http:ip/1.php

httpd的默认虚拟主机

  • 一台服务器可以访问多个网站,每个网站都是一个虚拟主机
  • 概念:域名(主机名)、DNS、解析域名、hosts 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机
vim /usr/local/apache2/conf/httpd.conf # 搜索httpd-vhost,去掉#,使支持虚拟主机

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf # 改为如下
<VirtualHost *:80>
    ServerAdmin [email protected]  # 管理员邮箱,可以不用配置
    DocumentRoot "/tmp/web-default" # 网站资源目录
    ServerName test.com  # 域名
    ServerAlias www.test.com # 域名别名
    ErrorLog "logs/test.com-error_log"  # 错误日志
    CustomLog "logs/test.com-access_log" common # 访问日志
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/tmp/web-1/"
    ServerName www.123.com
</VirtualHost>
[root@test-a apache2.4]# bin/apachectl -t
Syntax OK
[root@test-a apache2.4]# bin/apachectl graceful

编写测试文件1.php放到/tmp/web-default目录下,2.php放到/tmp/web-1/下, 访问测试

[root@test-a apache2.4]# curl  -x 192.168.77.134:80  test.com
1.php
[root@test-a apache2.4]# curl  -x 192.168.77.134:80  www.test.com
1.php
[root@test-a apache2.4]# curl  -x 192.168.77.134:80  www.123.com
2.php
[root@test-a apache2.4]# curl  -x 192.168.77.134:80 www.example.com # 访问默认虚拟主机
1.php

猜你喜欢

转载自my.oschina.net/u/996931/blog/2876397