配置http支持php及虚拟主机
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置http支持php及虚拟主机相关的知识,希望对你有一定的参考价值。
配置httpd支持php
- httpd主配置文件/usr/local/apache2.4/conf/httpd.conf
- vim /usr/local/apache2.4/conf/httpd.conf
修改以下4个地方 ServerName 这个打开后开启httpd后没有警告 Require all denied 这个修改为Require all granted 防止打开虚拟主机配置文件时403 AddType application/x-httpd-php .php 加上这行才可以解析php DirectoryIndex index.html index.php 添加默认索引页 DocumentRoot "/usr/local/apache2.4/htdocs" 这个参数可以定义网站的域名根目录
- /usr/local/apache2.4/bin/apachectl -t //测试语法,改完配置需要先检查下再加载
- /usr/local/apache2.4/bin/apachectl graceful //重新加载配置文件,不重启服务
- iptables -I INPUT -p tcp --dport 80 -j ACCEPT //打开80端口
- netstat -lntp //查看80端口
[[email protected] ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1122/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2094/master
tcp6 0 0 :::3306 :::* LISTEN 2658/mysqld
tcp6 0 0 :::80 :::* LISTEN 2387/httpd
tcp6 0 0 :::22 :::* LISTEN 1122/sshd
tcp6 0 0 ::1:25 :::* LISTEN 2094/master
- 在物理机浏览器上打开主机ip地址能访问
- 打不开ip主页排查思路
-现在物理机上查看Ip能不能ping通,telnet能不能通,再查看虚拟机上有没有打开80端口[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT [[email protected] ~]# iptables -nvL Chain INPUT (policy ACCEPT 58 packets, 3900 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
- 写一个测试php,vim /usr/local/apache2.4/htdocs/1.php
[[email protected] ~]# vim /usr/local/apache2.4/htdocs/1.php
<?php
phpinfo();
?>
- 在物理机浏览器上打开主机ip地址/1.php,可以打开php信息页面,如果不支持php解析的话,访问该文件时会显示源代码。
-
故障排查
-
检查Apache是否已加载PHP模块
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_event_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) reqtimeout_module (shared) filter_module (shared) mime_module (shared) log_config_module (shared) env_module (shared) headers_module (shared) setenvif_module (shared) version_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) dir_module (shared) alias_module (shared) php7_module (shared)
- 查看php模块文件
[[email protected] ~]# ls /usr/local/apache2.4/modules/libphp7.so /usr/local/apache2.4/modules/libphp7.so [[email protected] ~]# ls /usr/local/apache2.4/modules/libphp5.so /usr/local/apache2.4/modules/libphp5.so
- 查看apache配置文件这四项
-
#LoadModule php5_module modules/libphp5.so
LoadModule php7_module modules/libphp7.so
ServerName www.example.com:80
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
Addtype application/x-httpd-php .php
<Directory />
AllowOverride none
Require all granted
</Directory>
- “/usr/local/apache2.4/bin/apachectl -t”检查配置文件是否存在语法错误
- 配置PHP7解析只需要把配置文件中加载的php模块换成php7的,其他相同
- ctrl+r用途:反向搜索并调用执行过的命令。(reverse-i-search)。
使用方法:按ctrl+r后命令行会变成“(reverse-i-search)`‘: ”状态,然后输入使用过的参数,此时会直接显示出相关的命令,回车即可执行该命令。
httpd的默认虚拟主机
- windows定义hosts
- C:\Windows\System32\drivers\etc\hosts
192.168.21.128 www.abc.com www.123.com
- 可以ping检查下是不是解析到你需要的ip
- C:\Windows\System32\drivers\etc\hosts
- 一台服务器可以访问多个网站,每个网站都是一个虚拟主机
- 概念:域名(主机名)、DNS(域名解析系统)、hosts(局域网的域名解析)
- 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机
- vim /usr/local/apache2/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
打开这个选项,启用虚拟主机配置文件/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
- vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
ServerAlias www.abc.com www.123.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.example.com
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
</VirtualHost>
- 创建目录
[[email protected] ~]# mkdir /data/wwwroot [[email protected] ~]# mkdir /data/wwwroot/abc.com [[email protected] ~]# mkdir /data/wwwroot/111.com
- 创建index.php文件
[[email protected] ~]# vim /data/wwwroot/abc.com/index.php [[email protected] ~]# vim /data/wwwroot/111.com/index.php <?php echo "abc.com";
- /usr/local/apache2/bin/apachectl –t
- /usr/local/apache2/bin/apachectl graceful
- 测试虚拟主机
[email protected] ~]# curl -x192.168.16.120:80 abc.com
abc.com[[email protected] ~]# curl -x192.168.16.120:80 111.com
111.com[[email protected] ~]# curl -x192.168.16.120:80 www.dahjdhajabc.com
abc.com[[email protected] ~]#
- 这里的abc.com为默认虚拟主机
扩展
以上是关于配置http支持php及虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章