Apache + PHP配置


  因工作需要,重新配置了Apache和PHP。想起當年曾經配置過,但是已經忘得差不多了。而且,也沒有記錄。從我個人來看,確實缺乏這樣的訓練,從國家教育體系來看,似乎也從未有過做科學記錄的訓練。中國的瓷器之所以有名氣,是因為發展得早,各種能工巧匠各種靈感迸發,精美絕倫,然后各種失傳,只留下一個精美的物件,而沒有怎樣再次制作的記錄。到現在也不知道地動儀的內部結構吧?也許是他們沒有做記錄的習慣,也許是他們不想讓別人知道,不想讓別人學去自己的看家本領。從歷史上中國人的個性來看,后者的可能性大。不是他們沒有做記錄的習慣,而是他們不想做記錄。反觀目前已經超越中國、處於世界頂級的德國瓷器,在奧古斯都大帝那個時代,為了仿制中國瓷器,進行了3萬多次實驗,並記錄。可復現、可證實、可證偽,這是科學。需要訓練,思維上,行動上。

==============言歸正傳雙層線===============================

Apache安裝與配置詳解

安裝

官方網站:http://www.apache.org/

頂層菜單找到:download

在下面的鏈接中選擇官方推薦的第一個,清華的鏡像服務器:

http://mirrors.tuna.tsinghua.edu.cn/apache/

會出現一個目錄列表,Ctrl + F,查找httpd,點擊進入。

如果想查看詳細信息,就搜Apache HTTP Server Download Page,也可以直接打開http://httpd.apache.org/download.cgi

在這個頁面上,我們可以看到一些說明和細節信息,各種下載方法。在這里,只說明windows系統的文件的下載方法。

在頁面最明顯的位置,有個

  • 2.4.29 (released 2017-10-23)

點擊,會跳到本頁面對應的節。如下:

是源碼和安裝文件的下載鏈接。apache服務器本身不存儲文件,一律通過第三方服務器發布,大概是因為全世界的流量太大了,作為一個非盈利組織,無力也沒有必要維護。

如果點擊Binaries,又會進入目錄瀏覽,兜兜轉轉又會回到第三方服務器,所以,直接點 a number of third party vendors,或者Files for Microsoft Windows,這倆鏈接是一樣的,會出現一個第三方的列表:

如果想要純的Apache服務器,就選前兩個,后面三個是集成了mysql和php的版本。

下載后,解壓,我放在了c:/apache24目錄下。

配置

打開apache24/conf目錄,用文本編輯器打開httpd.conf文件。

查找ServerRoot "${SRVROOT}"

(說明:因文件內有一些標記帶有引號,故凡是需要查找的,一律用中文中括號標記,以便區別,使用時只需查找中括號內的內容即可,下同)

此處的${SRVROOT}"是apache配置文件中的變量,它的上一句是Define SRVROOT "/Apache24",意思是,把SRVROOT這個變量的值定義為/Apache24,

這里我們根據實際情況調整一下,改成Define SRVROOT "c:/Apache24",也就是我們Apache安裝目錄的絕對路徑。

這個配置文件中,使用了很多${SRVROOT}變量來設置apache的安裝路徑,比如DocumentRoot,所以這里最好把這個變量用上,就不必到處改了。

 

查找【Listen  80】,這里是默認的,如無必要,不必修改。

查找【ServerName】

ServerName localhost:80

此處是主站點名稱,即網站域名,根據實際情況修改

查找【ServerAdmin】

ServerAdmin xxx@163.com

此處是管理員的郵件地址,可自動向管理員發日志等郵件。

查找【DocumentRoot】

DocumentRoot "${SRVROOT}/htdocs"

此處是網站的根目錄,SRVROOT即是前面定義的安裝目錄,可根據實際情況調整。可以設置在安裝目錄之外。

 

查找【<Directory />】

把其下的Require all denied 改為Require all granted

 

最后,使用httpd.exe文件進行安裝,可以cmd進入D:\Apache24\bin目錄執行,也可以任意目錄D:\Apache24\bin\httpd.exe。

httpd -k install -n "Apache24"

引號內是自定義的別名。

如果需要刪除這個服務:

httpd -k uninstall -n "Apache2.4"

 

至此,apache配置完畢。此時用http://localhost,應該就可以訪問了。

如果外網無法訪問,關閉防火牆或設置80端口通過,試試。

 

虛擬站點的配置

在http.conf 中將 httpd-vhosts.conf包含進來

# Virtual hosts

Include conf/extra/httpd-vhosts.conf

 

