CentOS LAMP環境搭建(基於fastcgi)

所需安裝包:httpd-2.4.10.tar.gz、mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz、php-5.4.34.tar.gz


1、安裝httpd

# yum install pcre-devel


# tar xf apr-1.5.1.tar.gz

# ./configure --prefix=/usr/local/apr

# make

# make install


# tar xf apr-util-1.5.4.tar.gz

# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

# make

# make install


# tar xf httpd-2.4.10.tar.gz

# cd httpd-2.4.10

# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-rewrite --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

# make

# make install


2、修改httpd配置文件

# vim /etc/httpd/httpd.conf

pidfile "/var/run/httpd.pid"

ServerName 192.168.101.168


3、添加服務啓動腳本

# vim /etc/init.d/httpd

————————————

#!/bin/bash

#

# httpd        Startup script for the Apache HTTP Server

#

# chkconfig: - 85 15

# description: The Apache HTTP Server is an efficient and extensible  \

#       server implementing the current HTTP standards.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd/httpd.pid

#

### BEGIN INIT INFO

# Provides: httpd

# Required-Start: $local_fs $remote_fs $network $named

# Required-Stop: $local_fs $remote_fs $network

# Should-Start: distcache

# Short-Description: start and stop Apache HTTP Server

# Description: The Apache HTTP Server is an extensible server 

#  implementing the current HTTP standards.

### END INIT INFO


# 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

STOP_TIMEOUT=${STOP_TIMEOUT-10}


# The semantics of these two functions differ from the way apachectl does

# things -- attempting to start while running is a failure, and shutdown

# when not running is also a failure.  So we just do it the way init scripts

# are expected to behave here.

start() {

    echo -n $"Starting $prog: "

    LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

    RETVAL=$?

    echo

    [ $RETVAL = 0 ] && touch ${lockfile}

    return $RETVAL

}


# When stopping httpd, a delay (of default 10 second) is required

# before SIGKILLing the httpd parent; this gives enough time for the

# httpd parent to SIGKILL any errant children.

stop() {

    echo -n $"Stopping $prog: "

    killproc -p ${pidfile} -d ${STOP_TIMEOUT} $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=6

        echo $"not reloading due to configuration syntax error"

        failure $"not reloading $httpd due to configuration syntax error"

    else

        # Force LSB behaviour from killproc

        LSB=1 killproc -p ${pidfile} $httpd -HUP

        RETVAL=$?

        if [ $RETVAL -eq 7 ]; then

            failure $"httpd shutdown"

        fi

    fi

    echo

}


# See how we were called.

case "$1" in

    start)

        start

        ;;

    stop)

        stop

        ;;

    status)

        status -p ${pidfile} $httpd

        RETVAL=$?

        ;;

    restart)

        stop

        start

        ;;

    condrestart|try-restart)

        if status -p ${pidfile} $httpd >&/dev/null; then

            stop

            start

        fi

        ;;

    force-reload|reload)

        reload

        ;;

    graceful|help|configtest|fullstatus)

        $apachectl $@

        RETVAL=$?

        ;;

    *)

        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"

        RETVAL=2

esac


exit $RETVAL

————————————

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

# chkconfig --add httpd

# chkconfig --level 35 httpd on


4、修改httpd PATH變量

# vim /etc/profile.d/httpd.sh

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

# source /etc/profile.d/httpd.sh


5、安裝mysql,這裏使用官方的通用二進制包

# tar xf mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz -C /usr/local

# cd /usr/local/

# ln -sv mysql-5.6.21-linux-glibc2.5-x86_64 mysql


6、創建mysql用戶和組

# groupadd -r -g 306 mysql

# useradd -g 306 -r -u 306 mysql


7、初始化mysql

# cd /usr/local/mysql

# chown -R mysql:mysql /usr/local/mysql/*

# mkdir -p /data/mysql

# chown -R mysql:mysql /data/mysql 

# chmod -R 750 /data/mysql

# scripts/mysql_install_db --user=mysql --datadir=/data/mysql/

# chown -R root /usr/local/mysql/*


8、創建mysql服務啓動腳本

# cp support-files/mysql.server /etc/init.d/mysqld

# chkconfig --add mysqld


9、修改mysql配置,啓動mysql

# mv /etc/my.cnf /etc/my.cnf.bak

# cp support-files/my-default.cnf /etc/my.cnf

# vim /etc/my.cnf

# 在[mysqld]段添加以下兩行

datadir = /data/mysql

thread_concurrency = 4

[client]

socket = /tmp/mysql.sock

# service mysqld start


10、修改MySQL PATH環境變量

# vim /etc/profile.d/mysql.sh

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

# source /etc/profile.d/mysql.sh


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

# vim /etc/man.config

MANPATH /usr/local/mysql/man


12、輸出mysql的庫文件至系統庫查找路徑

# vim /etc/ld.so.conf.d/mysql.conf

# /usr/local/mysql/lib

# ldconfig


13、鏈接mysql的頭文件至系統頭文件路徑/usr/include

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


14、安裝PHP

安裝所需依賴包:

    mhash-0.9.9.9-3.el6.x86_64.rpm

    mhash-devel-0.9.9.9-3.el6.x86_64.rpm

    libmcrypt-2.5.8-9.3.x86_64.rpm

    libmcrypt-devel-2.5.8-9.3.x86_64.rpm

# rpm -ivh  mhash-0.9.9.9-3.el6.x86_64.rpm mhash-devel-0.9.9.9-3.el6.x86_64.rpm libmcrypt-2.5.8-9.3.x86_64.rpm libmcrypt-devel-2.5.8-9.3.x86_64.rpm


# tar xf php-5.4.34.tar.gz

# cd php-5.4.34

# ./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

# make

# make test

# make install


注意:

1. 如果使用PHP5.3以上版本,爲了鏈接MySQL數據庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了;

2. mysqlnd從php 5.3開始可用,可以在編譯時綁定它(而不用和具體的MySQL客戶端庫綁定形成依賴,例如MySQL爲遠程服務器),但從PHP 5.4開始它就是默認設置了。

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


15、創建php配置文件

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


16、添加服務啓動腳本

# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

# chmod +x /etc/init.d/php-fpm

# chkconfig --add php-fpm


17、配置php-fpm並啓動php-fpm

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

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

pm.max_children = 100                            # 最大子進程

pm.start_servers = 5                                    # 初始化啓動進程數

pm.min_spare_servers = 5                        # 最小空閒進程

pm.max_spare_servers = 10                        # 最大空閒進程

# service php-fpm start

# netstat -tunlp                                            # 監聽9000端口


18、修改httpd支持php,啓用httpd相關模塊,重啓httpd

# vim /etc/httpd/httpd.conf

DirectoryIndex index.php index.html

AddType application/x-httpd-php .php

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

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

# httpd -t;service httpd restart


19、httpd虛擬主機配置

# vim /etc/httpd/httpd.conf

#DocumentRoot "/usr/local/apache/htdocs"    ;; 註釋此行

Include /etc/httpd/extra/httpd-vhosts.conf

# vim /etc/httpd/extra/httpd-vhosts.conf

————————————

<VirtualHost *:80>

    ServerName www.test1.com

    DocumentRoot "/data/www/test1.com"

    ProxyRequests off

    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/www/test1.com/$1

    <Directory "/data/www/test1.com">

        Options none

        AllowOverride none

        Require all granted

    </Directory>

    ErrorLog "/var/log/httpd/test1-error_log"

    CustomLog "/var/log/httpd/test1-access_log" combined

</VirtualHost>

————————————

# mkdir -p /data/www/test1.com

# mkdir -p /var/log/httpd

# httpd -t;service httpd restart


20、創建測試頁面index.html、index.php

# echo "<h1>test1.com</h1>" > /data/www/test1.com/index.html

# echo "

<?php

  $conn=mysql_connect('localhost','','');

  if ($conn)

    echo "Success...";

  else

    echo "Failure...";

phpinfo();

?>" > /data/www/test1.com/index.php

# 瀏覽器訪問:http://www.test1.com、http://www.test1.com/index.html


21、安裝php的xcache加速器

# tar xf xcache-3.2.0.tar.gz 

# cd xcache-3.2.0

# /usr/local/php/bin/phpize

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

# make

# make test

# make install

## /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/ ##


22、整合php和xcache,重啓php-fpm

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

# cp -r htdocs /data/www/test1.com/xcache

# vim /etc/php.d/xcache.ini

extension = xcache.so

xcache.count = 2

xcache.mmap_path = "/tmp/xcache"

xcache.coredump_directory = "/tmp/phpcore/"

## 其它參數按需修改 ##

# vim /etc/php.ini

date.timezone = Asia/Shanghai

# service php-fpm restart


# 瀏覽器訪問:http://www.test1.com    ;;搜索xcache

# 瀏覽器訪問:http://www.test1.com/xcache


23、啓用status查看服務器狀態

# mod_status模塊可以讓管理員查看服務器的執行狀態,它通過一個HTML頁面展示了當前服務器的統計數據,這些數據通常包括但不限於:

1. 處於工作狀態的worker進程數

2. 空閒狀態的worker進程數

3. 每個worker的狀態,包括此worker已經響應的請求數,及由此worker發送的內容的字節數

4. 當前服務器總共發送的字節數

5. 服務器自上次啓動或重啓以來至當前的時長

6. 平均每秒鐘響應的請求數、平均每秒鐘發送的字節數、平均每個請求所請求內容的字節數


# 啓用狀態頁面,只需要在主配置文件中添加如下內容即可:

<Location /server-status>

SetHandler server-status

Require all granted

</Location>

# 需要注意的是,這裏的狀態信息不應該被所有人隨意訪問,因此,應限制僅允許某些特定地址的客戶端查看。比如使用Require ip 192.168.101.0/24來限制僅允許指定網段的主機查看此頁面。


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