天天看点

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软件包,需要事先安装。此软件包系统光盘自带,因此,找到并安装即可。

<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