天天看點

LAMP編譯部署

LAMP 就是Linux apache mysql php 本次實驗環境為CentOS6.7

httpd版本2.4.23 mariadb版本5.5.36 php版本5.6.26

一、編譯安裝apache

1、解決依賴關系

  httpd-2.4.23新版本的apr和apr-util,是以需要事先對其進行更新。更新方式有兩種,一種是通過源代碼編譯安裝,一種是直接更新rpm包。這裡選擇使用編譯源代碼的方式進行

 (1) 編譯安裝apr

<code>[root@centfils ~]</code><code># wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.bz2</code>

<code>[root@centfils ~]</code><code># tar xf apr-1.5.2.tar.bz2 </code>

<code>[root@centfils ~]</code><code># cd apr-1.5.2</code>

<code>[root@centfils apr-1.5.2]</code><code># ./configure --prefix=/usr/local/apr</code>

<code>[root@centfils apr-1.5.2]</code><code># make &amp;&amp; make install</code>

 (2) 編譯安裝apr-util

<code>[root@centfils ~]</code><code># wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.5.4.tar.bz2</code>

<code>[root@centfils ~]</code><code># tar xf apr-util-1.5.4.tar.bz2 </code>

<code>[root@centfils ~]</code><code># cd apr-util-1.5.4</code>

<code>[root@centfils apr-util-1.5.4]</code><code># ./configure --prefix=/usr/local/apr-util  --with-apr=/usr/local/apr</code>

<code>#指明編譯安裝路徑,并指明apr所在位置</code>

<code>[root@centfils apr-util-1.5.4]</code><code># make &amp;&amp; make install</code>

 (3) httpd-2.4.9編譯過程也要依賴于pcre-devel軟體包,需要事先安裝。此軟體包系統CD光牒自帶,是以,找到并安裝即可。

<code>[root@centfils ~]</code><code># yum install pcre-devel -y</code>

2、編譯安裝httpd-2.4.23

  首先下載下傳httpd-2.4.23到本地,而後執行如下指令進行編譯安裝過程:

<code>[root@centfils ~]</code><code># wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.23.tar.bz2</code>

<code>[root@centfils ~]</code><code># tar xf httpd-2.4.23.tar.bz2 </code>

<code>[root@centfils ~]</code><code># cd httpd-2.4.23</code>

<code>[root@centfils httpd-2.4.23]</code><code># ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all</code>

<code>#含義:--perfix=/usr/local/apache 指定安裝路徑;</code>

<code>--sysconfdir=</code><code>/etc/httpd</code> <code>指定配置檔案路徑;</code>

<code>--</code><code>enable</code><code>-so  apache核心裝載DSO,但實際不編譯任何動态子產品;</code>

<code>--</code><code>enable</code><code>-ssl 支援ssl子產品;</code>

<code>--</code><code>enable</code><code>-cgi支援cgi子產品;</code>

<code>--</code><code>enable</code><code>-rewrite 支援url重寫;</code>

<code>--with-zlib支援zlib庫檔案;</code>

<code>--with-pcre 包含pcre函數;</code>

<code>--with-apr=</code><code>/usr/local/apr</code> <code>指定apr的路徑;</code>

<code>--with-apr-util=</code><code>/usr/local/apr-util</code> <code>指明apr-util路徑;</code>

<code>--</code><code>enable</code><code>-modules=most 支援子產品;支援共享子產品;</code>

<code>--with-mpm=event加載子產品event</code>

<code>[root@centfils httpd-2.4.23]</code><code># make &amp;&amp; make install</code>

補充:

(1)建構MPM為靜态子產品

在全部平台中,MPM都可以建構為靜态子產品。在建構時選擇一種MPM,連結到伺服器中。

如果要改變MPM,必須重新建構。為了使用指定的MPM,請在執行configure腳本 時,使用參數 --with-mpm=NAME。NAME是指定的MPM名稱。

編譯完成後,可以使用 ./httpd -l 來确定選擇的MPM。 此指令會列出編譯到伺服器程式中的所有子產品,包括 MPM。

