伺服器篇--之 LAMP安裝手記
Server OS: Cent OSApache: http-2.2.11PHP:PHP-5.2.9Mysql: 5.1.32 GD: gd-2.0.35 JPEG:jpeg-6blibpng:libpng-1.2.35libxml2:libxml2-2.6.19freetype:freetype-2.3.9
ZendOptimizer:ZendOptimizer-3.3.0a-linux-glibc21-i386
記得安裝Linux的時候不要安裝系統預設的apache,mysql和php以及相關的軟體。已經安裝的請用rpm -e * 刪除已經安裝的包。
一,安裝apache2
(這裡我把軟體包都放/root下了)# cd /root
進入目錄/root
# tar -zvxf httpd-2.2.11.tar.gz
解壓httpd-2.2.11.tar.gz
# cd httpd-2.2.11
進入httpd-2.2.11目錄# mkdir -p /usr/local/apache2
產生/usr/local/apache2目錄
# ./configure --prefix=/usr/local/apache2
或者
# ./configure --prefix=/usr/local/apache --enable-modules=so --enable-rewrite配置是否支援重寫# make編譯# make install
安裝apache
# vi /usr/local/apache2/conf/httpd.conf
設定DocumentRoot 的路徑儲存退出,現在就這麼簡單配置apache。
# /usr/local/apache2/bin/apachectl -k start
啟動apahce
用瀏覽器查看http://www.wengdo.com(這裡寫你自己網站,如http://localhost/),得到It works,說明apache已經配置成功了。
# /usr/local/apache2/bin/apachectl -k stop
停止apache# /usr/local/apache/bin/apachectl restart重啟Apache 把httpd 添加到系統的啟動項裡# cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd# chmod 700 /etc/rc.d/init.d/httpd# vi /etc/rc.d/init.d/httpd在第三行後增加# Comments to support chkconfig on RedHat Linux# chkconfig: 2345 90 90# description:http server注意:沒有這幾行,在使用chkconfig時會提示你:service httpd does not support chkconfig。chkconfig –add httpdchkconfig –level 345 httpd on這樣,在運行層級345上httpd就可以自動啟動了。
二,Mysql的安裝 # cd /root
# tar -zvxf mysql-5.1.32-linux-i686-glibc23.tar.gz
# mkdir -p /usr/local/mysql (省略也可)
# cp -r mysql-5.1.32-linux-i686-glibc23/* /usr/local/mysql
# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
添加mysql使用者及使用者組
# groupadd mysql
# useradd -g mysql mysql
修改mysql目錄許可權
# chown -R root /usr/local/mysql
# chgrp -R mysql /usr/local/mysql
# chown -R mysql /usr/local/mysql/data
產生mysql系統資料庫
# /usr/local/mysql/scripts/mysql_install_db --user=mysql&
啟動mysql服務
# /usr/local/mysql/bin/mysqld_safe --user=mysql&
如出現 Starting mysqld daemon with databases from /usr/local/mysql/data
代表正常啟動mysql服務了, 按Ctrl + C 跳出
修改 mysql 的 root 密碼
# /usr/local/mysql/bin/mysqladmin -u root -p password "wengdo"wengdo 這裡可以換為你自己的密碼 把你編譯目錄的一個指令碼COPY過去
# cp /usr/local/mysql/support-files/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的庫檔案地址。
三,安裝GD庫 (如果你用系統內建的GD庫,可以省略這一步)
建立目錄:
# mkdir -p /usr/local/jpeg6
# mkdir -p /usr/local/jpeg6/bin
# mkdir -p /usr/local/jpeg6/lib
# mkdir -p /usr/local/jpeg6/include
# mkdir -p /usr/local/jpeg6/man
# mkdir -p /usr/local/jpeg6/man1
# mkdir -p /usr/local/jpeg6/man/man1
# cd /root
# tar -zvxf jpegsrc.v6b.tar.gz
# cd jpeg-6b
# ./configure --prefix=/usr/local/jpeg6/ --enable-shared --enable-static
# make
# make install
- 安裝libpng
# cd /root
# tar -zvxf libpng-1.2.35.tar.gz
# cd libpng-1.2.35
# cp scripts/makefile.std makefile
# make
# make install
- 安裝 freetype
# cd /root
# tar -zvxf freetype-2.3.9.tar.gz
# cd freetype-2.3.9
# mkdir -p /usr/local/freetype
# ./configure --prefix=/usr/local/freetype
# make
# make install
# cd /root
# tar -zvxf gd-2.0.35.tar.gz
# mkdir -p /usr/local/gd2
# cd gd-2.0.35
# ./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg6/ --with-png=/usr/local/lib/ --with-zlib=/usr/local/lib/ --with-freetype=/usr/local/freetype/ --enable-m4_pattern_allow
# make
# make install
四,安裝PHP5
php5必須有libxml2支援!
安裝libxml2
# cd /root
# tar -zxvf libxml2-2.6.19.tar.gz
# cd libxml2-2.6.19
# mkdir -p /usr/local/libxml2
# ./configure --prefix=/usr/local/libxml2
# make# make install
安裝php5
# cd /root
# tar -zvxf php-5.2.9.tar.gz
# mkdir -p /usr/local/php
# cd php-5.2.9
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs
--with-mysql=/usr/local/mysql/
--with-curl=/usr/local/curl --enable-ftp
--with-libxml-dir=/usr/local/libxml2
--with-expat-dir=/usr/lib --enable-soap
--with-xsl=/usr/local/libxslt --enable-xslt
--with-gd=/usr/local/gd2/
--with-jpeg-dir=/usr/local/jpeg6/
--with-zlib-dir=/usr/local/lib/
--with-png=/usr/local/lib/
--with-freetype-dir=/usr/local/freetype/
# make
# make install
# cp php.ini-dist /usr/local/php/lib/php.ini (別忘記了)
其中./configure 後的
--prefix=/usr/local/php
--with-apxs2=/usr/local/apache2/bin/apxs
--with-mysql=/usr/local/mysql/
--with-libxml-dir=/usr/local/libxml2
是必要的選項
--with-gd=/usr/local/gd2/
--with-jpeg-dir=/usr/local/jpeg6/
--with-png=/usr/local/lib
--with-zlib-dir=/usr/lib
--with-freetype-dir=/usr/local/freetype
這是讓PHP支援GD庫的配置選項
--with-curl=/usr/local/curl 支援CURL庫
--enable-ftp 開啟FTP庫函數支援
--enable-soap --with-xsl=/usr/local/libxslt --enable-xslt
讓PHP支援SOAP, 上面這些一般用得少, 可以去掉
如果伺服器內建 GD庫及其相關的庫,可以用下面這個方式去配置PHP# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-freetype-dir=/usr --with-png-dir=/usr --with-gd --enable-gd-native-ttf --with-ttf --with-gdbm --with-gettext --with-iconv --with-jpeg-dir=/usr --with-png --with-zlib --with-xml --enable-calendar --with-apxs=/usr/local/apache2/bin/apxs
五,安裝 ZendOptimizer # cd /root# tar -zxvf ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz# cd ZendOptimizer-3.3.0a-linux-glibc21-i386# ./install.sh然後出現圖形介面,按提示Next 六,配置一些資訊
配置 httpd.conf 讓apache支援PHP
# vi /usr/local/apache2/conf/httpd.conf
找到 AddType application/x-gzip .gz .tgz 在其下添加如下內容
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
重啟apache
# /usr/local/apache2/bin/apachectl restart
在你設定的目錄裡建一內容為 <? phpinfo(); ?> PHP檔案, 輸入URL地址查看PHP配置是否正確
安裝時可能會出現的一些問題
編譯最新的GD庫2.0.35版本,用預設的 ./configure編譯
當make的時候,出現以下錯誤
configure.ac:64: error: possibly undefined macro: AM_ICONV
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
make: *** [configure] Error 1
解決方案:
編譯加m4_pattern_allow參數,上面已添加
# ./configure --enable-m4_pattern_allow
資料庫可能會出現這樣的錯誤
"Can't connect to local MySQL server through socket '/tmp/mysql.sock'"
檢查是否存在那個socket檔案,可以在/etc/my.cnf 中指定 socket = /tmp/mysql.sock
使用.mysqld_safe --socket=/tmp/mysql.sock & 啟動mysql進程,
有時pam_mysql使用/var/lib/mysql/mysql.sock
所以做個串連 ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
或者是修改/etc/my.cnf
有可能是許可權問題,看看mysql的錯誤記錄檔,一步一步調整
重啟Apache出錯
重新啟動apache報如下錯誤:
httpd: Syntax error on line 53 of /usr/local/apache2/conf/httpd.conf: Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied
原因:是Linux有一個SELinux保護模式引起的。
解決辦法:
1關閉SELINUX的方法:
# vi /etc/selinux/config
將SELINUX=enforcing 改成SELINUX=disabled 需要重啟
這個方法可能會對伺服器帶來風險。
2不關閉SELINUX的方法:
# setenforce 0
# chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache2/modules/libphp5.so
# service httpd restart
# setenforce 1
PHP 在Make 或者Make install的時候出錯,報錯資訊:
Installing PEAR environment: /usr/local/lib/php/
/usr/local/php-5.2.5/sapi/cli/php: error while loading shared libraries: /usr/local/gd2/lib/libgd.so.2: cannot restore segment prot after reloc: Permission denied
make[1]: *** [install-pear-installer] Error 127
make: *** [install-pear] Error 2
安裝出錯,這時候輸入 tail /var/log/messages 查看錯誤記錄檔
# tail /var/log/messages
...
Mar 3 06:40:46 localhost setroubleshoot: SELinux is preventing /usr/local/php-5.2.5/sapi/cli/php from loading /usr/local/gd2/lib/libgd.so.2.0.0 which requires text relocation. For complete SELinux messages. run sealert -l 6e29749f-3908-480d-bcc1-9042f8a9e846
你可以找到 run sealert -l 6e29749f-3908-480d-bcc1-9042f8a9e846,執行它
# sealert -l 6e29749f-3908-480d-bcc1-9042f8a9e846
...
The following command will allow this access:
chcon -t textrel_shlib_t /usr/local/gd2/lib/libgd.so.2.0.0
找到 following command chcon -t textrel_shlib_t /usr/local/gd2/lib/libgd.so.2.0.0
# chcon -t textrel_shlib_t /usr/local/gd2/lib/libgd.so.2.0.0
再次運行make install 或make
忘了mysql的root口令怎麼辦
# /usr/local/mysql/support-files/mysql.server stop
# mysqld_safe --skip-grant-tables &
# mysqladmin -u user password 'newpassword'
# mysqladmin flush-privileges