ubuntu下apache與php配置


實驗環境

uname -a

Linux tomato 4.4.0-34-generic #53-Ubuntu SMP Wed Jul 27 16:06:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

步驟

安裝

sudo apt-get update
sudo apt-get install php7.0 apache2 libapache2-mod-php7.0

修改配置

剛開始的時候我上網查詢如何配置apache2和php,很多人都提到了“在httpd.conf中AddType和LoadModule”,然而我找了很久都沒找到這個文件,其他各種答案也是沒效果。可能這個httpd.conf要下載源碼才有吧,用apt安裝的沒有。於是我去下載了源碼,但又是一大堆的依賴包什么的,也是失敗了。在圖書館搞了4個小時,感覺身體被掏空......

后來我從圖書館回到宿舍,平靜了一下,打開了剛剛看過的配置文件:

cd /etc/apache2
less apache2.conf

我被注釋里的目錄數吸引了:

於是我認真地看了開頭兩頁長的注釋:

Summary of how the Apache 2 configuration works in Debian:

The Apache 2 web server configuration in Debian is quite different to
upstream's suggested way to configure the web server. This is because Debian's
default Apache2 installation attempts to make adding and removing modules,
virtual hosts, and extra configuration directives as flexible as possible, in
order to make automating the changes and administering the server as easy as
possible.

apache2在Debian中的配置不同,配置文件分成了幾個文件。

apache2.conf is the main configuration file (this file). It puts the pieces
together by including all remaining configuration files when starting up the
web server

apache2.conf是主要的配置文件,將其他配置文件整合在一起。

Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
directories contain particular configuration snippets which manage modules,
global configuration fragments, or virtual host configurations,
respectively.

They are activated by symlinking available configuration files from their
respective *-available/ counterparts.

mods-enabled/、conf-enabled/和sites-enabled/這幾個目錄分別管理modules,
global configuration fragments和virtual host configurations。它們鏈接到相應的-available文件而起作用。

看到modules我馬上就動了:

cd mods-available 
grep -d recurse "AddType"

mime.conf: # AddType allows you to add to or override the MIME configuration
mime.conf: #AddType application/x-gzip .tgz
mime.conf: AddType application/x-compress .Z
mime.conf: AddType application/x-gzip .gz .tgz
mime.conf: AddType application/x-bzip2 .bz2
mime.conf: AddType text/html .shtml
ssl.conf: AddType application/x-x509-ca-cert .crt
ssl.conf: AddType application/x-pkcs7-crl .crl

嘿嘿......

sudo vim mime.conf

在文件中定位到AddType的部分,加入這兩行:

AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php

保存退出。再來找一下“LoadModule”的歸處:

mime.load:LoadModule mime_module /usr/lib/apache2/modules/mod_mime.so
sudo vim mime.load

加入這一句:

LoadModule php7_module /usr/lib/apache2/modules/libphp7.0.so

重啟apache2,使配置生效:

sudo service apache2 restart

測試

apache2的默認web根目錄在/etc/apache2/sites-available/000-default.conf中設置:

DocumentRoot /var/www/html/

在該目錄下添加測試文件info.php,內容為:

<?php
phpinfo();
?>

保存退出。

打開瀏覽器,在地址欄輸入:

localhost/phpinfo.php

回車。成功跳出由php解釋后的信息:

PHP logo
PHP Version 7.0.8-0ubuntu0.16.04.2

System    Linux tomato 4.4.0-34-generic #53-Ubuntu SMP Wed Jul 27 16:06:39 UTC 2016 x86_64
Server API    Apache 2.0 Handler
Virtual Directory Support    disabled
Configuration File (php.ini) Path    /etc/php/7.0/apache2
Loaded Configuration File    /etc/php/7.0/apache2/php.ini
Scan this dir for additional .ini files    /etc/php/7.0/apache2/conf.d
......

感動!


免責聲明!

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



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