LAMP源代码编译整理
生活随笔
收集整理的這篇文章主要介紹了
LAMP源代码编译整理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?在我們編譯安裝Apache 之前,要考慮的是讓Apache 在什么樣的模式下運行,因為從Apache 2.0
就加入了MPM(Multi-Processing Modules,多道處理模塊)。 Apache 2.0 在性能上的改善最吸引人。在支持POSIX 線程的Unix 系統上,Apache 可以通過不 同的MPM 運行在一種多進程與多線程相混合的模式下,增強部分配置的可擴充性能。相比于Apache 1.3,2.0 版本做了大量的優化來提升處理能力和可伸縮性,并且大多數改進在默認狀態下即可生效。 但是在編譯和運行時刻,2.0 也有許多可以顯著提高性能的選擇. 毫不夸張地說,MPM 的引入是Apache 2.0 最重要的變化。大家知道,Apache 是基于模塊化的設 計,而Apache 2.0 更擴展了模塊化設計到Web 服務器的最基本功能。服務器裝載了一種多道處理模 塊,負責綁定本機網絡端口、接受請求,并調度子進程來處理請求。擴展模塊化設計有兩個重要好 處: ◆ Apache 可以更簡潔、有效地支持多種操作系統; ◆ 服務器可以按站點的特殊需要進行自定制。 在用戶級,MPM 看起來和其它Apache 模塊非常類似。主要區別是在任意時刻只能有一種MPM 被 裝載到服務器中。prefork 的工作原理及配置 如果不用“--with-mpm”顯式指定某種MPM,prefork 就是Unix 平臺上缺省的MPM。它所采用 的預派生子進程方式也是Apache 1.3 中采用的模式。prefork 本身并沒有使用到線程,2.0 版使用 它是為了與1.3 版保持兼容性;另一方面,prefork 用單獨的子進程來處理不同的請求,進程之間 是彼此獨立的,這也使其成為最穩定的MPM 之一。
若使用prefork,在make 編譯和make install 安裝后,使用“httpd -l”來確定當前使用的 MPM,應該會看到prefork.c(如果看到worker.c 說明使用的是worker MPM,依此類推)。再查看 缺省生成的httpd.conf 配置文件,里面包含如下配置段:<IfModule prefork.c> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule>
prefork 的工作原理是,控制進程在最初建立“StartServers”個子進程后,為了滿足 MinSpareServers 設置的需要創建一個進程,等待一秒鐘,繼續創建兩個,再等待一秒鐘,繼續創 建四個……如此按指數級增加創建的進程數,最多達到每秒32 個,直到滿足MinSpareServers 設 置的值為止。這就是預派生(prefork)的由來。這種模式可以不必在請求到來時再產生新的進程。 從而減小了系統開銷以增加性能。 MaxSpareServers 設置了最大的空閑進程數,如果空閑進程數大于這個值,Apache 會自動 kill 掉一些多余進程。這個值不要設得過大,但如果設的值比MinSpareServers 小,Apache 會自 動把其調整為MinSpareServers+1。如果站點負載較大,可考慮同時加大MinSpareServers 和 MaxSpareServers。 MaxRequestsPerChild 設置的是每個子進程可處理的請求數。每個子進程在處理了 “MaxRequestsPerChild”個請求后將自動銷毀。0 意味著無限,即子進程永不銷毀。雖然缺省設 為0 可以使每個子進程處理更多的請求,但如果設成非零值也有兩點重要的好處: ◆ 可防止意外的內存泄漏; ◆ 在服務器負載下降的時侯會自動減少子進程數。 因此,可根據服務器的負載來調整這個值。筆者認為10000 左右比較合適。 MaxClients 是這些指令中最為重要的一個,設定的是Apache 可以同時處理的請求,是對 Apache 性能影響最大的參數。其缺省值150 是遠遠不夠的,如果請求總數已達到這個值(可通過 ps -ef|grep http|wc -l 來確認),那么后面的請求就要排隊,直到某個已處理請求完畢。這就是 系統資源還剩下很多而HTTP 訪問卻很慢的主要原因。系統管理員可以根據硬件配置和負載情況來 動態調整這個值。雖然理論上這個值越大,可以處理的請求就越多,但Apache 默認的限制不能大于 256。如果把這個值設為大于256,那么Apache 將無法起動。事實上,256 對于負載稍重的站點也 是不夠的。在Apache 1.3 中,這是個硬限制。如果要加大這個值,必須在“configure”前手工修 改的源代碼樹下的src/include/httpd.h 中查找256,就會發現“#define HARD_SERVER_LIMIT 256” 這行。把256 改為要增大的值(如4000),然后重新編譯Apache 即可。在Apache 2.0 中新加入了 ServerLimit 指令,使得無須重編譯Apache 就可以加大MaxClients。下面是筆者的prefork 配置段: <IfModule prefork.c> StartServers 10 MinSpareServers 10 MaxSpareServers 15 ServerLimit 2000 MaxClients 1000 MaxRequestsPerChild 10000 </IfModule>
上述配置中,ServerLimit 的最大值是20000,對于大多數站點已經足夠。如果一定要再加 大這個數值,對位于源代碼樹下server/mpm/prefork/prefork.c 中以下兩行做相應修改即可: #define DEFAULT_SERVER_LIMIT 256#define MAX_SERVER_LIMIT 20000
worker的工作原理及配置 相對于prefork,worker 是2.0 版中全新的支持多線程和多進程混合模型的MPM。由于使 用線程來處理,所以可以處理相對海量的請求,而系統資源的開銷要小于基于進程的服務器。但是, worker 也使用了多進程,每個進程又生成多個線程,以獲得基于進程服務器的穩定性。這種MPM 的 工作方式將是Apache 2.0 的發展趨勢。 在configure -with-mpm=worker 后,進行make 編譯、make install 安裝。在缺省生成的 httpd.conf 中有以下配置段:<IfModule worker.c> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule>
worker 的工作原理是,由主控制進程生成“StartServers”個子進程,每個子進程中包含 固定的ThreadsPerChild 線程數,各個線程獨立地處理請求。同樣,為了不在請求到來時再生成線 程,MinSpareThreads 和MaxSpareThreads 設置了最少和最多的空閑線程數;而MaxClients 設置了 所有子進程中的線程總數。如果現有子進程中的線程總數不能滿足負載,控制進程將派生新的子進 程。 MinSpareThreads 和MaxSpareThreads 的最大缺省值分別是75 和250。這兩個參數對Apache 的性能影響并不大,可以按照實際情況相應調節。
ThreadsPerChild 是worker MPM 中與性能相關最密切的指令。ThreadsPerChild 的最大缺 省值是64,如果負載較大,64 也是不夠的。這時要顯式使用ThreadLimit 指令,它的最大缺省值 是20000 。上述兩個值位于源碼樹server/mpm/worker/worker.c 中的以下兩行: #define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000 這兩行對應著ThreadsPerChild 和ThreadLimit 的限制數。最好在configure 之前就把64 改成所希望的值。注意,不要把這兩個值設得太高,超過系統的處理能力,從而因Apache 不起動使 系統很不穩定。 Worker 模式下所能同時處理的請求總數是由子進程總數乘以ThreadsPerChild 值決定的, 應該大于等于MaxClients。如果負載很大,現有的子進程數不能滿足時,控制進程會派生新的子 進程。默認最大的子進程總數是16,加大時也需要顯式聲明ServerLimit(最大值是20000)。這 兩個值位于源碼樹server/mpm/worker/worker.c 中的以下兩行:#define DEFAULT_SERVER_LIMIT 16
#define MAX_SERVER_LIMIT 20000 需要注意的是,如果顯式聲明了ServerLimit,那么它乘以ThreadsPerChild 的值必須大 于等于MaxClients,而且MaxClients 必須是ThreadsPerChild 的整數倍,否則Apache 將會自動調 節到一個相應值(可能是個非期望值)。下面是筆者的worker 配置段:<IfModule worker.c> StartServers 3 MaxClients 2000 ServerLimit 25 MinSpareThreads 50 MaxSpareThreads 200 ThreadLimit 200 ThreadsPerChild 100 MaxRequestsPerChild 0 </IfModule>
通過上面的敘述,可以了解到Apache 2.0 中prefork 和worker 這兩個重要MPM 的工作原 理,并可根據實際情況來配置Apache 相關的核心參數,以獲得最大的性能和穩定性。
源碼安裝LAMP
源代碼編譯都是使用C語言編寫的,需要在本機編譯才能安裝。 檢查gcc,可用命令gcc -v
可以卸載默認的低版本的環境
rpm -qa | grep -i httpd rpm -e httpXXXX yum remove
rpm -qa | grep -i mysql
rpm -qa | grep -i php
安裝libxml2 庫文件 libxml2 是個xml的c語言版的解析器。
# tar xf libxml2-2.6.30.tar.gz? # cd libxml2-2.6.30
# ./configure --prefix=/usr/local/libxml2 && make && make install
安裝libmcrypt庫 libmcrypt 加密解密庫 # tar xf libmcrypt-2.5.8.tar.gz # ./configure ?--prefix=/usr/local/libmcrypt && make && make install
安裝zlib庫
zlib是提供數據壓縮用的函式庫
# tar xf zlib-1.2.3.tar.bz2? # ./configure --prefix=/usr/local/zlib && make && make install
安裝libpng 庫 png圖片處理 ./configure --prefix=/usr/local/libpng && make && make install
安裝jpeg6庫 安裝GD2庫前所需的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/man1
# ./configure \ > --prefix=/usr/local/jpeg6 \ > --enable-shared \ ? ? ? ? ? ? ? ?#建立共享庫使用的GNU的libtool > --enable-static ? #建立靜態庫使用的GNU的libtool
# make && make install
安裝freetype ? 字體庫
# ./configure --prefix=/usr/local/freetype && make && make install
安裝autoconf 庫 Autoconf是一個用于生成可以自動地配置軟件源代碼包以適應多種Unix類系統的 shell腳本的工具。
# tar xf autoconf-2.63.tar.gz # ./configure && make && make install
安裝GD庫 php處理圖形的擴展庫,GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片
# tar xf gd-2.0.33.tar.gz # ./configure --prefix=/usr/local/gd2 --with-zlib=/usr/local/zlib/ --with-jpeg=/usr/local/jpeg6/ --with-png=/usr/local/libpng/ --with-freetype=/usr/local/freetype/
會看到 ** Configuration summary for gd 2.0.33:
? ?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: ? ? ? ? ?yes ? ?Support for pthreads: ? ? ? ? ? ? yes
? ?make && make install
安裝apache2 服務器 為了試驗采用worker模式
# tar xf httpd-2.2.17.tar.gz?
# ./configure --prefix=/usr/local/apache2work \ > --with-mpm=worker \ #指定worker模式 > --with-z=/usr/local/zlib/ \ #指定zlib庫的位置 > --with-include-apr \ #使用捆綁apr > --disable-userdir \ #請求映射到用戶特點目錄 > --sysconfdir=/etc/apache2 \ #配置文件位置 > --enable-so \ #以動態共享對象編譯 > --enable-deflate=shared #縮小傳輸碼 > --enable-expires=shared #期滿頭控制 > --enable-rewrite=shated #基于規則的URL > --enable-static-suport #建立一個靜態鏈接版本
make $$ make install
# cp apachectl /etc/init.d/apache # chmod +x /etc/init.d/apache? # /etc/init.d/apache start # netstat -anpt | grep 80 tcp ? ? ? ?0 ? ? ?0 0.0.0.0:80 ? ? ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?13116/httpd
可以看到五個進程。 ps -ef | grep httpd root ? ? 13116 ? ? 1 ?0 14:38 ? ? ? ? ?00:00:00 /usr/local/apache2work/bin/httpd -k start daemon ? 13117 13116 ?0 14:38 ? ? ? ? ?00:00:00 /usr/local/apache2work/bin/httpd -k start daemon ? 13118 13116 ?0 14:38 ? ? ? ? ?00:00:00 /usr/local/apache2work/bin/httpd -k start daemon ? 13120 13116 ?0 14:38 ? ? ? ? ?00:00:00 /usr/local/apache2work/bin/httpd -k start daemon ? 13121 13116 ?0 14:38 ? ? ? ? ?00:00:00 /usr/local/apache2work/bin/httpd -k start root ? ? 13221 15015 ?0 14:39 pts/1 ? ?00:00:00 grep httpd
觀察上面進程列表,發現 進程 13116 的父進程 pid 是1,同時它也是 進程的父進程。 為什么是5個進程呢?5個進程之間有什么關系呢? mpm_default.h 中設置了默認進程數(DEFAULT_START_DAEMON),最大進程數(DEFAULT_MAX_FREE_DAEMON),最小進程數(DEFAULT_MIN_FREE_DAEMON)。在沒有參數配置的情況下,會應用這些值。在 2.2.17 的版本中, DEFAULT_START_DAEMON 的值是 3。所以,上面的5個進程的組成是:一個listen 進程,3個 recv進程.還有一個進程,我就不知道啦。
修改配置文件 http.conf,加入以下內容,設置主線程為1: ? # worker MPM # StartServers: initial number of server processes to start # MaxClients: maximum number of simultaneous client connections # MinSpareThreads: minimum number of worker threads which are kept spare # MaxSpareThreads: maximum number of worker threads which are kept spare # ThreadsPerChild: constant number of worker threads in each server process # MaxRequestsPerChild: maximum number of requests a server process serves <IfModule mpm_worker_module> ? ? StartServers ? ? ? ? ?1 ? ? MaxClients ? ? ? ? ? 15 ? ? MinSpareThreads ? ? ? 1 ? ? MaxSpareThreads ? ? ? 1? ? ? ThreadsPerChild ? ? ? 1 ? ? MaxRequestsPerChild ? 0 </IfModule> ?查看進程 $ /usr/local/apache2worker/bin/apachectl restart $ ps -ef |grep httpd fancp ? ?15242 ? ? 1 ?0 23:19 ? ? ? ? ?00:00:00 /usr/local/apache2worker/bin/httpd -k start fancp ? ?16035 15242 ?0 23:45 ? ? ? ? ?00:00:00 /usr/local/apache2worker/bin/httpd -k start fancp ? ?16036 15242 ?0 23:45 ? ? ? ? ?00:00:00 /usr/local/apache2worker/bin/httpd -k start fancp ? ?16041 ?4762 ?0 23:45 pts/1 ? ?00:00:00 grep httpd
安裝MYSQL
# useradd mysql
# tar xf mysql-5.1.55.tar.gz
./configure --prefix=/usr/local/mysql \ --localstatedir=/data/mysql --enable-assembler \ --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static \ --with-pthread --enable-static --with-big-tables --without-ndb-debug \ --with-charset=utf8 --with-extra-charsets=all \ --without-debug --enable-thread-safe-client --enable-local-infile --with-plugins=max
--localstatedir=/data/mysql //庫文件存放目錄 --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static//靜態編譯安裝mysql 客戶端和服務端 --with-pthread //采用線程 --with-big-tables //對大表的支持 --with-charset=utf8 //默認字符集為utf8 --with-extra-charsets=all //安裝所有字符集 --without-debug //去掉debug 模式 --enable-thread-safe-client //以線程方式編譯客戶端 --with-plugins=max //添加對innodb 及partition 的支持 --enable-local-infile //對load data 的支持
make && make install
初始化數據庫 cd /usr/local/mysql/ mysql]# bin/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql
相應權限的修改 # chown -R root:mysql /usr/local/mysql/ # chown -R mysql:mysql /data/mysql/
配置文件 cp support-file/my-medium.cnf /etc/my.cnf cp support-files/mysql/mysql.server /etc/init.d/mysqld chmod 755 /etc/init.d/mysqld chkconfig --add mysqld
# vim /root/.bash_profile PATH=$PATH:$HOME/bin:/usr/local/mysql/bin # source /root/.bash_profile
啟動數據庫并初始化密碼。 # service mysqld start Starting MySQL [ OK ] # mysqladmin -u root password xxxx //設置成自己的密碼
安裝PHP # tar xf php-5.2.9.tar.gz php-5.2.9 ?./configure \ > --prefix=/usr/local/php \ > --with-config-file-path=/usr/local/php/etc \ //php5配置文件 > --with-apxs2=/usr/local/apache2work/bin/apxs \ //apahce2位置 > --with-mysql=/usr/local/mysql/ \ //mysql位置 > --with-libxml-dir=/usr/local/libxml2/? > --with-png-dir=/usr/local/libpng/ \ > --with-jpeg-dir=/usr/local/jpeg6/ \ > --with-freetype-dir=/usr/local/freetype/ \ > --with-gd=/usr/local/gd2/ \ > --with-zlib-dir=/usr/local/zlib/ \ > --with-mcrypt=/usr/local/libmcrypt/ \ > --with-mysqli=/usr/local/mysql/bin/mysql_config \ //激活mysqli的功能 > --enable-soap \ //變量激活SOAP和WEB servers > --enable-mbstring=all \ //多字節穿支持 > --enable-sockets //變量激活socket通信
+--------------------------------------------------------------------+ | 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 install
報錯 /usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit status make: *** [libphp5.la] Error 1
解決方案;安裝libtool-ltdl-devel 安裝完成后可使用 make test 測試
配置文件 # cp php.ini-dist /usr/local/php/etc/php.ini
與apache整合 在編譯時我們使用了--with-apxs2=/usr/local/apache2work/bin/apxs ,將php作為apache功能模塊使用 但是還需要修該配置文件,添加支持php解析。使用vi 編譯http.conf 在AddTyep application/x-gzip .gz .tgz下添加 AddType application/x-httpd-php .php .phtml
? # If the AddEncoding directives above are commented-out, then you ? ? # probably should define those extensions to indicate media types: ? ? # ? ? AddType application/x-compress .Z ? ? AddType application/x-gzip .gz .tgz ? ? AddType application/x-httpd-php .php .phtml 找到: <IfModule dir_module> DirectoryIndex index.html </IfModule> 將該行改為 <IfModule dir_module> DirectoryIndex index.html index.htm index.php </IfModule> 找到: #Include conf/extra/httpd-mpm.conf #Include conf/extra/httpd-info.conf #Include conf/extra/httpd-vhosts.conf #Include conf/extra/httpd-default.conf 去掉前面的“#”號,取消注釋。 注意:以上 4 個擴展配置文件中的設置請按照相關原則進行合理配置!
在htdocs下創建test.php <?php phpinfo(); ?> 重啟apache測試
安裝zend 加速器 為了提高php速度,理論上zend加速器可以提高40%-100%的速度。 tar xf ZendOptimizer-3.3.0a-linux-glibc21.i386 ./install.sh
會出現圖形界面 安裝提示:輸入Zend安裝的位置 /usr/local/zend 詢問php.ini的所在位置 /usr/local/php/etc 詢問是否安裝apache及其啟動文件位置 /usr/local/apache2work/bin/apachectl 詢問是否重啟apache服務
最后在test.php頁面上可以看到Zend的信息
安裝phpMyAdmin phpmyadmin是使用php語言開發的一個mysql管理軟件,通過web形式管理mysql。
# tar xf phpMyAdmin-3.2.0-all-languages.tar.bz2? # mv phpMyAdmin-3.2.0-all-languages /usr/local/apache2work/htdocs/phpmyadmin
在使用前。需要配置下創建config.inc.php,可以復制模板config.sample.inc.php # cp config.sample.inc.php config.inc.php
配置phpmyadmin 通過身份驗證模式,可以有2種方案。一種是http和cookie身份驗證模式。 這種做法的好處是,登錄時在對話框中提示輸入密碼。mysql數據庫密碼沒有出現在 config.inc.php文件里,而且可以支持多用戶管理。 第二種方案是:config驗證模式。密碼以明文寫在配置文件中。不會提示輸入密碼。
HTTP驗證模式 vi config.inc.php
$i++; /* Authentication type */ $cfg['Servers'][$i]['auth_type'] = 'http'; //把cookie換成http /* Server parameters */ $cfg['Servers'][$i]['host'] = 'localhost'; $cfg['Servers'][$i]['connect_type'] = 'tcp'; $cfg['Servers'][$i]['compress'] = false; /* Select mysqli if your server has it */ $cfg['Servers'][$i]['extension'] = 'mysql'
保存退出后,進去IP/phpmyadmin即可
COOKIE模式 vi config.inc.php
$i++; /* Authentication type */ $cfg['Servers'][$i]['auth_type'] = 'cookie'; //cookie模式 /* Server parameters */ $cfg['Servers'][$i]['host'] = 'localhost'; $cfg['Servers'][$i]['connect_type'] = 'tcp'; $cfg['Servers'][$i]['compress'] = false; /* Select mysqli if your server has it */ $cfg['Servers'][$i]['extension'] = 'mysql'
這樣啟動phpmyadmin還是需要輸入密碼用戶名
如果需要cookie模式 在配置文件中加入
$cfg['Servers'][$i]['auth_type'] = 'cookie'; /* Server parameters */ $cfg['Servers'][$i]['host'] = 'localhost'; $cfg['Servers'][$i]['connect_type'] = 'tcp'; $cfg['Servers'][$i]['compress'] = false; /* Select mysqli if your server has it */ $cfg['Servers'][$i]['extension'] = 'mysql';
//加入這兩行 $cfg['Servers'][$i]['user'] = 'root'; $cfg['Servers'][$i]['password'] = 'zhang';
轉載于:https://blog.51cto.com/zcnick/773479
總結
以上是生活随笔為你收集整理的LAMP源代码编译整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到朋友怀孕生孩子是什么意思
- 下一篇: 梦到自己买衣服是什么意思