26期2018081 Apache和PHP结合 Apache默认虚拟主机

8月1日任务

11.14/11.15 Apache和PHP结合
11.16/11.17 Apache默认虚拟主机




image.png

1. 首先看一下:

image.png

这是个警告,虽然并非错误但是可以取消掉。办法就是去定义好 servername 


vim /usr/local/apache2/conf/httpd.conf,将servername前面的#去掉即可

 image.png

image.png


2. 增加一行配置 

Require all denied 改成 allowed 这样就可以被访问到了!telnet访问肯定不行,因为80端口尚未打开。看下图,无法用telnet访问

image.png

需要修改配置文件 vim /usr/local/apache2/conf/httpd.conf  require all granted 

image.png


/usr/local/apache2/bin/apachectl -t 用来检查配置文件的语法是否正确

image.png


/usr/local/apache2/bin/apachectl graceful 重新加载配置文件 不会影响进程


3. 增加一行与php相关的配置。 搜AddType 然后增加一行可以让php解析的语句 

AddType application/x-httpd-php .php

image.png


4. 在/htodcs下放一个文件,php 文件 看是否加载php。

但是失败了。

原因是防火墙的设置忘记设置了,所以没有打开80端口。

iptables -I INPUT -p tcp --dport 80 -j ACCEPT 临时打开80端口就好了。 image.png


注意,同样将php5换成7也可以成立。



Apache默认虚拟主机


image.png


可以理解成在一个httpd服务下运行了多个网站,域名。每个域名对应的是一个虚拟主机。

有一个httpd配置文件的位置,DocumentRoot定义了网站的根目录的位置。ServerName定义的就是域名。


首先从windows下来进行理解。

windows下hosts的地址是 C:\Windows\System32\drivers\etc\hosts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Copyright (c)  1993 - 2009  Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP  for  Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a  '#'  symbol.
#
# For example:
#
#       102.54 . 94.97      rhino.acme.com          # source server
#        38.25 . 63.10      x.acme.com              # x client host
 
# localhost name resolution is handled within DNS itself.
#    127.0 . 0.1        localhost
#   :: 1              localhost

可以在这里面定义一个ip 域名,让域名指向ip 

image.png

然后访问www.163.com就变成了访问192.168.202.123 

image.png

image.png

这样就临时改变了一个域名对应的ip, 这是在DNS未生效的情况下使用


Linux下的虚拟主机 apache配置文件中 virtual hosts

image.png

将这个注释取消后,就可以去到一个二级文件目录对虚拟主机进行定义。

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 

每一对成对出现的 VirtualHost标签就代表一个主机的定义。第一个是默认的虚拟主机。

image.png

如果虚拟目录生效的话,apache的配置文件里的servername就失效了。

image.png

然后在对应的位置创建目录以及index测试文件。

image.png


测试虚拟主机

curl 命令来实现访问虚拟主机


curl -x192.168.202.123:80 www.goau.com.au 

image.png


猜你喜欢

转载自blog.51cto.com/13691454/2156516