(2)建構MPM為動态子產品

在Unix或類似平台中,MPM可以建構為動态子產品,與其它動态子產品一樣在運作時加載。 

建構 MPM 為動态子產品允許通過修改LoadModule指令内容來改變MPM,而不用重新建構伺服器程式。

在執行configure腳本時,使用--enable-mpms-shared選項即可啟用此特性。

當給出的參數為all時,所有此平台支援的MPM子產品都會被安裝。還可以在參數中給出子產品清單。

預設MPM,可以自動選擇或者在執行configure腳本時通過--with-mpm選項來指定,然後出現在生成的伺服器配置檔案中。編輯LoadModule指令内容可以選擇不同的MPM。

3、修改httpd的主配置檔案,設定其Pid檔案的路徑

編輯/etc/httpd24/httpd.conf,添加如下行即可:

PidFile  "/var/run/httpd.pid"

4、提供SysV服務腳本/etc/rc.d/init.d/httpd,内容如下:

<code>#!/bin/bash</code>

<code>#</code>

<code># httpd        Startup script for the Apache HTTP Server</code>

<code># chkconfig: - 85 15</code>

<code># description: Apache is a World Wide Web server.  It is used to serve \</code>

<code>#        HTML files and CGI.</code>

<code># processname: httpd</code>

<code># config: /etc/httpd/conf/httpd.conf</code>

<code># config: /etc/sysconfig/httpd</code>

<code># pidfile: /var/run/httpd.pid</code>

<code># Source function library.</code>

<code>. </code><code>/etc/rc</code><code>.d</code><code>/init</code><code>.d</code><code>/functions</code>

<code>if</code> <code>[ -f </code><code>/etc/sysconfig/httpd</code> <code>]; </code><code>then</code>

<code>        </code><code>. </code><code>/etc/sysconfig/httpd</code>

<code>fi</code>

<code># Start httpd in the C locale by default.</code>

<code>HTTPD_LANG=${HTTPD_LANG-</code><code>"C"</code><code>}</code>

<code># This will prevent initlog from swallowing up a pass-phrase prompt if</code>

<code># mod_ssl needs a pass-phrase from the user.</code>

<code>INITLOG_ARGS=</code><code>""</code>

<code># Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server</code>

<code># with the thread-based "worker" MPM; BE WARNED that some modules may not</code>

<code># work correctly with a thread-based MPM; notably PHP will refuse to start.</code>

<code># Path to the apachectl script, server binary, and short-form for messages.</code>

<code>apachectl=</code><code>/usr/local/apache/bin/apachectl</code>

<code>httpd=${HTTPD-</code><code>/usr/local/apache/bin/httpd</code><code>}</code>

<code>prog=httpd</code>

<code>pidfile=${PIDFILE-</code><code>/var/run/httpd</code><code>.pid}</code>

<code>lockfile=${LOCKFILE-</code><code>/var/lock/subsys/httpd</code><code>}</code>

<code>RETVAL=0</code>

<code>start() {</code>

<code>        </code><code>echo</code> <code>-n $</code><code>"Starting $prog: "</code>

<code>        </code><code>LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS</code>

<code>        </code><code>RETVAL=$?</code>

<code>        </code><code>echo</code>

<code>        </code><code>[ $RETVAL = 0 ] &amp;&amp; </code><code>touch</code> <code>${lockfile}</code>

<code>        </code><code>return</code> <code>$RETVAL</code>

<code>}</code>

