CentOS6.5安裝配置LAMP


本文主要介紹了LAMP的安裝。

Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一組常用來搭建動態網站或者服務器的開源軟件,本身都是各自獨立的程序,但是因爲常被放在一起使用,擁有了越來越高的兼容度,共同組成了一個強大的Web應用程序平臺


本文所用環境和安裝包爲CentOS6.5+httpd 2.4.6+mysql-5.5.33+php-5.4.19+xcache-3.0.3。


一、編譯安裝apache


1、解決依賴關係


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

(1) 編譯安裝apr

# tar xf apr-1.4.6.tar.bz2
# cd apr-1.4.6
# ./configure --prefix=/usr/local/apr
# make && make install

(2) 編譯安裝apr-util

# tar xf apr-util-1.5.2.tar.bz2
# cd apr-util-1.5.2
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install

(3) httpd-2.4.6編譯過程也要依賴於pcre-devel軟件包,需要事先安裝。此軟件包系統光盤自帶,因此,找到並安裝即可。

參考命令:

#yum install -y pcre-devel


2、編譯安裝httpd-2.4.6

首先下載httpd-2.4.6到本地

# tar xf httpd-2.4.6.tar.bz2
# cd httpd-2.4.6
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --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 --with-mpm=event
# make && make install


3、修改httpd的主配置文件,設置其Pid文件的路徑


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

PidFile  "/var/run/httpd.pid"


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

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#        HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

stop() {
  echo -n $"Stopping $prog: "
  killproc -p ${pidfile} -d 10 $httpd
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  status)
        status -p ${pidfile} $httpd
  RETVAL=$?
  ;;
  restart)
  stop
  start
  ;;
  condrestart)
  if [ -f ${pidfile} ] ; then
    stop
    start
  fi
  ;;
  reload)
        reload
  ;;
  graceful|help|configtest|fullstatus)
  $apachectl $@
  RETVAL=$?
  ;;
  *)
  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
  exit 1
esac

exit $RETVAL

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

# chmod +x /etc/rc.d/init.d/httpd

加入服務列表:

# chkconfig --add httpd

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

#service httpd start

打開瀏覽器訪問ip地址即可看到:

wKiom1PGglPQ1gMLAAAbzGWGKpQ577.jpg


二、安裝mysql-5.5.33


1、準備數據存放的文件系統


新建一個邏輯卷,並將其掛載至特定目錄即可。這裏不再給出過程。

這裏假設其邏輯卷的掛載目錄爲/mydata,而後需要創建/mydata/data目錄做爲mysql數據的存放目錄。


2、新建用戶以安全方式運行進程:

# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data


3、安裝並初始化mysql-5.5.33


首先下載平臺對應的mysql版本至本地,這裏是64位平臺,因此,選擇的爲mysql-5.5.33-linux2.6-x86_64.tar.gz。

# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-5.5.33-linux2.6-x86_64  mysql
# cd mysql 
# chown -R mysql:mysql  .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root  .


4、爲mysql提供主配置文件:

# cd /usr/local/mysql
# cp support-files/my-large.cnf  /etc/my.cnf


並修改此文件中thread_concurrency的值爲你的CPU個數乘以2,比如這裏使用如下行:

thread_concurrency = 4

另外還需要添加如下行指定mysql數據文件的存放位置:

datadir = /mydata/data


5、爲mysql提供sysv服務腳本:

# cd /usr/local/mysql
# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld

添加至服務列表:

# chkconfig --add mysqld
# chkconfig mysqld on

6.在/etc/profile.d下創建mysql.sh,並修改內容:

export PATH=/usr/local/mysql/bin:$PATH

wKiom1PGlb2Spv3nAAGxBSysU6g386.jpg


爲了使用mysql的安裝符合系統使用規範,並將其開發組件導出給系統使用,這裏還需要進行如下步驟:

7、輸出mysql的man手冊至man命令的查找路徑:

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

 MANPATH /usr/local/mysql/man

8、輸出mysql的頭文件至系統頭文件路徑/usr/include:

這可以通過簡單的創建鏈接實現:

# ln -sv /usr/local/mysql/include  /usr/include/mysql

9、輸出mysql的庫文件給系統庫查找路徑:

# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

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

# ldconfig


三、編譯安裝php-5.4.19

1、解決依賴關係:

# yum install libxml2
# yum install libxml2-devel -y


2、編譯安裝php-5.4.19

首先下載源碼包至本地目錄。

# tar xf php-5.4.19.tar.bz2
# cd php-5.4.19
# ./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


說明:

1、這裏爲了支持apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。

2、如果使用PHP5.3以上版本,爲了鏈接MySQL數據庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL客戶端庫綁定形成依賴),但從PHP 5.4開始它就是默認設置了。命令爲:

# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd



接着開始安裝:

# make
# make test
# make intall

爲php提供配置文件:

# cp php.ini-production /etc/php.ini


3、 編輯apache配置文件httpd.conf,以apache支持php

# vim /etc/httpd/httpd.conf

 1、添加如下二行

   AddType application/x-httpd-php  .php

   AddType application/x-httpd-php-source  .phps

 2、定位至DirectoryIndex index.html 

   修改爲:

    DirectoryIndex  index.php  index.html

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


4.測試,在/usr/local/apache/htdocs下創建index.php文件,內容如下:

<?php
    phpinfo();
 ?>

從瀏覽器打開測試頁如下:

wKioL1PHWb2xdvuJAAIiMC2v6ww139.jpg


四、安裝xcache,爲php加速:


1、安裝

# tar xf xcache-3.0.3.tar.gz
# cd xcache-3.0.3
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install

安裝結束時,會出現類似如下行:

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


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


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

# mkdir /etc/php.d
# cp xcache.ini /etc/php.d

說明:xcache.ini文件在xcache的源碼目錄中。


接下來編輯/etc/php.d/xcache.ini,找到extension開頭的行,修改爲如下行:

extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so


3.測試,此時刷新之前的測試頁,可以在其中找到xcache選項

wKiom1PHWTWQlwssAAHP0-jY9Ac329.jpg



附:配置fpm方式的php:

1、編譯安裝php-5.4.19

# tar xf php-5.4.19.tar.bz2
# cd php-5.4.19
# ./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 --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2


爲php提供配置文件:

# cp php.ini-production /etc/php.ini


2、配置php-fpm

 

爲php-fpm提供Sysv init腳本,並將其添加至服務列表:

# cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on

爲php-fpm提供配置文件:

# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

編輯php-fpm的配置文件:

# vim /usr/local/php/etc/php-fpm.conf

配置fpm的相關選項爲你所需要的值,並啓用pid文件(如下最後一行):

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 2

pm.max_spare_servers = 8

pid = /usr/local/php/var/run/php-fpm.pid 


接下來就可以啓動php-fpm了:

# service php-fpm start


可以使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啓動成功了):

# ps aux | grep php-fpm

效果如下:

wKioL1PH2pvi-d25AAGgs4cbApU319.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章