在 httpd-vhost.conf中配置

    (1)基於IP的虛擬主機

     修改hosts文件,添加3個域名與之對應

     192.168.1.11 www.test1.com

     192.168.1.12 www.test2.com

     192.168.1.13 www.test3.com

     建立虛擬主機存放文件的根目錄,如

     www/test1/1.html

     www/test2/2.html

     www/test3/3.html

     在httpd-vhosts.conf進行如下配置

     <VirtualHost 192.188.1.11:80>

         ServerName www.test1.com

         DocumentRoot "www/test1"

         <Directory "www/test1">

             Options Indexs FollowSysLinks

             AllowOverride None

             Order allow deny

             allow from all

             DirectoryIndex  index.html index.htm index.php

         </Directory>

     </VirtualHost>

              

     <VirtualHost 192.168.1.12:80>

          ServerName www.test2.com

           DocumentRoot /www/test2/

          <Directory "/www/test2">

             Options Indexes FollowSymLinks

              AllowOverride None

              Order allow,deny

              Allow From All

           </Directory>

     </VirtualHost>

  

    

      <VirtualHost 192.168.1.13:80>

        ServerName www.test3.com

        DocumentRoot /www/test3/

        <Directory "/www/test3">

         Options Indexes FollowSymLinks

         AllowOverride None

         Order allow,deny

          Allow From All

       </Directory>

      </VirtualHost>

  

   (2)基於主機名

     設置域名映射同一個主機

     192.168.1.10 www.test1.com

     192.168.1.10 www.test2.com

     192.168.1.10 www.test3.com

     設置存放網頁的根目錄

     www/test1/1.html

     www/test2/2.html

     www/test3/3.html

     在使用基於域名的虛擬主機時,必須指定服務器的IP地址和可能的訪問端口來使主機接受請求,可以

     使用NameVirtualHost指令來配置,如果服務器上所有的IP都會用到,則可以使用來表示,

     在NameVirtualHost指定的ip並不會讓服務器監聽這個IP

     然后配置<VirtualHost>

     如果在現有的WEB服務器上配置虛擬主機,則必須為現存的虛擬主機也配置<VirtualHost>,其中

     ServerName 和 DocumentRoot包含的內容應該與全局的內容一致,且要放在配置文件的最前面,

     作為默認主機的配置

     NameVirtualHost :80

     <VirtualHost :80>

        ServerName www.test1.com

        DocumentRoot "www/test2"

        <Directory "www/test1">

             Options Indexs FollowSymLinks

             AllowOverride None

             Order allow,deny

             allow from all

        </Directory>

     </VirtualHost>

       <VirtualHost :80>

        ServerName www.test2.com

        DocumentRoot "www/test2"

        <Directory "www/test2">

             Options Indexs FollowSymLinks

             AllowOverride None

             Order allow,deny

             allow from all

        </Directory>

     </VirtualHost>

       <VirtualHost :80>

        ServerName www.test3.com

        DocumentRoot "www/test3"

        <Directory "www/test3">

             Options Indexs FollowSymLinks

             AllowOverride None

             Order allow,deny

             allow from all

        </Directory>

     </VirtualHost>

  

   (3)基於端口

     修改httpd.conf

     設置為

Listen 8001

           Listen 8002

     修改虛擬主機配置文件 httpd-vhosts.conf

     <VirtualHost *:8001>

         ServerName www.test1.com

         DocumentRoot "www/test1"

     </VirtualHost>

      <VirtualHost *:8002>

         ServerName www.test2.com

         DocumentRoot "www/test2"

     </VirtualHost>

  此處要注意,如果 httpd-vhosts.conf中配置了80端口的virtualhost的DocumentRoot,就要與httpd.conf中的DocumentRoot一致,否則,以httpd-vhosts.conf中的為准,在httpd-vhosts.conf中找不到DocumentRoot時,才使用httpd.conf中的DocumentRoot。

 

PHP配置

下載鏈接:https://windows.php.net/download

注意與線程安全版本配合好,不然會報錯,而且你不知道錯在哪,先保證這一步是正確的。

Apache服務器下應當使用TS版,即線程安全版,官方說明如下:

TS(Thread Safe) refers to multithread capable builds. NTS(Non Thread Safe) refers to single thread only builds. Use case for TS binaries involves interaction with a multithreaded SAPI and PHP loaded as a module into a web server. For NTS binaries the widespread use case is interaction with a web server through the FastCGI protocol, utilizing no multithreading (but also for example CLI).

就是TS版可以跑多線程,NTS只能跑單線程。常見的就是Apache和IIS,Apache下用TS,IIS下用NTS。

 

配置文件是PHP目錄下的php-development.ini和php-dist.ini,開發期間把php-development.ini改成php.ini

   1. 模塊加載:

extension = php_mysqli.dll

 

   2. 修改模塊的目錄

     extension_dir = "D:/php/ext"

也可以將 D:/php ,D:/php/ext 添加到系統環境變量中,此處最好用絕對路徑

 

   3. 在Apache中配置php

更改httpd.conf

 

添加PHP模塊

LoadModule php7_module "D:/php/php7apache2_4.dll

 

配置php.in路徑

PHPIniDir "D:/php"

 

    配置AddType

    AddType application/x-httpd-php .php

    AddType application/x-httpd-php .txt

       

   4. register_globals = Off 設置是否開啟全局變量

   若設置為On

   已GET/POST提交的參數,直接可以使用變量用調用, 建議不開啟

   5.設置時區:date.timezone =PRC 

   6.設置session 

 

以上配置完成后,在命令行中運行php.exe,如果一切正常,不會有提示,否則會出現錯誤提示,例如缺少MSVC環境。

 

GOOD LUCK!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM