天天看點

apache+mysql+php編譯如何讓Apache能自動運作index

1.安裝Apache 2.2.10

    雖然選擇了無包安裝CentOS,但是由于包依賴關系,CentOS還是會自動安裝Apache,是以需要首先解除安裝掉Apache,依次點選Applications->Add/Remove Software->把網際網路伺服器前面的勾去掉即可。

    開始編譯安裝Apache

    下載下傳httpd-2.2.10

    解壓 tar zxvf httpd-2.2.10.tar.gz

    進入目錄,開始編譯

            ./configure --sysconfdir=/etc --enable-ssl --enable-modules --enable-rewrite

    一般apache還需要支援url改寫,是以編譯時加上後面的部分;

    正常情況下,如果選擇了無包安裝CentOS,會出現下面的錯誤:

        checking whether to enable mod_ssl… checking dependencies

        checking for SSL/TLS toolkit base… none

        checking for OpenSSL version… checking openssl/opensslv.h usability… no

        checking openssl/opensslv.h presence… no

        checking for openssl/opensslv.h… no

        checking openssl/ssl.h usability… noz

        checking openssl/ssl.h presence… no

        checking for openssl/ssl.h… no

        no OpenSSL headers found

        checking for SSL-C version… checking sslc.h usability… no

        checking sslc.h presence… no

        checking for sslc.h… no

        no SSL-C headers found

        configure: error: …No recognized SSL/TLS toolkit detected

    解決辦法,安裝 openssl

        yum -y install openssl-devel

   對于ubuntu使用者安裝libssl

       apt-get install libssl-dev

    下載下傳安裝和更新的清單資訊

        Running Transaction

        Updating : e2fsprogs-libs               ####################### [ 1/15]

        Updating : krb5-libs                    ####################### [ 2/15]

        Installing: e2fsprogs-devel              ####################### [ 3/15]

        Installing: libsepol-devel               ####################### [ 4/15]

        Installing: libselinux-devel             ####################### [ 5/15]

         Installing: keyutils-libs-devel          ####################### [ 6/15]

        Installing: krb5-devel                   ####################### [ 7/15]

        Installing: zlib-devel                   ####################### [ 8/15]

        Installing: openssl-devel                ####################### [ 9/15]

        Updating : krb5-workstation             ####################### [10/15]

        Updating : e2fsprogs                    ####################### [11/15]

        Cleanup   : e2fsprogs-libs               ####################### [12/15]

        Cleanup   : krb5-workstation             ####################### [13/15]

        Cleanup   : e2fsprogs                    ####################### [14/15]

        Cleanup   : krb5-libs                    ####################### [15/15]

        Installed: openssl-devel.i386 0:0.9.8b-8.3.el5_0.2

        Dependency Installed: e2fsprogs-devel.i386 0:1.39-10.el5_1.1 keyutils-libs-devel.i386 0:1.2-1.el5 krb5-devel.i386 0:1.6.1-17.el5_1.1    libselinux-devel.i386 0:1.33.4-4.el5 libsepol-devel.i386 0:1.15.2-1.el5 zlib-devel.i386 0:1.2.3-3

        Dependency Updated: e2fsprogs.i386 0:1.39-10.el5_1.1 e2fsprogs-libs.i386 0:1.39-10.el5_1.1 krb5-libs.i386 0:1.6.1-17.el5_1.1             krb5-workstation.i386 0:1.6.1-17.el5_1.1

        Complete!

    OK, openssl安裝完畢。

    然後再次輸入./configure --sysconfdir=/etc --enable-ssl --enable-modules --enable-rewrite

    這樣就生成了makefile

    然後make和make install即可完成Apache安裝。

    終端輸入:/usr/local/apache2/bin/apachectl start啟動apache,

    終端輸入:netstat -tnl看看端口,如果有80了,說明apache啟動成功了。

    最後需要設定Apache随Redhat啟動,終端輸入:echo “/usr/local/apache2/bin/apachectl start &” >> /etc/rc.local

或者寫一個shell腳本,或者下載下傳一個啟動腳本

    cp httpd /etc/init.d/httpd    

    chkconfig /etc/init.d/httpd on

如何讓Apache能自動運作index

    編輯httpd.conf檔案,找到DirectoryIndex在後面加一個index.htm等。

    例如,把DirectoryIndex這行改為:

        DirectoryIndex index.html index.htm index.php index.html.var

    apache2的配置檔案是/etc/httpd.conf和/etc/extra/目錄下的檔案,需要幾個檔案一起來配置。在httpd.conf中指向/etc/extra/目錄下的配置檔案。

2.安裝MySQL

下載下傳MySQL

解壓縮 tar xvf mysql-5.1.32.tgz

./configure --prefix=/usr/local/mysql --sysconfdir=/etc --localstatedir=/var/lib/mysql --enable-local-infile --enable-assembler --with-charset=utf8  --with-big-tables  --with-plugin-partition --with-plugin-federated

make

make install

#prefix=/usr/local/mysql mysql安裝的目标目錄

#sysconfdir=/etc my.ini配置檔案的路徑

#localstatedir=/var/lib/mysql 資料庫存放的路徑

安裝完以後要初始化資料庫,當然你是更新的話不用做這步;

cp /usr/local/mysql/share/mysql/my-huge.cnf /etc/my.cnf

/usr/local/mysql/bin/mysql_install_db

如果系統沒有mysql這個使用者的話,最好做以下這步:

useradd -M -o -r -d /var/lib/mysql -s /bin/bash -c "MySQL Server" -u 27 mysql 

然後我啟動mysql

/usr/local/mysql/bin/safe_mysqld &

ok,先看看mysql能否正常工作

mysql -uroot mysql

一般情況下都是不能正常連結資料庫,錯誤提示一般為:

ERROR 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 

其實網上大家問的最多的都是整個問題,說什麼連結不到mysqld.sock,其實大家不妨看看mysql的錯誤日志就明白怎麼回事,我這裡的錯誤日志是在

/var/lib/mysql/*.err 你會發現mysql隻是以不能啟動,是因為/var/lib/mysql的權限不允許mysql服務通路,英文mysql預設是調用mysql使用者來啟動服務的,好了,既然知道是什麼原因找到不能啟動,那就簡單了。我們隻要

chown -R mysql:mysql /var/lib/mysql 就行,如果還是啟動不了,再慢慢調試權限,反正一般啟動不了都是權限的問題。

如果大家還是不能啟動不了的話,那就用我的比較繁瑣的權限的設定,反正我每次都是這麼做的,一般不會有問題,見下:

chown -R root /usr/local/mysql 

chgrp -R mysql /usr/local/mysql 

chown -R root /usr/local/mysql/bin 

chgrp -R mysql /usr/local/mysql/bin 

chgrp -R mysql /var/lib/mysql 

chmod 777 /var/lib/mysql 

chown -R root /var/lib/mysql/mysql 

chgrp -R mysql /var/lib/mysql/mysql 

chmod 777 /var/lib/mysql/mysql 

chown -R root /var/lib/mysql/mysql/* 

chgrp -R mysql /var/lib/mysql/mysql/* 

chmod 777 /var/lib/mysql/mysql/* 

chmod 777 /usr/local/mysql/lib/mysql/libmysqlclient.a

做完上面的步驟,然後把你編譯目錄的一個腳本COPY過去

cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld

chkconfig --add mysqld

用ntsysv設定使mysql每次啟動都能自動運作。

好了,至此mysql安裝完畢,你可以這樣起動你的mysql服務

/etc/rc.d/init.d/mysqld start

下面這步比較關鍵,

ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

ln -s /usr/local/mysql/include/mysql /usr/include/mysql

大家可以不做這步,大可以在編譯其他軟體的時候自定義myslq的庫檔案路徑,但我還是喜歡把庫檔案連結到預設的位置,這樣你在編譯類似PHP,Vpopmail等軟體時可以不用指定mysql的庫檔案位址。   ps: 如果是非編譯版本的mysql,在啟動服務的時候,需要修改 /etc/rc.d/init.d/mysql 将basedir修改到資料庫data存放位置即可。

3.編譯安裝GD庫,讓php支援更多圖形方面的内容,例如gif jpg(jpeg) png wbmp xml 和 ttf字型(此部分要在PHP安裝前進行,且按照以下順序進行安裝)

◆安裝ncurses-5.6

解壓縮 tar zxvf ncurses-5.6.tar.gz,進入目錄

./configure --prefix=/usr --with-shared --without-debug

make

make install

◆安裝zlib2

解壓縮 tar  zxvf zlib-1.2.3.tar.gz,進入目錄

./configure --prefix=/usr/local/zlib

make

make install

◆安裝PNG

解壓縮 tar zxvf libpng-1.2.26.tar.gz,進入目錄

#cp scripts/makefile.linux ./makefile

./configure --prefix=/usr/local/libpng

make

make install

◆安裝ttf ( ttf 是字型的支援 )

tar zxvf freetype-2.3.5.tar.gz

cd freetype-2.3.5

./configure --prefix=/usr/local/freetype

make

make install

… ◆.安裝JPEG6

tar zxvf jpegsrc.v6b.tar.gz

cd jpeg-6b

mkdir -pv /usr/local/libjpeg/{,bin,lib,include,man/man1,man1}

./configure --prefix=/usr/local/libjpeg --enable-shared --enable-static

make

make install

◆安裝 libxml2

tar zxvf libxml2-2.6.31.tar.gz

cd libxml2-2.6.31

./configure --prefix=/usr/local/libxml2

make

make install

cp xml2-config /usr/bin

◆安裝 libmcrypt-2.5.7.tar.gz

tar zxvf libmcrypt-2.5.7.tar.gz

cd libmcrypt-2.5.7

./configure

make

make install

#如果安裝過程提示./libtool找不到該指令 cp /usr/share/libtool/libtool .

◆安裝Fontconfig

tar -zxvf fontconfig-2.4.2.tar.gz

cd fontconfig-2.4.2

./configure --prefix=/usr/local/fontconfig --with-freetype-config=/usr/local/freetype/bin/freetype-config

一般這裡會出現一個錯誤提示如下:

checking for LIBXML2… configure: error: Package requirements (libxml-2.0 >= 2.6) were not met:

No package ‘libxml-2.0′ found

Consider adjusting the PKG_CONFIG_PATH environment variable if you

installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBXML2_CFLAGS

and LIBXML2_LIBS to avoid the need to call pkg-config.

See the pkg-config man page for more details.

解決方法:确定 /usr/local/libxml2/lib/pkgconfig 目錄下有 libxml-2.0.pc

export PKG_CONFIG_PATH=/usr/local/libxml2/lib/pkgconfig:$PKG_CONFIG_PATH

如果是64位系統則加上 --with-expat-lib=/usr/lib64

然後再次輸入:./configure --prefix=/usr/local/fontconfig --with-freetype-config=/usr/local/freetype/bin/freetype-config  ( --with-expat-lib=/usr/lib64    64位系統加)

make

make install

◆安裝GD庫

tar zxvf gd-2.0.35.tar.gz

cd gd-2.0.35

./configure --prefix=/usr/local/libgd --with-png=/usr/local/libpng --with-freetype --with-expat-lib=/usr/lib64 (64位系統添加)

如果前面安裝都順利,會看到下面的支援資訊:

Support for PNG library:          yes

Support for JPEG library:         yes

Support for Freetype 2.x library: yes

Support for Fontconfig library:   yes

Support for Xpm library:          no

Support for pthreads:             yes

繼續

make

make install

make時遇到可能遇到錯誤:

Makefile.am:18: The usual way to define `LIBTOOL' is to add `AC_PROG_LIBTOOL '

Makefile.am:18: to `configure.ac' and run `aclocal ' and `autoconf' again.

解決方法:安裝gettext-devel 後指令行運作 aclocal autoconf 兩個指令重新編譯 可能需要額外庫檔案支援一一下載下傳安裝,可從安裝CD光牒找到。  

還可能遇到 /usr/lib/libexpat.so: could not read symbols: File in wrong format

解決方法:這是以為是64位系統configure時添加 --with-expat-lib=/usr/lib64)如果還有錯誤 則替換 64位/usr/lib64/libexpat.so  到 /usr/lib/libexpat.so 注意備份  mv /usr/lib/libexpat.so /usr/lib/libexpat.so.bak cp /usr/lib64/libexpat.so /usr/lib/libexpat.so

4.安裝PHP5.2.10

tar zxvf php-5.2.10.tar.gz

cd php-5.2.10

./configure /

--prefix=/usr/local/php /

--with-apxs2=/usr/local/apache2/bin/apxs /

--with-mysql=/usr/local/mysql /

--with-gd-dir=/usr/local/libgd /

--enable-gd-native-ttf /

--with-ttf /

--enable-gd-jis-conv /

--with-freetype-dir=/usr/local/freetype /

--with-jpeg-dir=/usr/local/libjpeg /

--with-png-dir=/usr/local/libpng /

--with-libxml-dir=/usr/local/libxml2 /

--with-zlib-dir=/usr/local/zlib /

--enable-xml --enable-mbstring --enable-sockets

成功提示資訊是:Thank you for using PHP.

有些程式包系統已經安裝,則不需要再安裝一遍,使用

    rpm -ql xxx

來檢查是否已經安裝了。

    redhat 5 企業版已經安裝了gd、png等幾個,則執行下面的configure指令:

./configure /

--prefix=/usr/local/php5 /

--with-apxs2=/usr/local/apache2/bin/apxs /

--with-mysql=/usr/local/mysql /

--with-gd --enable-gd-native-ttf /

--with-ttf --enable-gd-jis-conv /

--with-freetype-dir=/usr/local/freetype /

--with-jpeg-dir=/usr/local/libjpeg /

--with-png-dir=/usr/lib /

--with-libxml-dir=/usr/local/libxml2 /

--with-zlib-dir=/usr/local/zlib /

--enable-xml --enable-mbstring /

--enable-sockets --enable-calendar

    make

    make install

    拷貝一個配置檔案當作我們以後的php.ini

        cp php.ini-development /usr/local/php5/lib/php/php.ini

    如果需要用到regist_globals的話,則修改php.ini檔案

        register_globals = On

    修改 apache 配置檔案 httpd.conf 加入對php的支援

        vi /etc/httpd.conf

    查找<IfModule mod_mime.c>; 

    在此範圍添加 

        AddType application/x-httpd-php .php 

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

    然後重新開機 apache

        /usr/local/apache2/bin/apachectl restart

    站點目錄是:/var/www/html

    最後我們還要添加上系統PATH,這樣就可以在終端直接輸入php即可運作程式,在終端輸入:

        vi .bashrc,加入這一行:export PATH=”$PATH:/usr/local/php5/bin”,

    然後登出目前使用者重新登入即可,注意,這種方式隻對目前使用者有效,不是全局設定。

    設定全局路徑可以編輯/etc/profile檔案,在最後加入

        PATH="$PAHT":/usr/local/php5/bin

    即可。

    連結php庫,以便于開發。

        ln -s /usr/local/php5/include /usr/include/php5

    然後寫個php測試頁info.php:内容如下 

    <?php 

        phpinfo(); 

    ?>; 

    正常的話,應該能看到php的資訊了.