天天看點

php編譯安裝部署

雖然對php一竅不通,但是,php的環境還是得會搭建的嘛。

今天公司需要搭建php環境,查了些資料,搭建了一個,在這兒做個記錄,要是以後再需要的話,我就不需要再找資料了。

我大部分是參考這個文檔做的:http://www.cnblogs.com/lufangtao/archive/2012/12/30/2839679.html

1、下載下傳Apache

wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.20.tar.gz
           

 2、安裝Apache

tar -zxvf httpd-2.4.20.tar.gz 
cd httpd-2.4.20
mkdir /usr/local/apache2
###必須使用--enable-module=shared參數,不然php無法動态加載
./configure --prefix=/usr/local/apache2 --enable-module=shared
           

 2.1、此時若報錯如下

checking for APR... no
configure: error: APR not found.  Please read the documentation.
           

 解決方法:在Apache APR(http://apr.apache.org/)下載下傳apr和apr-util

2.2、下載下傳、安裝apr

wget http://mirror.bit.edu.cn/apache//apr/apr-1.5.2.tar.gz
tar -zxvf apr-1.5.2.tar.gz 
cd apr-1.5.2
mkdir /usr/local/apr
./configure --prefix=/usr/local/apr
make
make install
           

2.3、安裝pcre

wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.10/pcre-8.10.zip  
tar -zxvf pcre-8.10.zip 
unzip pcre-8.10.zip 
cd pcre-8.10
mkdir /usr/local/pcre
./configure --prefix=/usr/local/pcre
           

2.4、安裝apr-utl

wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
tar -zxvf apr-util-1.5.4.tar.gz 
cd apr-util-1.5.4
mkdir /usr/local/apr-url
./confiure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install
           

 2.5、重新編譯Apache

./configure --prefix=/usr/local/apache2 --enable-module=shared --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre 
make && make install
           

 3、若編譯成功則啟動Apache

cd /usr/local/apache2/bin
./apachectl -k start
           

4、配置Apache

vim /usr/local/apache2/conf/httpd.conf
#找到:
AddType  application/x-compress .Z
AddType application/x-gzip .gz .tgz
#在後面添加:
AddType application/x-httpd-php .php#使Apcche支援PHP
AddType application/x-httpd-php-source .php5   
#找到:
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
#添加:
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>    
#找到:
#ServerName www.example.com:80
#修改為:
ServerName 127.0.0.1:80或者ServerName localhost:80
#記得要去掉前面的“#”  
#修改完重新開機Apache
./apachectl -k restart
           

5、測試Apache啟動是否成功

curl http://localhost
           

 若可以得到It Works!的頁面說明Apache安裝成功

6、下載下傳php

wget http://cn2.php.net/get/php-7.0.7.tar.gz/from/this/mirror
           

 7、安裝php

tar -zxvf php-7.0.7.tar.gz 
cd php-7.0.7
mkdir /usr/local/php
./configure --prefix=/usr/local/php  --with-apxs2=/usr/local/apache2/bin/apxs
make
make test
make install
cp php.ini-development /usr/local/php/lib/php.ini
           

 8、安裝mysql (http://jingyan.baidu.com/article/acf728fd10c3d6f8e510a3ef.html)

yum list installed mysql*
rpm -qa | grep mysql*
yum install mysql
yum install mysql-server
yum install mysql-devel
#啟動mysql服務:
service mysqld start
#或者
/etc/init.d/mysqld start
#開機啟動:
chkconfig -add mysqld
#建立root密碼
mysqladmin -u root password 123456
           

 9、修改預設的Web站點目錄

 預設的目錄為  "/usr/local/apache2/htdocs",修改apache的配置檔案httpd.conf,比如在建立一個 /home/jhhome的目錄作為apache的站點目錄

    找到DocumentRoot這一行修改為:DocumentRoot "/home/jhhome"

   找到 <Directory> 這一行修改為:<Directory "/home/jhhome"> 

10、測試Web站點目錄是否修改成功

vi /usr/jhhome/index.html
#寫入
hello
           

 10.1、通路我們修改的站點

curl http://localhost
           

10.2、若出現如下錯誤:

“You don't have permission to access /index.html on this server.”

10.3、解決方法:

更改檔案權限;

chmod 755 index.html
           

打開apache配置檔案httpd.conf,找到<Directory /></Directory>,加入如下内容:(主要是Alow from all,表示允許擷取所有)

<Directory />
     Options FollowSymLinks
     AllowOverride None
     Order deny,allow
     Alow from all
     Satisfy all
</Directory>
           

 10.4、修改完重新開機httpd服務

./apachectl -k restart
           

 10.5、再次測試是否成功

curl http://localhost
           

得到hello字樣,表示修改Web站點目錄成功

11、測試php安裝是否成功

mv /usr/jhhome/index.html /usr/jhhome/index.php
vi /usr/jhhome/index.php
#寫入
<?php
phpinfo();
?>
           

 12、再次測試是否成功

curl http://localhost
           

若出現php版本資訊,說明php安裝成功

13、找到mysql_config路徑

find / -name mysql_config
           

 14、重新編譯安裝php,使php可以支援mysql  --with-mysqli後面更mysql_config的路徑

./configure --prefix=/usr/local/php  --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli=/usr/lib64/mysql/mysql_config
make
make test
make install
           

 15、測試php是否支援mysql

mysql -u root -p
#會提示輸入密碼,就用剛才設定的密碼
123456
#如果出現mysql>,說明連接配接成功了,下面通過指令 建立一個資料庫、建一個表,增加一條記錄,為後面的測試準備
mysql> create database gywtest;
mysql> use gywtest;
mysql> create table student(id int(4) not null primary key auto_increment,stuname char(20));
mysql> insert into student(stuname) values('Tom');
Ctrl+c 退出
vim /usr/jhhome/test.php
#加入以下内容
<?php 
  $mysqli=new mysqli();
  $mysqli->connect('localhost','root','123456','gywtest');
   // 建立查詢
   $sqlstr='select * from student';
  //發送查詢給MySql
   $result=$mysqli->query($sqlstr);
    while($row=$result->fetch_object())
    { 
      $name=$row->stuname;
       echo $name;
    }
?>
           

 16、再次測試是否成功

curl http://localhost/test.php
           

若出現tom字樣,說明php已支援mysql

繼續閱讀