编译安装LAMP及分离式LAMP平台构建

前言

LAMP网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Apache网站服务器,MySQL数据库,Perl、PHP或者Python编程语言,所有组成产品均是开源软件,是国际上成熟的架构框架,很多流行的商业应用都是采取这个架构,和Java/J2EE架构相比,LAMP具有Web资源丰富、轻量、快速开发等特点,与微软的.NET架构相比,LAMP具有通用、跨平台、高性能、低价格的优势,因此LAMP无论是性能、质量还是价格都是企业搭建网站的首选平台。但由于MySQL作为SUN公司的附属品已被Oracle收购,以后是否还会继续开源,这个鬼才知道,所以CentOS7上开始采用MySQL的分支MariaDB,估计以后在LAMP中MariaDB也会取代MySQL。

工作原理

LAMP工作原理.jpg

编译安装LAMP

编译安装httpd

系统环境:CentOS6.6

作为web服务器:172.16.10.100

所需软件包:apr-1.5.0.tar.bz2、apr-util-1.5.3.tar.bz2、httpd-2.4.9.tar.bz2

注意:httpd2.4需要依赖apr和arp-util 1.4以上版本

解决依赖关系

[root@scholar ~]# yum groupinstall Development tools Server Platform Development -y
[root@scholar ~]# yum -y install pcre-devel

编译安装apr和apr-util

[root@scholar ~]# tar xf apr-1.5.0.tar.bz2 
[root@scholar ~]# cd apr-1.5.0
[root@scholar apr-1.5.0]# ./configure --prefix=/usr/local/apr
[root@scholar apr-1.5.0]# make && make install
[root@scholar apr-1.5.0]# cd ..
[root@scholar ~]# tar xf apr-util-1.5.3.tar.bz2 
[root@scholar ~]# cd apr-util-1.5.3
[root@scholar apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util
 --with-apr=/usr/local/apr/
[root@scholar apr-util-1.5.3]# make && make install

编译安装httpd

[root@scholar apr-util-1.5.3]# cd ..
[root@scholar ~]# tar xf httpd-2.4.9.tar.bz2 
[root@scholar ~]# cd httpd-2.4.9
[root@scholar httpd-2.4.9]# ./configure --prefix=/usr/local/apache 
--sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi 
--enable-rewrite --enable-deflate --with-zlib --with-pcre 
--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ 
--enable-mpms-shared=all --with-mpm=event --enable-modules=most
[root@scholar httpd-2.4.9]# make && make install
编译参数详解:
--prefix:#安装路径
--sysconfdir:#指定配置文件路径
--enable-so:#DSO兼容,DSO=Dynamic Shared Object,动态共享对象,可实现模块动态生效
--enable-ssl:#支持SSL/TLS,可实现https访问 需已安装openssl-devel
--enable-cgi:#支持CGI脚本(默认对非线程的MPM模式开启)
--enable-rewrite:#启用Rewrite功能,URL重写
--enable-deflate:#支持压缩功能
--with-zlib:#使用指定的zlib库,不指定路径会自动寻找
--with-pcre:#使用指定的PCRE库,不指定路径会自动寻找 需已安装pcre-devel
--with-apr:#指定apr安装路径
--with-apr-util:#指定apr-util安装路径
--enable-mpms-shared:#支持动态加载的MPM模块,可选参数:all
--with-mpm:#设置默认启用的MPM模式,{prefork|worker|event}
--enable-modules:#支持动态启用的模块,可选参数:all,most,few,reallyall
#编译之前可使用./configure --help查看各项参数

添加环境变量

为了方便命令使用需要添加环境变量

[root@scholar ~]# vim /etc/profile.d/httpd24.sh  #名字自定义
 
export PATH=/usr/local/apache/bin:$PATH
 
[root@scholar ~]# source /etc/profile.d/httpd24.sh  #重读环境变量

导出头文件

[root@scholar ~]# ln -sv /usr/local/apache/include/ /usr/include/httpd24

导出man手册

[root@scholar ~]# vim /etc/man.config 
 
MANPATH /usr/local/apache/man   #添加编译安装的man位置

提供脚本

#!/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
#!/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-/usr/local/apache/logs/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
}
 
  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

