1. 程式人生 > >配置http支持php及虛擬主機

配置http支持php及虛擬主機

httpphp 虛擬主機

配置httpd支持php
  • httpd主配置文件/usr/local/apache2.4/conf/httpd.conf
  • vim /usr/local/apache2.4/conf/httpd.conf
    修改以下4個地方
    ServerName  這個打開後開啟httpd後沒有警告
    Require all denied   這個修改為Require all granted 防止打開虛擬主機配置文件時403
    AddType application/x-httpd-php .php 加上這行才可以解析php
    DirectoryIndex index.html index.php  添加默認索引頁
    DocumentRoot "/usr/local/apache2.4/htdocs" 這個參數可以定義網站的域名根目錄
  • /usr/local/apache2.4/bin/apachectl -t //測試語法,改完配置需要先檢查下再加載
  • /usr/local/apache2.4/bin/apachectl graceful //重新加載配置文件,不重啟服務
  • iptables -I INPUT -p tcp --dport 80 -j ACCEPT //打開80端口
  • netstat -lntp //查看80端口
[root@aminglinux-02 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1122/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2094/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2658/mysqld         
tcp6       0      0 :::80                   :::*                    LISTEN      2387/httpd          
tcp6       0      0 :::22                   :::*                    LISTEN      1122/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2094/master         
  • 在物理機瀏覽器上打開主機ip地址能訪問
  • 打不開ip主頁排查思路
    -現在物理機上查看Ip能不能ping通,telnet能不能通,再查看虛擬機上有沒有打開80端口
    [root@akuilinux01 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
    [root@akuilinux01 ~]# iptables -nvL
    Chain INPUT (policy ACCEPT 58 packets, 3900 bytes)
    pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
  • 寫一個測試php,vim /usr/local/apache2.4/htdocs/1.php
[root@aminglinux-02 ~]# vim /usr/local/apache2.4/htdocs/1.php

<?php
phpinfo();
?>
  • 在物理機瀏覽器上打開主機ip地址/1.php,可以打開php信息頁面,如果不支持php解析的話,訪問該文件時會顯示源代碼。
  • 故障排查

    • 檢查Apache是否已加載PHP模塊

      [root@aminglinux-02 ~]# /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)
      php7_module (shared)
    • 查看php模塊文件
      [root@aminglinux-02 ~]# ls /usr/local/apache2.4/modules/libphp7.so 
      /usr/local/apache2.4/modules/libphp7.so
      [root@aminglinux-02 ~]# ls /usr/local/apache2.4/modules/libphp5.so 
      /usr/local/apache2.4/modules/libphp5.so
    • 查看apache配置文件這四項
#LoadModule php5_module        modules/libphp5.so
LoadModule php7_module        modules/libphp7.so

ServerName www.example.com:80

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    Addtype application/x-httpd-php .php

<Directory />
    AllowOverride none
    Require all granted
</Directory>
  • “/usr/local/apache2.4/bin/apachectl -t”檢查配置文件是否存在語法錯誤
    • 配置PHP7解析只需要把配置文件中加載的php模塊換成php7的,其他相同
    • ctrl+r用途:反向搜索並調用執行過的命令。(reverse-i-search)。
      使用方法:按ctrl+r後命令行會變成“(reverse-i-search)`‘: ”狀態,然後輸入使用過的參數,此時會直接顯示出相關的命令,回車即可執行該命令。

httpd的默認虛擬主機

  • windows定義hosts
    • C:\Windows\System32\drivers\etc\hosts
      192.168.21.128 www.abc.com www.123.com
    • 可以ping檢查下是不是解析到你需要的ip
  • 一臺服務器可以訪問多個網站,每個網站都是一個虛擬主機
  • 概念:域名(主機名)、DNS(域名解析系統)、hosts(局域網的域名解析)
  • 任何一個域名解析到這臺機器,都可以訪問的虛擬主機就是默認虛擬主機
  • vim /usr/local/apache2/conf/httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
打開這個選項,啟用虛擬主機配置文件/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
  • vim /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>

<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@aminglinux-02 ~]# mkdir /data/wwwroot
    [root@aminglinux-02 ~]# mkdir /data/wwwroot/abc.com
    [root@aminglinux-02 ~]# mkdir /data/wwwroot/111.com
  • 創建index.php文件
    [root@aminglinux-02 ~]# vim /data/wwwroot/abc.com/index.php
    [root@aminglinux-02 ~]# vim /data/wwwroot/111.com/index.php
    <?php
    echo "abc.com";
  • /usr/local/apache2/bin/apachectl –t
  • /usr/local/apache2/bin/apachectl graceful
  • 測試虛擬主機
root@aminglinux-02 ~]# curl -x192.168.16.120:80 abc.com
abc.com[root@aminglinux-02 ~]# curl -x192.168.16.120:80 111.com
111.com[root@aminglinux-02 ~]# curl -x192.168.16.120:80 www.dahjdhajabc.com
abc.com[root@aminglinux-02 ~]# 
  • 這裏的abc.com為默認虛擬主機

擴展

  • apache所有的主機都指向第一個

配置http支持php及虛擬主機