在Amazon EC2主機上部署LAMP環境


LAMP環境:httpd 2.4.6 + mysql-5.6.13 + php-5.4.22

系統環境:Amazon Linux (64-bit) (類似於RHEL6.4_x86_64)


一、編譯安裝httpd-2.4.6

1、安裝httpd所依賴的軟件包

$ sudo yum groupinstall Development Libraries Development tools
$ sudo mkdir /usr/src/lamp

<1> 編譯安裝apr

$ cd /usr/src/lamp
$ sudo wget http://down1.chinaunix.net/distfiles/apr-1.4.6.tar.bz2
$ sudo tar xf apr-1.4.6.tar.bz2
$ cd apr-1.4.6
$ sudo ./configure --prefix=/usr/local/apr
$ sudo make
$ sudo make install

<2> 編譯安裝apr-util

$ cd /usr/src/lamp
$ sudo wget http://apache.spinellicreations.com//apr/apr-util-1.5.2.tar.bz2
$ sudo tar xf apr-util-1.5.2.tar.bz2
$ cd apr-util-1.5.2
$ sudo ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
$ sudo make
$ sudo make install

<3> Yum安裝pcre-devel

$ sudo yum -y install pcre-devel

2、編譯安裝httpd-2.4.6

$ cd /usr/src/lamp
$ sudo wget http://mirror.cc.columbia.edu/pub/software/apache//httpd/httpd-2.4.6.tar.bz2
$ sudo tar xf httpd-2.4.6.tar.bz2
$ cd httpd-2.4.6
$ sudo ./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=all --enable-mpms-shared=all --with-mpm=event
$ sudo make
$ sudo make install

如編譯出現以下錯誤:

checking for OpenSSL version >= 0.9.7... FAILED
configure: WARNING: OpenSSL version is too old

則需要安裝最新的openssl和openssl-devel的rpm包,如默認已安裝最新的openssl仍編譯出錯,查看openssl-devel是否未裝,安裝即解決問題

$ sudo yum install openssl-devel.x86_64

3、編輯httpd的主配置文件,添加Pid文件的路徑

$ sudo vim /etc/httpd/httpd.conf
PidFile  "/var/run/httpd.pid"

4、爲httpd提供sysv服務腳本

$ sudo vim /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

爲此腳本添加執行權限:

$ sudo chmod +x /etc/rc.d/init.d/httpd

添加至服務列表:

$ sudo chkconfig --add httpd

5、修改PATH環境變量

$ sudo vim /etc/profile.d/httpd.sh
export PATH=$PATH:/usr/local/apache/bin

6、啓動httpd服務並測試

$ sudo service httpd start


二、安裝mysql-5.6.13(通用二進制)

1、新建mysql用戶

$ sudo groupadd -r mysql
$ sudo useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
$ sudo chown -R mysql:mysql /mydata/data
$ sudo mkdir /mydata/data -p
$ sudo chown -R mysql:mysql /mydata/data

2、安裝並初始化mysql-5.6.13

$ cd /usr/src/lamp
$ sudo wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz
$ sudo tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local
$ cd /usr/local/
$ sudo ln -sv mysql-5.6.13-linux-glibc2.5-x86_64 mysql
$ cd mysql
$ sudo chown -R mysql:mysql .
$ sudo scripts/mysql_install_db --user=mysql --datadir=/mydata/data
$ sudo chown -R root .

3、爲mysql提供主配置文件並編輯

$ cd /usr/local/mysql
$ sudo cp support-files/my-default.cnf /etc/my.cnf
$ sudo vim /etc/my.cnf
datadir=/mydata/data
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
thread_concurrency = 2
innodb_file_per_table = 1

4、爲mysql提供sysv服務腳本

$ cd /usr/local/mysql
$ sudo cp support-files/mysql.server /etc/rc.d/init.d/mysqld

添加執行權限:

$ sudo chmod +x /etc/rc.d/init.d/mysqld

添加至服務列表:

$ sudo chkconfig --add mysqld
$ sudo chkconfig mysqld on

5、編輯man配置文件,添加mysql的man文件路徑

$ sudo vim /etc/man.config
MANPATH  /usr/local/mysql/man

6、將mysql的頭文件輸出至系統

$ sudo ln -sv /usr/local/mysql/include /usr/include/mysql

7、將mysql的庫文件輸出至系統庫並重新載入

$ sudo echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
$ sudo ldconfig

8、修改PATH環境變量

$ sudo vim /etc/profile.d/mysqld.sh
export PATH=$PATH:/usr/local/mysql/bin

9、啓動mysql服務並測試

$ sudo service mysqld start


三、編譯安裝php-5.4.22

1、安裝PHP所依賴的軟件包

<1> RPM安裝mcrypt

$ cd /usr/src/lamp
$ sudo wget ftp://ftp.muug.mb.ca/mirror/fedora/epel/beta/6/x86_64/libmcrypt-2.5.8-9.el6.x86_64.rpm
$ sudo wget http://www.gymglish.com/opensource/rpms/centos6-rpms/libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
$ sudo rpm -ivh lib*

<2> Yum安裝libxml和bzip2的devel包

$ sudo yum install libxml2-devel.x86_64
$ sudo yum install bzip2-devel.x86_64

2、編譯安裝php-5.4.22

$ cd /usr/src/lamp
$ sudo wget http://au1.php.net/distributions/php-5.4.22.tar.bz2
$ sudo tar xf php-5.4.22.tar.bz2
$ cd php-5.4.22
$ sudo ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --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
$ sudo make
$ sudo make install

爲php提供配置文件:

$ sudo cp php.ini-production /etc/php.ini

3、編輯httpd配置文件,使其支持php及默認主頁

$ sudo vim /etc/httpd/httpd.conf
# 添加以下內容:
AddType application/x-httpd-php  .php
AddType application/x-httpd-php-source  .phps
# 修改以下內容:
DirectoryIndex  index.php  index.html

4、編輯httpd配置文件,新建虛擬主機

$ sudo vim /etc/httpd/httpd.conf
Include /etc/httpd/extra/vhosts.conf
$ sudo vim /etc/httpd/extra/vhosts.conf
<VirtualHost *:80>
  DocumentRoot "/mydata/web"
  ServerName cominggo.com
  ServerAlias www.cominggo.com
  <Directory "/mydata/web">
    Options none
    AllowOverride none
    Require all granted
  </Directory>
</VirtualHost>

5、重啓httpd服務並測試

$ sudo servcie httpd configtest
$ sudo service httpd restart


四、安裝PHP加速器XCache(可選)

1、編譯安裝xcache-3.0.4

$ cd /usr/src/lamp
$ sudo wget http://xcache.lighttpd.net/pub/Releases/3.0.4/xcache-3.0.4.tar.gz
$ sudo tar xf xcache-3.0.4.tar.gz
$ cd xcache-3.0.4
$ /usr/local/php/bin/phpize
$ sudo /usr/local/php/bin/phpize
$ sudo ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
$ sudo make
$ sudo make install
# 安裝結果如下:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

2、編輯xcache配置文件,整合PHP和XCache

$ sudo mkdir /etc/php.d
$ sudo cp xcache.ini /etc/php.d/
$ sudo vim /etc/php.d/xcache.ini
# 修改以下內容:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

注:xcache自3.0版本開始不再支持使用zend_extension加載xcache

3、重啓httpd服務並測試

$ sudo service httpd restart


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