加入服务列表,测试脚本启动服务,查看80端口是否被监听

1.jpg

访问测试,查看是否正常工作

2.jpg

OK,没问题,httpd编译安装完成

安装MariaDB

系统环境:CentOS6.6

作为数据库服务器:172.16.10.211

所需软件包:mariadb-5.5.36-linux-x86_64.tar.gz(二进制格式包)

装备数据存放文件系统

为了安全起见,一般数据库数据是被放在其他磁盘上的,这里我们新建一个逻辑卷,并将其挂载至特定目录即可。

[root@MariaDB ~]# fdisk /dev/sdb
n
p
1

+20G
t
8e
w
[root@MariaDB ~]# partx -a /dev/sdb
[root@MariaDB ~]# pvcreate /dev/sdb1
[root@MariaDB ~]# vgcreate myvg /dev/sdb1
[root@MariaDB ~]# lvcreate -L 10G -n mydata myvg
[root@MariaDB ~]# mke2fs -t ext4 -L MYDATA -b 4096 -m 3 /dev/myvg/mydata
[root@MariaDB ~]# mkdir /mydata
[root@MariaDB ~]# vim /etc/fstab
LABEL=MYDATA            /mydata            ext4    defaults,noatime 0 0
[root@MariaDB ~]# mount -a
[root@MariaDB ~]# mkdir /mydata/data

为了安全,新建用户及组,以普通用户运行

3.jpg

解压安装MariaDB,权限修改

4.jpg

提供配置文件,初始化数据库

[root@MariaDB local]# mkdir /etc/mysql
[root@MariaDB local]# cp ./mysql/support-files/my-large.cnf /etc/mysql/my.cnf
[root@MariaDB local]# vim /etc/mysql/my.cnf

thread_concurrency = 4   #运行CPU数量乘2
datadir = /mydata/data   #添加行,指明数据文件存放位置

