CentOS編譯安裝所需開發(fā)包與相關(guān)系統(tǒng)環(huán)境構(gòu)架
特別值得一提的是CentOS編譯安裝有很多值得學(xué)習(xí)的地方,這里我們主要介紹CentOS編譯安裝相關(guān)系統(tǒng),包括介紹CentOS開發(fā)包等方面。CentOS編譯安裝mysql, apache, php, Zend Optimizer等基本環(huán)境。
1. 使用 yum 程序安裝所需開發(fā)包(以下為標(biāo)準(zhǔn)的rpm包名稱)
# yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel libxml2-devel gettext-devel pcre-devel
#這里我們將CentOS編譯安裝GD所必須的一些小軟件比如libpng,libtiff,freetype,libjpeg,gettext-devel等先用RPM 的方式一并安裝好,避免手動CentOS編譯安裝浪費時間,同時也能避免很多錯誤,這幾個小軟件的編譯很麻煩。這幾個小軟件編譯錯誤了,GD當(dāng)然安裝不了,php5的編譯當(dāng)然也沒戲了。所以我們抓大放小,對這些小牛鬼蛇神采取快速簡潔的方式進(jìn)行安裝。并且對服務(wù)器的性能也不能產(chǎn)生什么影響。
另外libxml2系統(tǒng)已經(jīng)默認(rèn)安裝了,所以我們不需要手工編譯了,直接安裝它的開發(fā)包就行了。
2. 源碼CentOS編譯安裝所需包 (Source)
(1) GD2
# cd /usr/local/src
# wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
# tar xzvf gd-2.0.35.tar.gz
# cd gd-2.0.35
# yum install libtool libtool-ltdl
# aclocal
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/gd2 --mandir=/usr/share/man
// 注意,CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" 這個環(huán)境參數(shù)只針對intel P4 芯片,如果你的CPU是AMD的,注意不能使用。請查看相應(yīng)的編譯優(yōu)化參數(shù)。否則程序會無法編譯,即使編譯成功也無法運行,嘿嘿。
關(guān)于其他CPU的優(yōu)化參見我的BLOG的一篇轉(zhuǎn)貼:
http://www.cnprint.org/bbs/blogs/1/blog43.html
//./configure 配置。
# make //make 是用來編譯的,它從 Makefile 中讀取指令,然后編譯。
# make install //make install 是用來對CentOS編譯安裝的,它也從 Makefile 中讀取指令,安裝到指定的位置。
(2) Apache 日志截斷程序
# cd /usr/local/src
# wget http://cronolog.org/patches/cronolog-1.7.0-beta.tar.gz
# tar cronolog-1.7.0-beta.tar.gz
# cd cronolog-1.7.0-beta
#CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/cronolog && make && make install
3、CentOS編譯安裝mysql 5.0.50
mysql 5.0.50是企業(yè)版本,貌似雙數(shù)版本都是企業(yè)版本了。個人覺得代碼質(zhì)量要比社區(qū)版本要好一些。大家可以下載,免費使用。并不需要向mysql公司交錢。
#cd /usr/local/src
# wget http://mirror.provenscaling.com/mysq...-5.0.50.tar.gz
# tar xzvf mysql-5.0.50.tar.gz
# cd mysql-5.0.50
修改mysql 客戶端最大連接數(shù), 默認(rèn)的只有100,遠(yuǎn)遠(yuǎn)達(dá)不到我們的要求。
# vi sql/mysqld.cc
搜索找到下面一行:
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.", (gptr*) &max_connections,
(gptr*) &max_connections, 0, GET_ULONG, REQUIRED_ARG, 100, 1, 16384, 0, 1,
0},
將其中的100改為1500, 當(dāng)然小點也可以,根據(jù)你的需要來,不建議改的太大。
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.", (gptr*) &max_connections,
(gptr*) &max_connections, 0, GET_ULONG, REQUIRED_ARG, 1500, 1, 16384, 0, 1,
0},
保存。
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-enterprise-gpl --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,latin1 --with-pthread --enable-static --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-innodb --without-ndb-debug --without-isam --enable-local-infile --with-readline --with-raid
配置成功會提示:
MySQL has a Web site at http://www.mysql.com/ which carries details on the
latest release, upcoming features, and other information to make your
work or play with MySQL more productive. There you can also find
information about mailing lists for MySQL discussion.
Remember to check the platform specific part of the reference manual for
hints about installing MySQL on your platform. Also have a look at the
files in the Docs directory.
Thank you for choosing MySQL!
# make
編譯的時間可能會比較長,畢竟優(yōu)化的比較厲害。
# make install
CentOS編譯安裝完成后執(zhí)行后續(xù)操作:
# useradd mysql //添加 mysql 用戶
# cd /usr/local/mysql
# bin/mysql_install_db --user=mysql
# chown -R root:mysql . //設(shè)置權(quán)限,注意后面有一個 "."
# chown -R mysql /var/lib/mysql //設(shè)置 mysql 目錄權(quán)限
# chgrp -R mysql . //注意后面有一個 "."
# cp share/mysql/my-medium.cnf /etc/my.cnf
# cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld //開機(jī)自動啟動 mysql。
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
#添加LIB PATH
echo "/usr/local/mysql/lib" >> /etc/ld.so.conf && ldconfig
vi /etc/my.cnf
修改 MySQL 配置,增加部分優(yōu)化參數(shù),如下:
[mysqld]
ft_min_word_len=2
運行以下命令即可啟動 MySQL 服務(wù)器:
# /etc/rc.d/init.d/mysqld start //啟動 MySQL
# bin/mysqladmin -u root password "password_for_root"
# service mysqld stop //關(guān)閉 MySQL
4. CentOS編譯安裝 Apache
# cd /usr/local/src
# wget http://www.ip97.com/apache.org/httpd/httpd-2.2.6.tar.gz
# tar zxvf httpd-2.2.6.tar.gz
# cd httpd-2.2.6
先依次安裝apr和apr-util
# cd srclib/apr
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/apr --enable-threads --enable-other-child --enable-static
# make && make install
# cd ../apr-util
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ --with-mysql=/usr/local/mysql
# make && make install
cd /usr/local/src/httpd-2.2.6
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --with-mysql=/usr/local/mysql --enable-cache --enable-file-cache --enable-mem-cache --enable-disk-cache --enable-static-support --enable-static-htpasswd --enable-static-htdigest --enable-static-rotatelogs --enable-static-logresolve --enable-static-htdbm --enable-static-ab --enable-static-checkgid --disable-cgid --disable-cgi --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --enable-ssl --with-ssl=/usr/include/openssl --with-pcre
# make
# make install
注解:
./configure //配置源代碼樹
--prefix=/usr/local/apache2 //體系無關(guān)文件的頂級安裝目錄PREFIX ,也就Apache的安裝目錄。
--enable-module=so //打開 so 模塊,so 模塊是用來提 DSO 支持的 apache 核心模塊
--enable-mods-shared=all //編譯全部的模板,對于不需要我們可以在httpd.conf去掉。
--enable-cache //支持緩存
--enable-file-cache //支持文件緩存
--enable-mem-cache //支持記憶緩存
--enable-disk-cache //支持磁盤緩存
--enable-static-support //支持靜態(tài)連接(默認(rèn)為動態(tài)連接)
--enable-static-htpasswd //使用靜態(tài)連接編譯 htpasswd - 管理用于基本認(rèn)證的用戶文件
--enable-static-htdigest //使用靜態(tài)連接編譯 htdigest - 管理用于摘要認(rèn)證的用戶文件
--enable-static-rotatelogs //使用靜態(tài)連接編譯 rotatelogs - 滾動 Apache 日志的管道日志程序
--enable-static-logresolve //使用靜態(tài)連接編譯 logresolve - 解析 Apache 日志中的IP地址為主機(jī)名
--enable-static-htdbm //使用靜態(tài)連接編譯 htdbm - 操作 DBM 密碼數(shù)據(jù)庫
--enable-static-ab //使用靜態(tài)連接編譯 ab - Apache HTTP 服務(wù)器性能測試工具
--enable-static-checkgid //使用靜態(tài)連接編譯 checkgid
--disable-cgid //禁止用一個外部 CGI 守護(hù)進(jìn)程執(zhí)行CGI腳本
--disable-cgi //禁止編譯 CGI 版本的 PHP
--enable-ssl // 編譯 ssl模塊。
我們不再使用worker模式編譯apache,worker模式和php貌似有一些不協(xié)調(diào)不穩(wěn)定之處。所以使用了默認(rèn)的perfork模式。
將apache設(shè)置成開機(jī)自啟動:
在/etc/rc.d/rc.local文件中加入一行
/usr/local/apache2/bin/apachectl start
這樣每次重新啟動系統(tǒng)以后,apache也會隨系統(tǒng)一起啟動.
或者將apache安裝為系統(tǒng)服務(wù)
# cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
然后 vi /etc/rc.d/init.d/httpd 添加(#!/bin/sh下面)
# chkconfig: 2345 50 90
# description: Activates/Deactivates Apache Web Server
最后,運行chkconfig把Apache添加到系統(tǒng)的啟動服務(wù)組里面:
# chkconfig --add httpd
# chkconfig httpd on
5、CentOS編譯安裝php 5.2.5
Suhosin是php增強型安全補丁,可以編譯到靜態(tài)內(nèi)核中,也可以編譯成php動態(tài)擴(kuò)展。我個人強烈你建議安裝成靜態(tài)內(nèi)核。Suhosin已經(jīng)進(jìn)入 Gentoo Linux、FreeBSD、OpenSuSE Linux、Mandriva Linux、Debian Linux官方包。下面的以下先說靜態(tài)安裝步驟。當(dāng)然你也可以在安裝php后將它編譯成php的動態(tài)擴(kuò)展。
# cd /usr/local/src
# wget http://cn.php.net/get/php-5.2.5.tar.gz/from/this/mirror
wget http://www.hardened-php.net/suhosin/...9.6.2.patch.gz
# tar zxvf php-5.2.5.tar.gz
# gunzip suhosin-patch-5.2.5-0.9.6.2.patch.gz
# cd php-5.2.5
# patch -p 1 -i ../suhosin-patch-5.2.5-0.9.6.2.patch
# ./buildconf --force
# CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-pear=/usr/share/php --with-zlib-dir --with-bz2 --with-libxml-dir=/usr --with-gd=/usr/local/gd2 --enable-gd-native-ttf --enable-gd-jis-conv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-ttf=shared,/usr --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/etc --with-iconv --disable-ipv6 --enable-static --enable-zend-multibyte --enable-inline-optimization --enable-zend-multibyte --enable-sockets --enable-soap --with-openssl --with-gettext --enable-suhosin
配置成功會提示:
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
# make
# make test
# make install
# cp php.ini-recommended /etc/php.ini
# echo "/usr/local/php/lib" >> /etc/ld.so.conf && ldconfig
在這里也順便說一下將suhosin安裝成為php的動態(tài)擴(kuò)展的方法。畢竟網(wǎng)上根本不見它的中文安裝教程。
雖然我個人不推薦這種方式。
wget http://www.hardened-php.net/suhosin/...sin-0.9.20.tgz
tar zxvf suhosin-0.9.20.tgz
cd suhosin-0.9.20
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
會提示編譯的模塊存在的目錄,記住它。
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20060613/
然后在php.ini中增加一行下列語句。
extension="/usr/local/php/lib/php/extensions/no-debug-zts-20060613/suhosin.so"
6、整合apache 與php
# vi /usr/local/apache2/conf/httpd.conf
在最后一行加上:
AddType application/x-httpd-php .php
查找:(設(shè)置 WEB 默認(rèn)文件)
DirectoryIndex index.html
修改為:
DirectoryIndex index.php index.html index.htm
找到這一段:
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride none
更改為AllowOverride all
允許apache rewrite
保存httpd.conf,退出。
# /usr/local/apache2/bin/apachectl restart //重啟 Apache
這時會出現(xiàn)錯誤:
/usr/local/apache2/bin/apachectl start
httpd: Syntax error on line 107 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
不急,我們慢慢解決。
這個Permission denied問題,在centos 5下面一般是Selinux引起的,作為生產(chǎn)用服務(wù)器,我建議你千萬別草率地關(guān)掉Selinux一了百了。就像家里的防盜網(wǎng),阻礙了你的貓自由進(jìn)出窗戶,你不能為了貓方便,就把防盜網(wǎng)簡單拆除是同樣的道理。我看見網(wǎng)上許多人建議把Selinux簡單關(guān)閉來解決這個問題,這是削足適履的做法,不值得提倡。
我們可以這樣操作:
# audit2allow -a //查看究竟問題出在什么地方
<no matches>
allow unconfined_t usr_t:file execmod;
allow useradd_t var_log_t:file { read write };
然后
# cd /etc/selinux/targeted/modules/
# audit2allow -M local -d
屏幕產(chǎn)生如下提示:
Generating type enforcment file: local.te
Compiling policy
checkmodule -M -m -o local.mod local.te
semodule_package -o local.pp -m local.mod
******************** IMPORTANT ***********************
In order to load this newly created policy package into the kernel,
you are required to execute
semodule -i local.pp
我們運行
# semodule -i local.pp
這樣就讓Selinux加載了新的規(guī)則。
更詳細(xì)的內(nèi)容請看我在BLOG上的轉(zhuǎn)貼:
http://www.cnprint.org/bbs/blogs/1/blog48.html
重啟apache
哈哈,apache不會再報錯了吧?
這樣我保留了selinux的功能,同時apache也能正常運行。
PHP5.1.x開始需要設(shè)置時區(qū),默認(rèn)時區(qū)與中國時區(qū)差8個小時,這種情況需要在php.ini中這么設(shè)置,找到date.timezone,去掉前面的分號,修改為以下值,大陸地區(qū)可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次為重慶,上海,烏魯木齊)
不然一些php程序的時間老是和中國標(biāo)準(zhǔn)時間相差8個小時。我的我的VBB論壇在windows上就是這樣。這兒有亞洲地區(qū)的對應(yīng)時區(qū)。
http://www.php.net/manual/en/timezones.asia.php
7、CentOS編譯安裝 Zend Optimizer
# cd /usr/local/src
# wget http://downloads.zend.com/optimizer/...21-i386.tar.gz
# tar xzvf ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
# ./ZendOptimizer-3.3.0-linux-glibc21-i386/install.sh
按照它的提示一步步進(jìn)行就行了。
總之一句話。如果你的服務(wù)器環(huán)境不需要ZendOptimizer,那么能不安就不安裝這個。避免和eaccelerator沖突。
8. 查看確認(rèn)CentOS編譯安裝 L.A.M.P 環(huán)境信息
vi /usr/local/apache2/htdocs/phpinfo.php
新增加下面一行,并保存。
<?php phpinfo(); ?>
# chmod 755 /usr/local/apache2/htdocs/phpinfo.php
用瀏覽器打開 http://192.168.9.150/phpinfo.php
檢查 phpinfo中的各項信息是否正確。
測試php與mysql的連接
# vi /usr/local/apache2/htdocs/testdb.php
增加下面幾行,并保存。
<?php
$link=mysql_connect('localhost','root','yourpassword');
if(!$link) echo "fail";
else echo "success";
mysql_close();
?>
# chmod 755 /usr/local/apache2/htdocs/testdb.php
# service mysqld start
用瀏覽器打開 http://192.168.9.150/testdb.php
如果輸出success就OK了
到這一步,一個基本的lamp就建立完成了。如果你是初學(xué)者,下面的步驟根據(jù)需要參考,不必全部照做,記住一句話,功能越多,越容易出錯,在任何領(lǐng)域都是這樣。
【編輯推薦】