<code>stop() {</code>

<code>  </code><code>echo</code> <code>-n $</code><code>"Stopping $prog: "</code>

<code>  </code><code>killproc -p ${pidfile} -d 10 $httpd</code>

<code>  </code><code>RETVAL=$?</code>

<code>  </code><code>echo</code>

<code>  </code><code>[ $RETVAL = 0 ] &amp;&amp; </code><code>rm</code> <code>-f ${lockfile} ${pidfile}</code>

<code>reload() {</code>

<code>    </code><code>echo</code> <code>-n $</code><code>"Reloading $prog: "</code>

<code>    </code><code>if</code> <code>! LANG=$HTTPD_LANG $httpd $OPTIONS -t &gt;&amp;</code><code>/dev/null</code><code>; </code><code>then</code>

<code>        </code><code>echo</code> <code>$</code><code>"not reloading due to configuration syntax error"</code>

<code>        </code><code>failure $</code><code>"not reloading $httpd due to configuration syntax error"</code>

<code>    </code><code>else</code>

<code>        </code><code>killproc -p ${pidfile} $httpd -HUP</code>

<code>    </code><code>fi</code>

<code>    </code><code>echo</code>

<code># See how we were called.</code>

<code>case</code> <code>"$1"</code> <code>in</code>

<code>  </code><code>start)</code>

<code>  </code><code>start</code>

<code>  </code><code>;;</code>

<code>  </code><code>stop)</code>

<code>  </code><code>stop</code>

<code>  </code><code>status)</code>

<code>        </code><code>status -p ${pidfile} $httpd</code>

<code>  </code><code>restart)</code>

<code>  </code><code>condrestart)</code>

<code>  </code><code>if</code> <code>[ -f ${pidfile} ] ; </code><code>then</code>

<code>    </code><code>stop</code>

<code>    </code><code>start</code>

<code>  </code><code>fi</code>

<code>  </code><code>reload)</code>

<code>        </code><code>reload</code>

<code>  </code><code>graceful|help|configtest|fullstatus)</code>

<code>  </code><code>$apachectl $@</code>

<code>  </code><code>*)</code>

<code>  </code><code>echo</code> <code>$</code><code>"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"</code>

<code>  </code><code>exit</code> <code>1</code>

<code>esac</code>

<code>exit</code> <code>$RETVAL</code>

 或者

<code>[root@centfils init.d]</code><code># cp httpd httpd24</code>

<code>[root@centfils init.d]</code><code># vim httpd24</code>

<code>修改其中的路徑</code>

<code>pidfile=${PIDFILE-</code><code>/var/run/httpd/httpd24</code><code>.pid}</code>

<code>lockfile=${LOCKFILE-</code><code>/var/lock/subsys/httpd24</code><code>}</code>

 而後為此腳本賦予執行權限:

<code>[root@centfils ~]</code><code># chmod +x /etc/rc.d/init.d/httpd24</code>

 加入服務清單:

<code>[root@centfils ~]</code><code># chkconfig --add httpd24</code>

 接下來就可以啟動服務進行測試了。

<code>[root@centfils ~]</code><code># service httpd24 start</code>

 如有老版本,更改path路徑,并清除緩存

<code>[root@centfils htdocs]</code><code># vim /etc/profile.d/httpd.sh</code>

<code>export</code> <code>PATH=</code><code>/usr/local/apache/bin</code><code>:$PATH</code>

<code>[root@centfils ~]</code><code># . /etc/profile.d/httpd.sh</code>

<code>[root@centfils htdocs]</code><code># hash -r</code>

二、安裝mysql-5.5.36

1、準備資料存放的檔案系統

<code>[root@centfils ~]</code><code># pvcreate /dev/sdd1</code>

<code>[root@centfils ~]</code><code># pvcreate /dev/sdd2</code>

<code>[root@centfils ~]</code><code># vgcreate vgdata -s 8 /dev/sdd1 /dev/sdd2</code>

<code>[root@centfils ~]</code><code># lvcreate -L 5G -n mylv1 vgdata </code>

<code>[root@centfils ~]</code><code># mke2fs -t ext4 /dev/vgdata/mylv1</code>

<code>[root@centfils ~]</code><code># mkdir -pv /mydata/data</code>

<code>[root@centfils ~]</code><code># mount /dev/vgdata/mylv1 /mydata/data</code>

2、建立使用者以安全方式運作程序

<code>[root@centfils ~]</code><code># groupadd -r mysql</code>

<code>[root@centfils ~]</code><code># useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql</code>

<code>[root@centfils ~]</code><code># chown -R mysql:mysql /mydata/data</code>

