1)本文主要和大家分享Linux下如何安裝配置PHP,下載PHP安裝包,這裡選用PHP 5.6版本,希望能協助到大家。
並解壓PHP安裝包
wget cn2.php.net/get/php-5.6.31.tar.gz/from/this/mirror
gzip -d php-5.6.31.tar.gz
tar xvf php-5.6.31.tar
2)添加www使用者組和www使用者(如果系統中已存在該使用者組,不用添加該使用者組)
groupadd www
useradd -g www -s /sbin/nologin -M www
3)使用su超級使用者登入Linux ,編譯PHP參數
su
#./configure
--prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-apxs=/usr/local/apache2/bin/apxs \
--prefix 指定安裝路徑
--with-mysql 指定mysql路徑
--with-apxs 指定apahce路徑
在接下來的編譯過程如果報錯,可以根據報錯調用 ./configure --help查看編譯參數,並下載安裝相應的依賴包;
4)編譯,安裝PHP
make
make install
5)修改Apache的設定檔,使其支援php, 對apache 的 httpd.conf 作以下修改
在“AddType application/x-gzip .gz .tgz”下添加
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .php5
在"<IfModule dir_module>"內的內容添加"index.php",即如下:
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
將"#ServerName www.example.com:80" 修改為
ServerName 127.0.0.1:80
或
ServerName localhost:80
6)複製php-5.6.31安裝包中的設定檔
cp php-5.6.31/php.ini.dist /usr/local/php/lib/php.ini
如果“php.ini.dist”不存在,可以將“php.ini-development ”或“php.ini-production”之一更改為“php.ini.dist”再執行以上指令
更改 php.ini 檔案
register_globals = On
7)測試安裝是否完成
重啟Apache
service apache restart
隨便編寫一個php 檔案(加入命名為test.php),放置在apache/htdoc中,在瀏覽器其中訪問:http://127.0.0.1/test.php
如果能夠正確顯示php檔案中的內容,則安裝成功;
快速安裝方式
在Centos下可以使用yum工具,在Ubuntu下使用apt-get工具,可以實現快速的安裝方式,用法基本一樣,這裡示範使用yum安裝PHP
1)查看系統中是否已經存在PHP
rpm -qa php
2)安裝PHP
yum -y install php
3)查看PHP安裝資訊
chkconfig php on
如果yum源上的php版本過低,比如要安裝php5.6版本,可以通過以下方法:
1)配置centos epel 和 remi源
# Centos 6
rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
#Centos 7
yum install epel-release
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
2)查看可安裝包
yum list --enablerepo=remi --enablerepo=remi-php56 | grep php
3)安裝PHP
yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof