1. 程式人生 >實用技巧 >其它 >配置httpd支援PHP和httpd的預設虛擬主機

配置httpd支援PHP和httpd的預設虛擬主機


原始碼編譯安裝的情況下


一、配置httpd支援PHP


首先修改httpd的主配置檔案

[[email protected] ~]# vim /usr/local/apache2.4/conf/httpd.conf

修改下面四個地方

配置httpd支援PHP和httpd的預設虛擬主機

修改完成後,測試語法並且啟動服務

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl start

檢視埠

[[email protected] ~]# netstat -lnpt
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      1549/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2579/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      31014/Mysqld        
tcp6       0      0 :::80                   :::*                    LISTEN      17969/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      1549/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2579/master      

看一看預設的首頁顯示

[[email protected] ~]# curl localhost
<html><body><h1>It works!</h1></body></html>

在新增PHP檔案,測試以下是否支援成功

[[email protected] ~]# cd  /usr/local/apache2.4/
[[email protected] apache2.4]# ls
bin    cgi-bin  error   icons    lib   man     modules
build  conf     htdocs  include  logs  manual
[[email protected] apache2.4]# cd htdocs 
[[email protected] htdocs]# ls
index.html
[[email protected] htdocs]# vim test.php    # 編輯一個PHP的首頁檔案,使首頁顯示123
<?php
echo 123;
?>
~                                                            
~                                                                                                              
~                                                            
~                                                            
~                                                            
"test.php" [New] 3L, 19C written           
[[email protected] htdocs]# curl localhost/test.php
123[[email protected] htdocs]# curl localhost/test.php
123[[email protected] htdocs

二、httpd的預設虛擬主機


1、修改http的主配置檔案,找到關於虛擬主機的那一行,把註釋去掉

[[email protected] ~]# vim /usr/local/apache2.4/conf/httpd.conf
# 這一行註釋去掉,可搜尋httpd-vhost
Include conf/extra/httpd-vhosts.conf

2、修改虛擬主機配置檔案,在配置檔案裡修改虛擬主機配置

[[email protected] extra]# vim httpd-vhosts.conf 
# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
#
# for further details before you try to setup virtual hosts.
#
    DocumentRoot "/usr/local/apache2.4/docs/dummy-host.exampl
    DocumentRoot "/usr/local/apache2.4/docs/dummy-host2.examp
    ServerAdmin [email protected]
    ErrorLog "logs/dummy-host2.example.com-error_log"
# configuration.

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost conta
iner.
# The first VirtualHost section is used for all requests that
 do not
# match a ServerName or ServerAlias in any <VirtualHost> bloc
k.
#### 根據下面的例子修改自己想要新增的虛擬主機  
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/lf.com"
    ServerName lf.com
    ServerAlias www.lf.com
    ErrorLog "logs/lf.com-error_log"
    CustomLog "logs/lf.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
</VirtualHost>

3、建立虛擬主機的資料目錄,並且編輯虛擬主機的首頁目錄

[[email protected] extra]# cd /data/wwwroot 
[[email protected] wwwroot]# mkdir lf.com
[[email protected] wwwroot]# mkdir www.123.com 
[[email protected] extra]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] extra]# /usr/local/apache2.4/bin/apachectl graceful
[[email protected] extra]# echo "lf.com">/data/wwwroot/lf.com/index.html
[[email protected] extra]# echo "123.com">/data/wwwroot/www.123.com/index.html

4、檢視
它會訪問/data/wwwroot/lf.com/index.html

[[email protected] wwwroot]# curl -x10.30.59.219:80 lf.com
lf.com

訪問/data/wwwroot/www.123.com/index.html

[[email protected] wwwroot]# curl -x10.30.59.219:80 www.123.com
123.com

它會訪問/data/wwwroot/lf.com/index.html

[[email protected] wwwroot]# curl -x10.30.59.219:80 www.abc.com     lf.com