3、安裝并初始化mysql-5.5.36

<code>[root@centfils ~]</code><code># tar xf mariadb-5.5.36-linux-i686.tar.gz -C /usr/local</code>

<code>[root@centfils ~]</code><code># cd /usr/local</code>

<code>[root@centfils </code><code>local</code><code>]</code><code># ln -sv mariadb-5.5.36-linux-i686 mysql</code>

<code>[root@centfils </code><code>local</code><code>]</code><code># cd mysql</code>

<code>[root@centfils mysql]</code><code># chown -R mysql:mysql .</code>

<code>[root@centfils mysql]</code><code># scripts/mysql_install_db --user=mysql --datadir=/mydata/data</code>

<code>[root@centfils mysql]</code><code># chown -R root  .</code>

4、為mysql提供主配置檔案

<code>[root@centfils ~]</code><code># cd /usr/local/mysql</code>

<code>[root@centfils mysql]</code><code># cp support-files/my-large.cnf  /etc/my.cnf</code>

另外修改并添加如下内容

<code>thread_concurrency = 2</code>

<code>datadir = </code><code>/mydata/data</code>

<code>innodb_file_per_table = on</code>

<code>skip_name_resolve = on</code>

5、為mysql提供sysv服務腳本

<code>[root@centfils mysql]</code><code># cp support-files/mysql.server  /etc/rc.d/init.d/mysqld</code>

<code>[root@centfils mysql]</code><code># chmod +x /etc/rc.d/init.d/mysqld</code>

<code>添加至服務清單:</code>

<code>[root@centfils mysql]</code><code># chkconfig --add mysqld</code>

<code>[root@centfils mysql]</code><code># chkconfig mysqld on</code>

為了使用mysql的安裝符合系統使用規範,并将其開發元件導出給系統使用,這裡還需要進行如下步驟:

6、輸出mysql的man手冊至man指令的查找路徑:

編輯/etc/man.config,添加如下行即可:

MANPATH  /usr/local/mysql/man

7、輸出mysql的頭檔案至系統頭檔案路徑/usr/include:

這可以通過簡單的建立連結實作:

<code>[root@centfils ~]</code><code># ln -sv /usr/local/mysql/include  /usr/include/mysql</code>

8、輸出mysql的庫檔案給系統庫查找路徑

<code>[root@centfils ~]</code><code># echo '/usr/local/mysql/lib' &gt; /etc/ld.so.conf.d/mysql.conf</code>

而後讓系統重新載入系統庫:

<code>[root@centfils ~]</code><code># ldconfig</code>

9、修改PATH環境變量,讓系統可以直接使用mysql的相關指令

<code>[root@centfils ~]</code><code># vim /etc/profile.d/mysqld</code>

<code>export</code> <code>PATH=</code><code>/usr/local/mysql/bin</code><code>:$PATH</code>

<code>[root@centfils ~]</code><code># . /etc/profile.d/mysqld</code>

三、編譯安裝php-5.6.26

<code>[root@centfils ~]</code><code># yum -y install libxml2</code>

<code>[root@centfils ~]</code><code># yum -y install libxml2-devel</code>

<code>[root@centfils ~]</code><code># yum -y install libmcrypt</code>

<code>[root@centfils ~]</code><code># yum -y install libmcrypt-devel</code>

<code>[root@centfils ~]</code><code># yum -y groupinstall "Desktop Platform Development" </code>

<code>[root@centfils ~]</code><code># yum -y install bzip2-devel libmcrypt-devel</code>

2、編譯安裝php-5.6.26

<code>[root@centfils ~]</code><code># tar xf php-5.6.26.tar.bz2</code>

<code>[root@centfils ~]</code><code># cd php-5.6.26</code>

<code># ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd</code>

<code>說明:</code>

<code>1、這裡為了支援apache的worker或event這兩個MPM,編譯時使用了--</code><code>enable</code><code>-maintainer-zts選項。</code>

