天天看點

源碼編譯搭建LAMP環境

源碼編譯搭建LAMP環境

一:實驗目标

源碼編譯LAMP環境 

二:實驗環境

xuegod63.cn IP:192.168.1.63

LAMP環境的概述

    LAMP(Linux- Apache-MySQL-PHP)一般用來做網站架構的,是目前國際流行的Web架構,該架構包括:Linux作業系統,Apache網絡伺服器,MySQL資料 庫,Perl、PHP或者Python程式設計語言。LAMP具有通用、跨平台、高性能、低價格的 優勢,是以LAMP無論是性能、品質還是價格都是企業搭建網站的首選平台。

   對于大流量、大并發量的網站系統架構來說,除了硬體上使用高 性能的伺服器、負載均衡、CDN等之外,在軟體架構上需要重點關注下面幾個環節:使用高性能的作業系統(OS)、高性能的網頁伺服器(Web Server)、高性能的資料庫(Database)、高效率的程式設計語言等。下面我将從這幾點對其一一讨論。

作業系統

    Linux作業系統有很多個不同的發行版,如Red Hat Enterprise Linux、SUSE Linux Enterprise、Debian、Ubuntu、CentOS等,每一個發行版都有自己的特色,比如RHEL的穩定,Ubuntu的易用,基于穩定性 和性能的考慮,作業系統選擇CentOS(Community ENTerprise Operating System)是一個理想的方案。

Web伺服器、緩存和PHP加速

    Apache是LAMP架構最核心的Web Server,開源、穩定、子產品豐富是Apache的優勢。但Apache的缺點是有些臃腫,記憶體和CPU開銷大,性能上有損耗,不如一些輕量級的Web 伺服器(例如nginx)高效,輕量級的Web伺服器對于靜态檔案的響應能力來說遠高于Apache伺服器。

資料庫

    開源的資料庫中,MySQL在性能、穩定性和功能上是首選,可以達到百萬級别的資料存儲,網站初期可以将MySQL和Web伺服器放在一起,但是當通路 量達到一定規模後,應該将MySQL資料庫從Web Server上獨立出來,在單獨的伺服器上運作,同時保持Web Server和MySQL伺服器的穩定連接配接。

當資料庫通路量達到更大的級别,可以考慮使用MySQL Cluster等資料庫叢集或者庫表散列等解決方案。

總的來說,LAMP架構的網站性能會遠遠優于Windows IIS + ASP + Access(例如月光部落格)這樣的網站,可以負載的通路量也非常大,國内的大量個人網站如果想要支撐大通路量,采用LAMP架構是一個不錯的方案。

三:實驗代碼

LAMP=Linux+Apache+Mysql+PHP

1:安裝apache  

Apache源碼包:httpd-2.2.25.tar.gz

1)解壓httpd軟體包

[root@xuegod63 ~]# tar -zxvf httpd-2.2.25.tar.gz -C /usr/local/src/

[root@xuegod63 ~]#cd /usr/local/src/httpd-2.2.25 

[root@xuegod63 httpd-2.2.25]# yum install openssl*

2)源碼編譯安裝apache

[root@xuegod63 httpd-2.2.25]# ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite --enable-ssl

   配置參數用途:

--prefix=/usr/local/apache2  #存放網站的根目錄:

--enable-so # 支援動态加載子產品

--enable-rewrite #支援網站位址重寫

--enable-ssl # 支援ssl加密

[root@xuegod63 httpd-2.2.25]#make -j 4  #使用4個CPU來編譯 。不要使用大于你的實體CPU個數

[root@xuegod63 httpd-2.2.25]#make install

[root@xuegod63 apache2]# ls

bin    cgi-bin  error   icons    lib   man     modules

build  conf     htdocs  include  logs  manual

   http配置檔案位置: 

 [root@xuegod63 httpd-2.2.25]# ls /usr/local/apache2/conf/httpd.conf

/usr/local/apache2/conf/httpd.conf

  存放網站的根目錄: 

[root@xuegod63 httpd-2.2.25]# ls /usr/local/apache2/htdocs/

index.html

[root@xuegod63 httpd-2.2.25]# cat /usr/local/apache2/htdocs/index.html

<html><body><h1>It works!</h1></body></html>

