1.6 Apache2.4与php结合

1.6 Apache2.4与php结合

  • httpd主配置文件/usr/local/apache2.4/conf/httpd.conf
  • vim /usr/local/apache2.4/conf/httpd.conf //修改以下4个地方
    ServerName
    Require all denied
    AddType application/x-httpd-php .php
    DirectoryIndex index.html index.php
  • /usr/local/apache2.4/bin/apachectl -t //测试语法
  • /usr/local/apache2.4/bin/apachectl start //启动服务
    netstat -lntp
    curl localhost
  • vim /usr/local/apache2.4/htodcs/test.php //增加如下内容

<?php echo 123; ?>

curl localhost/test.php

要想实现php的效果先编辑Apache测试文件!

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/httpd.conf
……
ServerName www.app.com
……
<Directory />
    AllowOverride none
     Require all granted
</Directory>
……
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
……
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
……

mark


这里插入Linux防火墙知识:
selinux //临时关闭 setenforce 0
selinux //永久关闭 vi /etc/selinux/config
centos7之前使用netfilter防火墙、
centos7开始使用firewalld防火墙
关闭firewalld开启netfilter方法:
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl enable iptables
systemctl start iptables

##关于selinux:
##临时关闭:
[root@Dasoncheng ~]# getenforce
Enforcing
[root@Dasoncheng ~]# setenforce 0
[root@Dasoncheng ~]# getenforce
Permissive
##永久关闭:
[root@Dasoncheng ~]# vim /etc/selinux/config
[root@Dasoncheng ~]# grep -B2 -w '^SELINUX' /etc/selinux/config
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
##关于关闭firewalld开启netfilter:
[root@Dasoncheng ~]# systemctl stop firewalld
[root@Dasoncheng ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@Dasoncheng ~]# yum install -y iptables-services
[root@Dasoncheng ~]# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@Dasoncheng ~]# systemctl start iptables.service
[root@Dasoncheng ~]# iptables -F
[root@Dasoncheng ~]# iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 428 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@Dasoncheng ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

编辑好了httpd.conf之后,测试:

[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@Dasoncheng ~]# ps aux |grep httpd
daemon    22383  0.1  0.9 540344  9520 ?        Sl   10:13   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    22384  0.1  0.9 540344  9512 ?        Sl   10:13   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon    22386  0.1  0.9 540344  9508 ?        Sl   10:13   0:00 /usr/local/apache2.4/bin/httpd -k start
root      22468  0.0  0.0 112664   968 pts/0    S+   10:13   0:00 grep --color=auto httpd
root      34165  0.0  1.2 253516 12792 ?        Ss   03:56   0:02 /usr/local/apache2.4/bin/httpd -k start
[root@Dasoncheng ~]# netstat -lntp |grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      22383/httpd  
[root@Dasoncheng ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

访问测试:

如果无法访问,从selinux、防火墙、端口等进行排查!

[root@Dasoncheng ~]# curl localhost
<html><body><h1>It works!</h1></body></html>

mark
mark

1.7 Apache默认虚拟主机

先编辑wins的hosts文件:
mark
域名访问:
mark

编辑httpd.conf开启虚拟主机:

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/httpd.conf    ##编辑主配置文件,开启虚拟主机(开启之后,原站点就会失效!)
[root@Dasoncheng ~]# grep -A1 'Virtual hosts' /usr/local/apache2.4/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

编辑虚拟主机配置文件:

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
#   ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com 123.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
#   ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

编辑index页面:

[root@Dasoncheng ~]# mkdir -p /data/wwwroot/abc.com/
[root@Dasoncheng ~]# cat /data/wwwroot/abc.com/index.php
<?php
echo "abc.com";
?>
[root@Dasoncheng ~]# mkdir -p /data/wwwroot/111.com
[root@Dasoncheng ~]# cat /data/wwwroot/111.com/index.php
<?php
echo "111.com";
?>

小提示:开启了虚拟主机之后,原先的主配置站点就失效!
虚拟主机配置文件可以定义多个virtual host;每一个host代表一个站点!其中第一个host就变为了主站点,访问ip就是这个主机!

测试虚拟主机访问:

[root@Dasoncheng ~]# curl -x192.168.60.11:80 www.abc.com
abc.com
##ServerAlias我就没有测试,下面也没有;
[root@Dasoncheng ~]# curl -x192.168.60.11:80 www.111.com
111.com
[root@Dasoncheng ~]# vim /etc/hosts
[root@Dasoncheng ~]# tail -1 /etc/hosts
192.168.60.11 www.abc.com www.111.com
[root@Dasoncheng ~]# curl www.abc.com
abc.com
[root@Dasoncheng ~]# curl www.111.com
111.com
[root@Dasoncheng ~]# curl http://192.168.60.11
abc.com

mark

apache所有的主机都指向第一个? http://www.aminglinux.com/bbs/thread-491-1-1.html

猜你喜欢

转载自my.oschina.net/u/3651233/blog/1622746
1.6
今日推荐