<code>2、如果使用PHP5.3以上版本,為了連結MySQL資料庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL用戶端庫綁定形成依賴),但從PHP 5.4開始它就是預設設定了。</code>

<code># ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd</code>

<code># make</code>

<code># make test</code>

<code># make intall</code>

為php提供配置檔案:

<code>[root@centfils php-5.6.26]</code><code># cp php.ini-production /etc/php.ini</code>

3、 編輯apache配置檔案httpd.conf,以apache支援php

<code>[root@centfils php-5.6.26]</code><code># vim /etc/httpd24/httpd.conf</code>

<code> </code><code>1、添加如下二行</code>

<code>   </code><code>AddType application</code><code>/x-httpd-php</code>  <code>.php</code>

<code>   </code><code>AddType application</code><code>/x-httpd-php-source</code>  <code>.phps</code>

<code> </code><code>2、定位至DirectoryIndex index.html </code>

<code>   </code><code>修改為:</code>

<code>    </code><code>DirectoryIndex  index.php  index.html</code>

而後重新啟動httpd,或讓其重新載入配置檔案即可測試php是否已經可以正常使用

測試

<code>測試頁面index.php示例如下:</code>

<code>&lt;?php</code>

<code>      </code><code>$link = mysql_connect(</code><code>'127.0.0.1'</code><code>,</code><code>'root'</code><code>,</code><code>''</code><code>);</code>

<code>      </code><code>if</code> <code>($link)</code>

<code>        </code><code>echo</code> <code>"OK"</code><code>;</code>

<code>      </code><code>else</code>

<code>        </code><code>echo</code> <code>"Not OK"</code><code>;</code>

<code>      </code><code>phpinfo();</code>

<code>?&gt;</code>

四、安裝xcache,為php加速:

1、安裝

<code># tar xf xcache-3.0.3.tar.gz</code>

<code># cd xcache-3.0.3</code>

<code># /usr/local/php/bin/phpize</code>

<code># ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config</code>

<code># make &amp;&amp; make install</code>

<code>安裝結束時,會出現類似如下行:</code>

<code>Installing shared extensions:     </code><code>/usr/local/php/lib/php/extensions/no-debug-zts-20100525/</code>

2、編輯php.ini,整合php和xcache:

首先将xcache提供的樣例配置導入php.ini

<code># mkdir /etc/php.d</code>

<code># cp xcache.ini /etc/php.d</code>

<code>說明:xcache.ini檔案在xcache的源碼目錄中。</code>

<code>接下來編輯</code><code>/etc/php</code><code>.d</code><code>/xcache</code><code>.ini,找到zend_extension開頭的行,修改為如下行:</code>

<code>zend_extension = </code><code>/usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache</code><code>.so</code>

<code>注意:如果php.ini檔案中有多條zend_extension指令行,要確定此新增的行排在第一位。</code>

五、啟用伺服器狀态

mod_status子產品可以讓管理者檢視伺服器的執行狀态,它通過一個HTML頁面展示了目前伺服器的統計資料。這些資料通常包括但不限于:

(1) 處于工作狀态的worker程序數;

(2) 空閑狀态的worker程序數;

(3) 每個worker的狀态,包括此worker已經響應的請求數,及由此worker發送的内容的位元組數;

(4) 目前伺服器總共發送的位元組數;

(5) 伺服器自上次啟動或重新開機以來至目前的時長;

(6) 平均每秒鐘響應的請求數、平均每秒鐘發送的位元組數、平均每個請求所請求内容的位元組數;

啟用狀态頁面的方法很簡單,隻需要在主配置檔案中添加如下内容即可:

&lt;Location /server-status&gt;

    SetHandler server-status

    Require all granted

&lt;/Location&gt;

需要提醒的是,這裡的狀态資訊不應該被所有人随意通路,是以,應該限制僅允許某些特定位址的用戶端檢視。比如使用Require ip 172.16.0.0/16來限制僅允許指定網段的主機檢視此頁面。

本文轉自 元嬰期 51CTO部落格,原文連結:http://blog.51cto.com/jiayimeng/1858857