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

11.14 Apache和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
  • 先打开httpd的配置文件

- [root@aminglinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@aminglinux-001 ~]# vim /usr/local/apache2.4/conf/httpd.conf[1]+  已停止               vim /usr/local/apache2.4/conf/httpd.conf
[root@aminglinux-001 ~]# /usr/local/apache2.4/bin/apachectl restartAH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using fe80::a152:bbdf:8b2b:db9b. Set the ‘ServerName‘ directive globally to suppress this message
httpd not running, trying to start
[root@aminglinux-001 ~]# fgvim /usr/local/apache2.4/conf/httpd.conf#ServerAdmin [email protected]## ServerName gives the name and port that the server uses to identify itself.# This can often be determined automatically, but we recommend you specify# it explicitly to prevent problems during startup.## If your host doesn‘t have a registered DNS name, enter its IP address here.#ServerName www.example.com:80## Deny access to the entirety of your server‘s filesystem. You must# explicitly permit access to web content directories in other # <Directory> blocks below.#<Directory />
    AllowOverride none
    Require all denied
</Directory>## Note that from this point forward you must specifically allow# particular features to be enabled - so if something‘s not working as# you might expect, make sure that you have specifically enabled it# below.#-- 插入 --  [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[1]+  已停止               vim /usr/local/apache2.4/conf/httpd.conf
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl restartAH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName‘ directive globally to suppress this message
httpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79:  2258 段错误               $HTTPD -k $ARGV
[root@localhost ~]# fgvim /usr/local/apache2.4/conf/httpd.conf
[root@localhost ~]# fg-bash: fg: 当前: 无此任务
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl restarthttpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79:  2262 段错误               $HTTPD -k $ARGV
[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# /usr/local/apache2.4/bin/apachectl restarthttpd not running, trying to start
/usr/local/apache2.4/bin/apachectl: 行 79:  2266 段错误               $HTTPD -k $ARGV
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl stophttpd (no pid file) not running[root@localhost ~]# /usr/local/apache2.4/bin/apachectl start/usr/local/apache2.4/bin/apachectl: 行 79:  2272 段错误               $HTTPD -k $ARGV
[root@localhost ~]# !vivim /usr/local/apache2.4/conf/httpd.conf
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl start[root@localhost ~]#
[root@localhost ~]# ps aux |grep httpdroot       2277  0.0  0.8 253580  8788 ?        Ss   14:30   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon     2278  0.0  1.1 540408 11008 ?        Sl   14:30   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon     2279  0.0  1.1 540408 11008 ?        Sl   14:30   0:00 /usr/local/apache2.4/bin/httpd -k start
daemon     2282  0.0  1.1 540408 11008 ?        Sl   14:30   0:00 /usr/local/apache2.4/bin/httpd -k start
root       2364  0.0  0.0 112680   976 pts/0    S+   14:32   0:00 grep --color=auto httpd
[root@localhost ~]#
  • 刚才把servername打开了 也就不再提示那个错误了 httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName‘ directive globally to suppress this message httpd not running, trying to start

  • 这是第一处,第二处 Require all denied

  • 用网页访问地址192.168.202.131(本机ip)带不开,再用windowsping 下,是可以ping通,试下telnet 访问下80端口 使用telnet 192.168.202.131 80

  • 因为windows 并没有打开tenet的客户端,所以它会提示没有这个命令

  • 打开telnet客户端方式-打开控制面板-程序-程序和功能-打开或关闭windows功能

  • 使用telnet 192.168.202.131 80 结果是 C:\Users\Administrator.USER-20170629EN>telnet 192.168.202.131 80 正在连接192.168.202.131...无法打开到主机的连接。 在端口 80: 连接失败 80端口并不通,因为在linux服务器上并没有打开80端口

  • 使用iptables -nvL 看下规则

[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  660 54606 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED    1    80 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
   25  1798 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   25  1798 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   25  1798 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID   23  1686 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 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           
    0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 545 packets, 76871 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  545 76871 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           Chain FORWARD_IN_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDI_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDI_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_IN_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_OUT_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDO_public  all  --  *      ens33   0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 FWDO_public  all  --  *      +       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_OUT_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDI_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDI_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDI_public_allow  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           Chain FWDI_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 FWDO_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDO_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FWDO_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           Chain FWDO_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_ZONES (1 references)
 pkts bytes target     prot opt in     out     source               destination         
   25  1798 IN_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
    0     0 IN_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain INPUT_ZONES_SOURCE (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public (2 references)
 pkts bytes target     prot opt in     out     source               destination         
   25  1798 IN_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   25  1798 IN_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   25  1798 IN_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    60 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           Chain IN_public_allow (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    1    52 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW

Chain IN_public_deny (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain IN_public_log (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT_direct (1 references)
 pkts bytes target     prot opt in     out     source               destination         
[root@localhost ~]#
;临时增加一条规则,允许80端口访问
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT[root@localhost ~]#
  • 这个时候再来看网页 ,刷新下,显示可以了,能够访问了 It works !

  • 再看下telenet 再试下,telnet 192.168.202.131 80 ,只要显示成这个样子说明80端口通了

    扫描二维码关注公众号,回复: 1066638 查看本文章
  • 技术分享

  • 按ctrl + ] 再输quit 就退出来了

  • 再把那条规则删掉

[root@localhost ~]# iptables -D INPUT -p tcp --dport 80 -j ACCEPT
  • 再来看看技术分享卡死了,再加上规则,就可以了,浏览器也可以访问了

[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT[root@localhost ~]#

技术分享

  • 打开配置文件,把 Require all denied 改为 Require all granted

## Deny access to the entirety of your server‘s filesystem. You must# explicitly permit access to web content directories in other # <Directory> blocks below.#<Directory />
    AllowOverride none    Require all denied</Directory>
#<Directory />
    AllowOverride none    Require all granted</Directory>#
之所有能网页上能访问,DocumentRoot "/usr/local/apache2.4/htdocs" 指定了一个 <Directory "/usr/local/apache2.4/htdocs"> 后面有一个 Require all granted 如果把这个改成denied 肯定访问不了
AllowOverride None    #
    # Controls who can get stuff from this server.
    #
    Require all granted</Directory>
改为
# Controls who can get stuff from this server.
    #
    #Require all granted
    Require all denied</Directory>[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]#
改完配置,要做一个操作 重启,后者叫做 重新加载配置 /usr/local/apache2.4/bin/apachectl graceful,
这里告诉一个命令,用来检查配置文件语法是否正确/usr/local/apache2.4/bin/apachectl -t
结果是Syntax OK 就没问题,如果错了就会报错
[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK
[root@localhost ~]#[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful[root@localhost ~]#
  • 再来运行下网页,技术分享 打不开了

  • 所以这个地方需要改成granted,上面那个地方也是要改成granted

#
    # Controls who can get stuff from this server.
    #
    Require all granted</Directory>
  • 还有这个地方也是

# Deny access to the entirety of your server‘s filesystem. You must# explicitly permit access to web content directories in other # <Directory> blocks below.#<Directory />
    AllowOverride none    Require all granted</Directory>
  • 这是第二处,第三处,需要增加一行和php 相关的配置

  • 增加一行 AddType application/x-httpd-php .php

  • 首先在胚子文件搜AddType,

#
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    #
    # AddHandler allows you to map certain file extensions to "handlers":
增加一行 AddType application/x-httpd-php .php,如果不增加php就没办法解析
# If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php    #-- 插入 --  [root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]#
还有一个地方,增加一个索引页 DirectoryIndex index.html index.php
打开一个网站 为什么不用输入inde.php 就可以访问呢?
打开配置文件vim /usr/local/apache2.4/conf/httpd.conf 搜索Index, 在DirectoryIndex index.html后加入 index.php
Require all granted</Directory>## DirectoryIndex: sets the file that Apache will serve if a directory# is requested.#<IfModule dir_module>
    DirectoryIndex index.html index.php</IfModule>## The following lines prevent .htaccess and .htpasswd files from being -- 插入 --
检查 /usr/local/apache2.4/bin/apachectl -t
重新加载 /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful[root@localhost ~]#

11.15 Apache和PHP结合(下)

  • 先来验证一下apache 到底支不支持 php呢

[root@aminglinux-001 ~]# vi /usr/local/apache2.4/htdocs/1.php
[root@aminglinux-001 ~]# vi /usr/local/apache2.4/htdocs/1.php<?php
phpinfo();?>~                                                                                             
~                                                                                             
~                                                                                             
                                                                                         
~                                                                                             
~                                                                                             
~                                                                                             
~                                                                                             
:wq
  • 保存之后,用浏览器打开访问一下

  • 技术分享

  • 这个页面可以出来,说明你的php 支持解析了,如果不能出来,会是什么样的呢?

  • 比如说我们没有加AddType 这一行,把它注释掉 # AddType application/x-httpd-php .php

[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]#
  • 再来看下,打不开,

  • 如果解析不了,需要检查一下你的apache文件,都检查哪几项呢?

  • 1.第一项/usr/local/apache2.4/bin/apachectl -M 看看有没有加载php5_module (share)php5的模块

  • 2.如果没有加载要看一看有没有这个模块 ls /usr/local/apache2.4/modules/libphp5.so 有没有这个文件,如果有文件并没有显示那就查看配置文件里面有没有加载这个libphp5.so 的这一行配置,如果没有肯定不能解析的

[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -MLoaded 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@localhost ~]# ls /usr/local/apache2.4/modules/libphp5.so /usr/local/apache2.4/modules/libphp5.so
[root@localhost ~]#
  • 如果没有模块,看下配置文件有没有LoadModule php5_module modules/libphp5.so 这一行配置

LoadModule php5_module        modules/libphp5.so#LoadModule php7_module        modules/libphp7.so<IfModule unixd_module>
#
  • 3.接下来检查你的配置文件,有没有加这一行AddType application/x-httpd-php .php 注意这个地方 有个空格,没加空格就说明有问题

  • 有问题是可以检查出来的,使用命令/usr/local/apache2.4/bin/apachectl -t 如果显示结果是Syntnx OK

  • 4.还有一步 就是这个index.php,这一行加不加倒也不影响,为什么要加这个呢,因为在访问一个网站的时候 有时候并没有输入www.aplearn.com/index.php 正常的这才是真正的地址,但是不加/index.php 也可以访问,就是有这个索引页index.php,就是定义了一个index

[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]#
  • 再来看看技术分享

  • 现在来试试这个php7 ,不要php5 了,进入配置文件,搜索libphp

#LoadModule php5_module        modules/libphp5.soLoadModule php7_module        modules/libphp7.so<IfModule unixd_module>## If you wish httpd to run as a different user or group, you must run# httpd as root initially and it will switch.  ## User/Group: The name (or #number) of the user/group to run httpd as.# It is usually good practice to create a dedicated user and group for# running httpd, as with most system services.#User daemonGroup daemon</IfModule># ‘Main‘ server configuration#:wq  

[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]#
  • 快捷键ctrl r 再输入命令相关词,可以快速帮你找到命令历史里相关的 命令

  • 再打开网页,也是可以的技术分享

  • 咱们看到的这个网页和/usr/local/php7/bin/php -i |less 看到的结果基本一致的,只不过一个是图形化表格化 格式,一个是文本文件格式

[root@localhost ~]# /usr/local/php7/bin/php -i |less




phpinfo()
PHP Version => 7.1.6System => Linux localhost.localdomain 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64
Build Date => Oct  5 2017 23:52:17Configure 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 => 20160303PHP Extension => 20160303Zend Extension => 320160303Zend Extension Build => API320160303,TS
PHP Extension Build => API20160303,TS
Debug Build => no
Thread Safety => enabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => disabled

Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, tls, tlsv1.0, tlsv1.1, tlsv1.2Registered Stream Filters => zlib.*, bzip2.*, convert.iconv.*, mcrypt.*, mdecrypt.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk

This program makes use of the Zend Scripting Language Engine:
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
:

11.16 Apache 默认虚拟主机 (上)

  • 概念:域名(主机名)、DNS、解析域名、hosts

  • 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机

  • vim /usr/local/apache2/conf/httpd.conf //搜索httpd-vhost,去掉#

  • vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //改为如下

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/aming.com"
    ServerName aming.com
    ServerAlias www.aming.com
    ErrorLog "logs/aming.com-error_log"
    CustomLog "logs/aming.com-access_log" common
</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

  • 一台服务器可以访问多个网站,多个域名 ,每个网站都是一个虚拟主机

  • 假如你的网站既能访问百度 有可以访问谷歌,同时都在一台服务器上运行,就用了一个httpd的服务,这个就是一个网站 多个域名,每个域名对应着一个虚拟主机

  • /DocumentRoot "/usr/local/apache2.4/htdocs"

  • <Directory "/usr/local/apache2.4/htdocs"> 这个参数定义了 这个网站的根目录在那里,域名就是上面的ServerNmae www.exaple.com :80 我用ip 也可以访问它,用www.aminglinux.com 也能访问它

  • 先教大家在windows下怎么去写hosts

  • C:\Windows\System32\drivers\etc\hosts 这个和咱们的linux下的hosts 相似,可以在里面定义一个ip 定义一个域名,然后让这个域名指向到这个ip去,打开方式用记事本

# 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
#
# For example:
#
#      102.54.94.97     rhino.acme.com          
# source server#       38.25.63.10     x.acme.com              
# x client host192.168.202.131 www.abc.com 
 # localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
  • 到底能不能指向这个192.168.202.131 呢,保存下这个文件

  • 格式 先ip 域名 空格 域名 可以写多个,一行里面可以有多个域名 技术分享

  • 正常情况下不写这个hosts

  • 技术分享

  • 再来访问下

  • 技术分享

  • 这ip 61.132.13.130 很明显是一个公网ip,上面那个之所以可以访问到192。168.202.131那里是因为我们写了hosts,我们指向了这个ip 这个就是hosts 的作用,它的目的是就让你临时访问到一个服务器上去,比如上面的www.abc.com 或者 123.com

  • 把这个域名指向到这台机器上去,这个是在什么情况下用呢,是在DNS 还没有生效的情况下

  • 技术分享

  • 技术分享

  • 我们这个并没有在httpd 的配置文件上去定义,仅仅在ServerNamewww.example.com ,这个叫做 apache的默认主机 任何一个域名 只要是指定要这个ip,它都会访问到这个站点,那这个站点就叫做默认虚拟主机

  • 如果你没有做任何的操作,仅仅是使用了httpd.conf

  • 搜索这个extra 找到Virtual hosts #Include conf/extra/httpd-vhost.conf 把这行#去掉

[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf
# Virtual hostsInclude conf/extra/httpd-vhosts.conf
# Local access to the Apache HTTP Server Manual
#Include conf/extra/httpd-manual.conf
# Distributed authoring and versioning (WebDAV)
#Include conf/extra/httpd-dav.conf:wq
  • 打开一个二级配置文件就,这个既是虚拟主机配置文件,在这个配置文件里 可以定义多个ServerName ,vim /usr/local/apache2.4/conf/extra/httpd-vhoust-conf 虚拟主机配置文件

[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
# Virtual Hosts## Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# Virtual Hosts## Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn‘t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
## Please see the documentation at # <URL:
 # for further details before you try to setup virtual hosts.
 ## You may use the command line option ‘-S‘ to verify your virtual host# configuration.
 ## 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 "/usr/local/apache2.4/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common</VirtualHost><VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/local/apache2.4/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
  • 改下这个配置文件

  • 每一个这个都代表这一个主机,一个主机就是一个网站

  • 现在要想给它区分开,就得打开虚拟主机配置文件,打开之后做一个更改

  • DocumentRoot 这个用来定义你网站的更目录在哪里

<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>


:wq
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@localhost ~]# mkdir /data/wwwroot/
[root@localhost ~]# mkdir /data/wwwroot/abc.com
[root@localhost ~]# mkdir /data/wwwroot/111.com
[root@localhost ~]#
[root@localhost ~]# vim /data/wwwroot/abc.com/index.php<?php
echo "abc.com";~                                                                                                           
~  
[root@localhost ~]# vim /data/wwwroot/abc.com/index.php
[root@localhost ~]# vim /data/wwwroot/111.com/index.php<?php
ehco "111.com";~                                                                                                           
~
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful[root@localhost ~]#

11.17 Apache 默认虚拟主机 (下)

  • 新的命令curl -x

  • 如果不绑定hosts 去ping www.abc.com,会访问到外网上去,

[root@localhost ~]# ping www.abc.comPING abc.com (199.181.132.250) 56(84) bytes of data.
64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=1 ttl=128 time=238 ms
64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=2 ttl=128 time=241 ms
64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=3 ttl=128 time=236 ms
64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=4 ttl=128 time=237 ms
64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=5 ttl=128 time=236 ms
^C
--- abc.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 6519ms
rtt min/avg/max/mdev = 236.087/238.031/241.597/1.958 ms
[root@localhost ~]#
  • vim /etc/hosts 文件

  • 还可以用curl -x192。168.202.131:80 abc.com

- [root@localhost ~]# curl -x192.168.202.131:80 abc.comabc.com[root@localhost ~]# curl -x192.168.202.131:80 www.abc.comabc.com[root@localhost ~]# curl -x192.168.202.131:80 www.abcd.comabc.com[root@localhost ~]# curl -x192.168.202.131:80 www.abcde.comabc.com[root@localhost ~]#
  • 再来访问一个example

  • 1.在主配置文件当中,httpd.conf里面打开了虚拟主机配置文件,这时候虚拟主机配置文件生效了,主配置文件里定义的 失效

  • 2,虚拟主机配置文件一旦单开,里面可以定义多个vir host,每一个virtural host 都docunmentroot ,以及servername ,每一个vir host 杜代表一个站点,都代表一个虚拟主机

  • 默认虚拟主机,无论任何域名解析到这个ip 上,都会去访问默认虚拟主机

猜你喜欢

转载自www.cnblogs.com/pta188/p/9099891.html