[root@MariaDB local]# cd mysql/
[root@MariaDB mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data

提供脚本

5.jpg

加入服务列表,启动服务,查看3306端口是否被监听

6.jpg

添加环境变量

[root@MariaDB mysql]# vim /etc/profile.d/mysql.sh

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

[root@MariaDB mysql]# source /etc/profile.d/mysql.sh

导出man手册

[root@MariaDB mysql]# vim /etc/man.config 
 
MANPATH /usr/local/mysql/man

导出头文件

[root@MariaDB ~]# ln -sv /usr/local/mysql/include  /usr/include/mysql

输出库文件给系统库查找路径

[root@MariaDB ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

测试MariaDB

7.jpg

OK,MariaDB安装完成

编译安装php

系统环境:CentOS6.6

作为php服务器:172.16.10.110

所需安装包:php-5.4.26.tar.bz2、xcache-3.1.0.tar.bz2(非必须,加速软件)

解决依赖关系

[root@scholar ~]#  yum groupinstall Development tools Server Platform Development -y
[root@scholar ~]#  yum -y groupinstall Desktop Platform Development
[root@scholar ~]#  yum -y install bzip2-devel libmcrypt-devel

解压编译

[root@scholar ~]# tar xf php-5.4.26.tar.bz2 
[root@scholar ~]# cd php-5.4.26
[root@scholar php-5.4.26]#  ./configure --prefix=/usr/local/php --with-mysql=mysqlnd 
--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 --enable-fpm --with-mcrypt  --with-config-file-path=/etc 
--with-config-file-scan-dir=/etc/php.d --with-bz2 --with-openssl
[root@scholar php-5.4.26]# make && make install

提供配置文件

[root@scholar php-5.4.26]# cp php.ini-production /etc/php.ini

为php-fpm提供脚本

8.jpg

为php-fpm提供配置文件

9.jpg

编辑配置文件

[root@scholar php]# vim etc/php-fpm.conf  #注意文件位置/usr/local/php/etc/

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

listen = 172.16.10.110:9000

pm.max_children = 25  #最大子进程数

pm.start_servers = 5  #开机预启动子进程数

pm.min_spare_servers = 2 #最小空闲子进程数

pm.max_spare_servers = 6 #最大空闲子进程数

启动服务,检查9000端口是否被监听

10.jpg

到此为止,各软件算是编译安装完了,我们通过一个实例让他们工作起来

启动LAMP

案例要求:

DNS服务器:172.16.10.10

web服务器:172.16.10.100(已编译安装httpd-2.4.9)

php服务器:172.16.10.110(已编译安装php-5.4.26)

数据库服务器:172.16.10.211(已安装MariaDB-5.5.36)

要求web服务器提供3个站点:www.scholar.com(静态)、admin.scholar.com(动态,phpMyAdmin)、blog.scholar.com(动态、wordpress),站点文件目录分别为:/web/www、/web/pma、/web/blog,其中admin.scholar.com由于是管理数据库的站点,所以必须基于https进行通信。

DNS服务器配置

修改DNS正反向区域文件

正向解析

11.jpg

反向解析

12.jpg

检查语法,重启服务

13.jpg

web服务器配置

启用相关模块

[root@scholar ~]# vim /etc/httpd24/httpd.conf

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

启用虚拟主机

#DocumentRoot "/usr/local/apache/htdocs"   #关闭中心主机

Include /etc/httpd24/extra/httpd-vhosts.conf #启用虚拟主机

使之支持php

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

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

配置虚拟主机

[root@scholar ~]# vim /etc/httpd24/extra/httpd-vhosts.conf 

<VirtualHost *:80>
    DocumentRoot "/web/www"
    ServerName www.scholar.com
<Directory "/web/www">
        Options none
        AllowOverride none
        Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/web/pma"
    ServerName admin.scholar.com
    ProxyRequests Off                #关闭正向代理
    ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.10.110:9000/web/pma/$1 #代理至php服务器
<Directory "/web/pma">
        Options none
        AllowOverride none
        Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/web/blog"
    ServerName blog.scholar.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.10.110:9000/web/blog/$1
<Directory "/web/blog">
        Options none
        AllowOverride none
        Require all granted
</Directory>
</VirtualHost>

站点文件准备

web服务器:

14.jpg

[root@scholar php]# mkdir /web/{pma,blog} -pv  #此为php服务器,不要混淆
mkdir: created directory `/web/pma'
mkdir: created directory `/web/blog'
[root@scholar php]# scp 172.16.10.100:/web/pma/* /web/pma/
[root@scholar php]# scp 172.16.10.100:/web/blog/* /web/blog/
[root@scholar php]# cd /web/pma
[root@scholar pma]# cp config.sample.inc.php config.inc.php
[root@scholar pma]# vim config.inc.php 

$cfg['blowfish_secret'] = 'a8b7c6dhfgs';

$cfg['Servers'][$i]['host'] = '172.16.10.211';

[root@scholar pma]# cd ../blog/
[root@scholar blog]# cp wp-config-sample.php wp-config.php 
[root@scholar blog]# vim wp-config.php 

/** WordPress 数据库的名称 */
define('DB_NAME', 'wpdb');

/** MySQL 数据库用户名 */
define('DB_USER', 'wpuser');

/** MySQL 数据库密码 */
define('DB_PASSWORD', 'wppass');

/** MySQL 主机 */
define('DB_HOST', '172.16.10.211');

数据库准备

15.jpg

检查语法,重启服务

16.jpg

测试各站点,访问是否正常

17.jpg

18.jpg

19.jpg

至此,分离式的LAMP就可以统一正常工作了,接下来我们为admin.scholar.com提供https。

生成私钥,提供证书

20.jpg

签署证书,我这里本机就是CA就自签了,CA配置详见博客

21.jpg

启用ssl功能

[root@scholar ssl]# vim /etc/httpd24/httpd.conf 

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so

Include /etc/httpd24/extra/httpd-ssl.conf
[root@scholar ssl]# vim /etc/httpd24/extra/httpd-ssl.conf 

DocumentRoot "/web/pma"    #站点目录
ServerName admin.scholar.com:443    #启用ssl的站点
  ProxyRequests Off    #同样关闭正向向代理            
  ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.10.110:9000/web/pma/$1 #反向代理至php服务器
<Directory "/web/pma">
        Options none
        AllowOverride none
        Require all granted
</Directory>

SSLCertificateFile "/etc/httpd24/ssl/httpd.crt"   #证书位置

SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key" #私钥位置

测试语法,重启服务,查看443端口是否被监听

22.jpg

将CA证书导出,重命名为*.crt格式,安装在受信任的根证书颁发机构中,测试https

23.jpg

成功了,我们登陆数据库看一下

24.jpg

OK,登陆成功,可以管理数据库啦,至此,基于https的加密通信部署完成

xcache加速

最后,我们补充讲解一下xcache,xcache是一个开源的 opcode 缓存器/优化器,它可以提高php的性能,加快页面生成速率,从而降低服务器负载。

为了比较加速前后的效果,在安装xcache之前,我们来对现在的服务器进行压力测试

25.jpg

编译安装xcache

[root@scholar ~]# tar xf xcache-3.1.0.tar.bz2 
[root@scholar ~]# cd xcache-3.1.0
[root@scholar xcache-3.1.0]# /usr/local/php/bin/phpize
[root@scholar xcache-3.1.0]# ./configure --enable-xcache --with-php-config=
/usr/local/php/bin/php-config
[root@scholar xcache-3.1.0]# make && make install

配置xcache

[root@scholar xcache-3.1.0]# mkdir /etc/php.d
[root@scholar xcache-3.1.0]# cp xcache.ini /etc/php.d/
[root@scholar xcache-3.1.0]# vim /etc/php.d/xcache.ini 

[xcache-common]
;; non-Windows example:
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

安装成功,重启服务,再次测试

25.1.jpg

26.jpg

经过xcache加速,响应和传输速度快了好多倍,由此可见,xcache还是很实用的。

The end 

好了,LAMP就先讲解到这里啦,搭建LAMP平台还是挺费时间的,希望本文可以对你有所帮助,部署过程中遇到问题可留言,欲了解更多功能的LAMP,请关注后续文章。以上仅为个人学习整理,如有错漏,大神勿喷~~~

原创文章,作者:书生,如若转载,请注明出处:http://www.178linux.com/3414

(0)
上一篇 2015-04-20 21:33
下一篇 2015-04-20 21:41

相关推荐

  • bash功能特性一 变量

    一、什么是shell     shell是为了使用户能够操作应用程序的接口程序。如经常使用的图形界面及命令行界面都是shell。下面来介绍一下bash这个shell的功能。 二、变量的概念     被命名的内存空间即为变量,Linux中内置了许多变量,我们可以通过修改这些变量的值…

    Linux干货 2015-04-19
  • 马哥教育网络班19期+第7周课程练习

    1.创建一个10G分区,并格式为ext4文件系统:   (1)要求其block大小为2048,预留空间百分比为2,卷标为MYDATA,默认挂载属性包含acl;   (2)挂载至/data/mydata目录,要求挂载时禁止程序自行运行,且不更新文件的访问时间戳;   fdisk /dev/sdb    part…

    Linux干货 2016-06-22
  • N25_第四周博客作业

    N25_第四周博客作业   1、复制/etc/ske1目录为/home/tuser1,要求/home/tuser1及其内部文件的属组合其他用户均没有任何访问权限。     2、编辑/etc/group文件,添加组hadoop.。             &nb…

    Linux干货 2016-12-24
  • 初识linux内核编译

    编译内核前提:        1、准备开发环境。        2、获取目标主机上硬件设备的相关信息。        3、获取目标主机系统功能的相关信息,例如要启用的文件系统。 &…

    Linux干货 2016-09-13
  • linux历史命令history详解

    history命令 history命令用于显示指定数目的指令命令,读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入命令文件。 该命令单独使用时,仅显示历史命令,在命令行中,可以使用符号!执行指定序号的历史命令。例如,要执行第2个历史命令,则输入!2。  128  ls  129  history[…

    Linux干货 2016-09-05
  • Linux基础之文件管理和bash特性

    部分文件管理命令,bash的一些基础特性,部分glob文件匹配示例

    Linux干货 2018-01-16

评论列表(2条)

  • stanley
    stanley 2015-04-20 21:38

    漂亮专业,几乎已经是免检产品了。赞

    • 书生
      书生 2015-04-20 22:39

      @stanley卡章 出厂 :mrgreen: