<?php echo 123; ?>
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf //ctrtl+z退出下
[1]+ 已停止 vim /usr/local/apache2.4/conf/httpd.conf
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl restart //会看到警告信息
AH00557: httpd: apr_sockaddr_info_get() failed for hanfeng
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[root@hf-01 ~]# fg //回到刚暂停的任务中——>vim /usr/local/apache2.4/conf/httpd.conf
在文件中搜索 /ServerName
然后将 # ServerName www.example.com:80 前的# 去除掉保存退出即可
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl restart
httpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79: 2348 段错误 $HTTPD -k $ARGV
[root@hf-01 ~]#
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl start
[root@hf-01 ~]#
[root@hf-01 ~]# ps aux |grep httpd
root 2453 0.0 0.8 254072 8820 ? Ss 23:52 0:00 /usr/local/apache2.4/bin/httpd -k start
daemon 2454 0.0 0.8 540900 8972 ? Sl 23:52 0:00 /usr/local/apache2.4/bin/httpd -k start
daemon 2455 0.0 0.8 540900 8972 ? Sl 23:52 0:00 /usr/local/apache2.4/bin/httpd -k start
daemon 2456 0.0 0.8 540900 8972 ? Sl 23:52 0:00 /usr/local/apache2.4/bin/httpd -k start
root 2541 0.0 0.0 112676 980 pts/0 R+ 23:54 0:00 grep --color=auto httpd
[root@hf-01 ~]#
7. 若是可以运行 telnet 命令,那出来的则是一个结果,显示80并不通
[root@hf-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
347 32935 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
266 26673 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
266 26673 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
266 26673 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
262 26469 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
等等,只截取了一部分,会发现80端口并没有打开
[root@hf-01 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@hf-01 ~]#
[root@hf-01 ~]# iptables -D INPUT -p tcp --dport 80 -j ACCEPT
[root@hf-01 ~]#
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
然后搜索 /denied ,会看到
<Directory />
AllowOverride none
Require all denied
</Directory>
把 denied 改成 granted
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
是因为DocumentRoot指定了Directory,即如下:
DocumentRoot "/usr/local/apache2.4/htdocs"
<Directory "/usr/local/apache2.4/htdocs">
还有后面的
Require all granted
若是将这里的Require all granted改成Require all denied ,那肯定无法浏览
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]#
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]#
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
搜索 /AddType,然后
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
在这两行下面,增加php一行
AddType application/x-httpd-php .php
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
搜索 /Index ,找到
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
在index.html后加 index.php
显示为
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]#
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]#
[root@hf-01 ~]# vi /usr/local/apache2.4/htdocs/1.php
在文件中写入
<?php
phpinfo();
?>
然后保存退出
http://192.168.74.129/1.php
[root@hf-01 ~]# /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)
php5_module (shared)
[root@hf-01 ~]# ls /usr/local/apache2.4/modules/libphp5.so
/usr/local/apache2.4/modules/libphp5.so
2. 第二项,如果有文件,则没有显示php模块,那么就要去看apache的配置文件中有没有加载 libphp5 这一行配置,在文件中搜索/libphp5 ,如果没有这一行配置,那么肯定也是无法解析的
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
在文件中搜索/libphp5,看是否加载了这一行配置
LoadModule php5_module modules/libphp5.so
3. 第三项,检查配置文件中是否加载了 AddType application/x-httpd-php .php 这一行
- 在 .php 前面有一行空格,需要注意下!不能遗忘!!!——>使用 /usr/local/apache2.4/bin/apachectl -t 可以检查出配置写错了
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
在文件搜索/AddType中关于php这一行配置是否存在
AddType application/x-httpd-php .php
4. 第四项,检查是否加了 index.php
- 在访问一个网站的时候,比如访问www.baidu.com的时候,并不用是www.baidu.com/index.php访问,会发现,两个访问出的页面是相同的,就是因为这里有索引页(或者叫做index页),它默认能跳转到这个页面下去,包括直接输入虚拟机ip的时候,也会访问到 It works!这是访问到的是 index.html ,就是因为我们定义了一个index
[root@hf-01 ~]# ls /usr/local/apache2.4/htdocs/
1.php index.html
[root@hf-01 ~]#
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
在文件中搜索 /libphp7,然后注释掉php5打开php7
# LoadModule php5_module modules/libphp5.so
LoadModule php7_module modules/libphp7.so
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]#
[root@hf-01 ~]# /usr/local/php7/bin/php -i |less
phpinfo()
PHP Version => 7.1.6
System => Linux hf-01 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64
Build Date => Dec 18 2017 22:25:37
Configure Command => './configure' '--prefix=/usr/local/php7' '--with-apxs2=/usr/local/apache2.4/bin/apxs' '--with-config-file-path=/usr/local/php7/etc' '--with-pdo-mysql=/usr/local/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-libxml-dir' '--with-gd' '--with-jpeg-dir' '--with-png-dir' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-bz2' '--with-openssl' '--with-mcrypt' '--enable-soap' '--enable-gd-native-ttf' '--enable-mbstring' '--enable-sockets' '--enable-exif'
Server API => Command Line Interface
Virtual Directory Support => enabled
Configuration File (php.ini) Path => /usr/local/php7/etc
Loaded Configuration File => (none)
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20160303
PHP Extension => 20160303
Zend Extension => 320160303
Zend Extension Build => API320160303,TS
等等等,只截取了一部分
</VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com </VirtualHost> - /usr/local/apache2/bin/apachectl –t - /usr/local/apache2/bin/apachectl graceful ## 默认虚拟主机 - 默认虚拟主机,可以理解成Apache(也就是httpd),一个服务下面跑多了多个网站,跑了多个域名 - 例子,假如在服务器上,它既能访问百度,又能访问谷歌,这是两个不同的网站,但同时都在一台服务器运行着,就用了一个httpd的服务,这个就是一个网站多个域名,每一个域名对着一个虚拟主机 ## 更改hosts - 在windows下去写hosts 1. hostsl路径地址,这个和linux下的hosts类似 1. 打开物理机C盘 2. 然后选择Windows 3. 再选择System32 4. 选择dirvers 5. 选择etc 6. 选择hosts
192.168.202.130 www.abc.com www.123.com
# 192.168.202.130 www.abc.com www.123.com
192.168.202.130 www.abc.com www.123.com
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
搜索/extra 关键词,这一行就做虚拟主机
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
把#Include conf/extra/httpd-vhosts.conf前面的 #号 去掉 Include conf/extra/httpd-vhosts.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //虚拟主机配置文件
在打开配置文件,会发现有两个<VirtualHost *:80> ,每一个<VirtualHost >都是一对出现的,每一个<VirtualHost >都代表着一个主机,一个主机就是一个网站
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com //定义管理员的邮箱——>可删除
DocumentRoot "/usr/local/apache2.4/docs/dummy-host.example.com" //定义网站的根目录在哪里
ServerName dummy-host.example.com //服务器名字
ServerAlias www.dummy-host.example.com //定义别名,别名就是一个网站可以有多个域名访问,比如可以是abc.com访问,也可以是www.abc.com访问,访问的内容是一致的
ErrorLog "logs/dummy-host.example.com-error_log" //错误日志
CustomLog "logs/dummy-host.example.com-access_log" common //访问日志
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/local/apache2.4/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
<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>
然后保存退出
[root@hf-01 ~]# mkdir /data/wwwroot/
[root@hf-01 ~]# mkdir /data/wwwroot/abc.com
[root@hf-01 ~]# mkdir /data/wwwroot/111.com
[root@hf-01 ~]#
[root@hf-01 ~]# vim /data/wwwroot/abc.com/index.php
在文件中写入
<?php
echo "abc.com";
并保存退出
[root@hf-01 ~]# vim /data/wwwroot/111.com/index.php
在文件中写入
<?php
echo "111.com";
并保存退出
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]#
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]#
[root@hf-01 ~]# curl -x192.168.74.129:80 abc.com
abc.com[root@hf-01 ~]# curl -x192.168.74.129:80 www.abcd.com
abc.com[root@hf-01 ~]# curl -x192.168.74.129:80 www.abcde.com
abc.com[root@hf-01 ~]#
abc.com[root@hf-01 ~]# curl -x192.168.74.129:80 www.example.com
111.com[root@hf-01 ~]#