Apache和PHP结合
1、先禁用之前的php7的模块
[root@centos7 ~]# cat /usr/local/apache2.4/conf/httpd.conf| grep 'php'
LoadModule php5_module modules/libphp5.so
#LoadModule php7_module modules/libphp7.so
2、查看下是否禁用php7模块
[root@centos7 ~]# /usr/local/apache2.4/bin/apachectl -M
rewrite_module (shared)
php5_module (shared) #只有一个,说明禁用了
Syntax OK
3、访问下apache是否工作,可以看到It works!说明apache已经正常
<a href="https://s1.51cto.com/oss/201711/09/a2c3409799a176addea5d8aaafb37acb.png-wh_500x0-wm_3-wmp_4-s_841585805.png" target="_blank"></a>
4、添加php服务解析
[root@centos7 ~]# vi /usr/local/apache2.4/conf/httpd.conf
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php #添加下这行后,apache才能解析
<b>5、</b>/usr/local/apache2.4/bin/apachectl graceful<b></b>
6、
[root@centos7 ~]# vi /usr/local/apache2.4/htdocs/1.php
<?php
phpinfo();
<a href="https://s4.51cto.com/oss/201711/09/0315499068a0c12b1a69327ee52ca7b5.png-wh_500x0-wm_3-wmp_4-s_2062284237.png" target="_blank"></a>
7、vi /usr/local/apache2.4/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.html index.php #新增这个
</IfModule>
重载配置文件
/usr/local/apache2.4/bin/apachectl graceful
mv /usr/local/apache2.4/htdocs/1.php /usr/local/apache2.4/htdocs/index.php
8、访问http://192.168.3.74
apache配置php7来解析
1、[root@centos7 htdocs]# vi /usr/local/apache2.4/conf/httpd.conf
#LoadModule php5_module modules/libphp5.so
LoadModule php7_module modules/libphp7.so
2、[root@centos7 htdocs]# /usr/local/apache2.4/bin/apachectl graceful
3、访问
<a href="https://s1.51cto.com/oss/201711/09/2edfb486d42636828b7a9f22b3799e5d.png-wh_500x0-wm_3-wmp_4-s_2218255659.png" target="_blank"></a>
Apache默认虚拟主机
1、打开vhosts虚拟主机
[root@centos7 htdocs]# vi /usr/local/apache2.4/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
3、定义了两个虚拟主机
[root@centos7 htdocs]# cat /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>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.111.com www.example.com
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
4、
[root@centos7 htdocs]# mkdir /data/wwwroot/
[root@centos7 htdocs]# mkdir /data/wwwroot/abc.com
[root@centos7 htdocs]# mkdir /data/wwwroot/111.com
5、
[root@centos7 htdocs]# vi /data/wwwroot/abc.com/index.php
<h1>this is abc.com</h1>
[root@centos7 htdocs]# vi /data/wwwroot/111.com/index.php
<h1>this is 111.com</h1>
[root@centos7 htdocs]# /usr/local/apache2.4/bin/apachectl graceful
7、访问:[root@centos7 abc.com]# curl -x http://127.0.0.1:80 abc.com
错误:403拒绝访问:
<a href="https://s1.51cto.com/oss/201711/09/c4b401c14f2bb629ddafdcefb1acb2df.png-wh_500x0-wm_3-wmp_4-s_1770810071.png" target="_blank"></a>
解决: vi /usr/local/apache2.4/conf/httpd.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
# Deny from all
</Directory>
8、/usr/local/apache2.4/bin/apachectl graceful
9、访问
[root@centos7 abc.com]# curl -x127.0.0.1:80 www.abc.com
[root@centos7 abc.com]# curl -x127.0.0.1:80 www.111.com
10、abc.com为默认的页面
11、无论访问哪个都为调到这上
[root@centos7 abc.com]# curl -x127.0.0.1:80 123123123asdasd
本文转自 jiekegz 51CTO博客,原文链接:http://blog.51cto.com/jacksoner/1980209