3)啟用apache時,讓apache可以開機啟動并且可以使用service指令啟動apache伺服器

[root@xuegod63 httpd-2.2.25]# cp /usr/local/apache2/bin/apachectl  /etc/init.d/

[root@xuegod63 httpd-2.2.25]# /etc/init.d/httpd stop

[root@xuegod63 httpd-2.2.25]# chkconfig httpd off

#讓apache開機啟動,還要再在apachectl檔案的頭部的注釋中加兩條指令。

[root@xuegod63 httpd-2.2.25]# vim /etc/init.d/apachectl # 添加以下紅色内容

[root@xuegod63 httpd-2.2.25]# head !$

head /etc/init.d/apachectl

#!/bin/sh

# chkconfig: 2345 64 36 # 2345是系統級别下開些服務 ,64 啟動順序 , 36 關閉順序

# description: Activates/Deactivates all network interfaces configured to \

#可以參照:

[root@xuegod63 ~]# vim /etc/init.d/network

注:啟動時,要比network服務啟動的晚一些。先讓網絡伺服器啟動起來,其它依靠網絡相關的服務才可以正常啟動。 啟動的時候先啟動網絡,再啟動httpd,關閉的時候,先關閉httpd。再關閉網絡。

4)設定開機自動啟動:

[root@xuegod63 apache2]# chkconfig --add apachectl

[root@xuegod63 apache2]# chkconfig --list apachectl

apachectl 0:off 1:off 2:on 3:on 4:on 5:on6:off

5)啟動apache: 

[root@xuegod63 apache2]# /etc/init.d/apachectl start  #  啟動之後80端口就啟動了

6)測試:

注釋:源碼編譯安裝的apache運作身份是: daemon ;rpm安裝的httpd運作身份是:apache

<a href="http://s1.51cto.com/wyfs02/M00/89/EF/wKioL1gicweBf4eGAAaEHsxXhBc310.jpg" target="_blank"></a>

注:網站目錄權限設定: 

[root@xuegod63 htdocs]# id daemon

uid=2(daemon) gid=2(daemon) groups=2(daemon),1(bin),4(adm),7(lp)

[root@xuegod63 htdocs]# chown daemon:daemon kaixin001/data #這裡不需要寫============================================================

2:Mysql的源碼包安裝

msyql源碼包:MySQL-5.5.30-1.el6.src.rpm 

1)安裝前,如果不存在mysql 使用者,則建立

[root@xuegod63 ~]# useradd -s /sbin/nologin  mysql

[root@xuegod63 Desktop]# vim /etc/passwd #不讓mysql使用者登入系統

改成: mysql:x:501:501::/home/mysql:/sbin/nologin

2)解壓安裝

[root@xuegod63 ~]# tar zxvf mysql-5.5.30.tar.gz -C /usr/local/src/

[root@xuegod63 src]# cd /usr/local/src/mysql-5.5.30/

3)Mysql 5.5.15使用了新的cmake編譯方式,是以先安裝cmake

   cmake是什麼?

   CMake是一個跨平台的安裝(編譯)工具,可以用簡單的語句來描述所有平台的安裝(編譯過程)。他能夠輸出各種各樣的makefile或者project檔案,能測試編譯器所支援的C++特性,類似UNIX下的automake。

   安裝cmake:

[root@uplook mysql-5.5.30]# yum install -y cmake

#cmake-2.6.4-5.el6.x86_64.rpm軟體包,RHEL系統自帶,配置好yum源,

4)開始編譯:

[root@xuegod63 mysql-5.5.30]#  mkdir /server/

[root@xuegod63 mysql-5.5.30]# cmake -DCMAKE_INSTALL_PREFIX=/server/mysql-5.5   -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8   -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all   -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1   -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1   -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/server/mysql/data   -DMYSQL_USER=mysql  

cmake 編譯選項含意:

-DCMAKE_INSTALL_PREFIX=/server/mysql-5.5

#指定mysql安裝的根目錄,隻要/server目錄存在就可以了,mysql-5.5在安裝時,會自動建立。這個值可以在伺服器啟動時,通過--basedir來設定。

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock

#mysql伺服器用于監聽的套接字,這個必需是一個絕對路徑,預設是/tmp/mysql.sock。在伺服器啟動時,可通過--socket 來改變。

-DDEFAULT_CHARSET=utf8

設定mysql預設使用utf8字元集,不指定,預設使用latin1 西歐字元集。

-DDEFAULT_COLLATION=utf8_general_ci #預設字元校對。 db.opt

DWITH_EXTRA_CHARSETS=all #指定mysql擴充字元集支援所有的字元集,預設mysql支援所有字元集

-DWITH_MYISAM_STORAGE_ENGINE=1

-DWITH_INNOBASE_STORAGE_ENGINE=1

-DWITH_MEMORY_STORAGE_ENGINE=1

#靜态編譯Myisam、Innobase、Memory存儲引擎到mysql伺服器。這樣mysql伺服器就支援這三種存儲引擎了。

-DWITH_READLINE=1 #支援readline庫。

-DENABLED_LOCAL_INFILE=1 #允許從本地導入資料,啟用加載本地資料

-DMYSQL_DATADIR=/server/mysql/data#mysql資料庫存放資料的目錄

-DMYSQL_USER=mysql #指定運作mysql服務的使用者

注:具體編譯參數參考:

最終會像configure一樣生成Makefile。 

5)安裝:

[root@xuegod63 mysql-5.5.30]# make -j 4 

#注:-j 用來指定CPU核心數,可加快編譯速度。

[root@xuegod63 mysql-5.5.30]# make install  

  在編譯時,檢視CPU使用情況:

top-》P  檢視CPU使用情況:

cc1plus   #cc1plus是C++編譯器程式,用于該軟體對C++程式的編譯功能。

  擴充:

   gcc是GNU Compiler Collection,它的意思是“GNU的編譯器集合”,而不是“GNU C Compiler”.gcc就是所謂的front -end -driver, 由它驅動相應的編譯器、彙編器、連結器來完成整個由源代碼到可執行檔案的處理過程。CC程式叫做C Compiler。在linux中CC是gcc的一軟連結。

[root@xuegod63 mysql-5.5.30]# which cc

/usr/bin/cc

[root@xuegod63 mysql-5.5.30]# ll /usr/bin/cc

lrwxrwxrwx. 1 root root 3 Dec 18  2012 /usr/bin/cc -&gt; gcc

6)配置mysql運作環境: 

[root@xuegod63 mysql-5.5.30]# chown -R mysql:mysql /server/mysql-5.5#修改mysql安裝目錄權限,允許mysql使用者對mysql資料庫檔案夾讀寫。

複制mysql配置檔案

[root@xuegod63 mysql-5.5.30]#  cp /usr/local/src/mysql-5.5.30/support-files/my-large.cnf /etc/my.cnf

設定mysqld5.5服務開機啟動:

[root@xuegod63 ~]# chmod +x /etc/init.d/mysqld5.5

7)複制mysql開機啟動檔案,以後可以使用service指令來啟動和關閉mysql

[root@xuegod63 ~]# vim/etc/init.d/mysqld5.5(編輯此檔案,查找并修改以下變量内容:)

将原檔案中:

basedir=

datadir=

修改成:

basedir=/server/mysql-5.5 #mysql安裝目錄

datadir=/server/mysql-5.5/data #mysql資料庫存放資料的目錄

加入開機啟動項:

[root@xuegod63 Desktop]# chkconfig mysqld5.5 on

[root@xuegod63 Desktop]# chkconfig --list mysqld5.5

mysqld5.5 0:off 1:off 2:on 3:on 4:on 5:on 6:off

8)初始化編譯mysql資料庫:

[root@xuegod63 scripts]# pwd

/usr/local/src/mysql-5.5.30/scripts

[root@xuegod63 scripts]# chmod +x mysql_install_db

[root@xuegod63 scripts]# ./mysql_install_db --defaults-file=/etc/my.cnf --basedir=/server/mysql-5.5 --datadir=/server/mysql-5.5/data --user=mysql #類似于 rpm包

安裝的mysql資料庫,第一次啟動彈出的消息

........

You can test the MySQL daemon with mysql-test-run.pl

cd /server/mysql-5.5/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /server/mysql-5.5/scripts/mysqlbug script! 

[root@xuegod63 scripts]#

9)開啟mysql

[root@xuegod63 scripts]# /etc/init.d/mysqld5.5 start

Starting MySQL.... [ OK ]

10)測試登入:

[root@xuegod63 scripts]# mysql

11)設定mysqlroot密碼:

[root@xuegod63 Desktop]# mysqladmin -uroot password '123456'

[root@xuegod63 Desktop]# mysql -u root -p123456

12)設定mysql隻允許局域組中的伺服器和本地回環口連接配接3306端口:

[root@xuegod63 Desktop]# iptables -A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 3306 -j ACCEPT 

[root@xuegod63 Desktop]# iptables -A INPUT -s 127.0.0.1 -p tcp --dport 3306 -j ACCEPT

[root@xuegod63 Desktop]# iptables -A INPUT ! -s 127.0.0.1 -p tcp --dport 3306 -j DROP 

儲存規則:

[root@wt1 mysql-5.5.24]# /etc/init.d/iptables save

LAMP 都運作在一台機器上

============================================================

3:安裝PHP

PHP軟體包:php-5.4.14.tar.bz2

1)解壓 php-5.4.14.tar.bz2 軟體包

[root@xuegod63 ~]# tar -jxvf php-5.4.14.tar.bz2 -C /usr/local/src/

[root@xuegod63 ~]# cd /usr/local/src/php-5.4.14/

2)源碼編譯安裝

[root@xuegod63 php-5.4.14]# ./configure    --prefix=/server/php-5.4 --with-mysql=/server/mysql-5.5  --with-apxs2=/usr/local/apache2/bin/apxs  --with-config-file-path=/server/php-5.4

#彈出以一資訊,說明php環境檢查通過

。。。

Thank you for using PHP.

注解:

--prefix=/server/php-5.4  #安裝PHP的目錄

--with-apxs2=/usr/local/apache2/bin/apxs:用apache的apxs工具把php編譯成apache的一個子產品

--with-mysql=/usr/local/mysql:與mysql結合

--with-config-file-path=/usr/local/php#指定php配置檔案路徑

檢視伺服器原先參數:

[root@xuegod63 ~]# /server/php-5.4/bin/php -i | grep configure

Configure Command =&gt; './configure' '--prefix=/server/php-5.4' '--with-mysql=/server/mysql-5.5' '--with-apxs2=/usr/local/apache2/bin/apxs' '--with-config-file-path=/server/php-5.4'

PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_def

[root@xuegod63 ~]#make -j 4

[root@xuegod63 ~]#make install

擴充: 釋放cache : 不建意這樣做。 

[root@xuegod63 vm]# free -m

total used free shared buffers cached

Mem: 2297 1642 654 0 29 1183

-/+ buffers/cache: 429 1867

Swap: 999 0 999

[root@xuegod63 vm]# sync #把記憶體中沒有儲存的資料,寫到磁盤上。

[root@xuegod63 vm]# echo 3 &gt; /proc/sys/vm/drop_caches

Mem: 2297 1642 654 0 29 64

3:)生成php配置文檔:php.ini

[root@xuegod63 Desktop]# cp /usr/local/src/php-5.4.14/php.ini-production /server/php-5.4/php.ini

4)如果一切順利,會成功一個子產品:

[root@xuegod63 ~]# ls /usr/local/apache2/modules/

httpd.exp libphp5.so

讓apache支援PHP子產品:

[root@xuegod63 Desktop]# vim /usr/local/apache2/conf/httpd.conf

&lt;IfModule dir_module&gt;

DirectoryIndex index.html index.php #預設首頁支援index.php

&lt;/IfModule&gt;

在此檔案311行下,添加: 

AddType application/x-httpd-php .php

5)重新開機服務,測試Apache及php支援  

[root@xuegod63 Desktop]# cd /usr/local/apache2/htdocs/

[root@xuegod63 htdocs]# ls

[root@xuegod63 htdocs]# mv index.html index.html.back

[root@xuegod63 htdocs]# vim index.php

[root@xuegod63 htdocs]# cat index.php

&lt;?php

phpinfo();

?&gt;

[root@xuegod63 htdocs]# /etc/init.d/apachectl stop

[root@xuegod63 htdocs]# /etc/init.d/apachectl start

本文轉自 于學康 51CTO部落格,原文連結:http://blog.51cto.com/blxueyuan/1870900,如需轉載請自行聯系原作者