---
title: "linux环境安装"
author: "pavel"
date: "2011-01-07T18:24:19Z"
tags: ["Apache", "Cache", "Linux", "MySQL", "PHP"]
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2011-01-07T18:37:15Z"
id: 865501
url: "https://pavel.iteye.com/blog/865501"
---

**.先找到 apache php mysql   源码包下载的URL地址**  
[http://www.apache.org](http://www.apache.org/)  
[http://www.php.net](http://www.php.net/)  
[http://www.mysql.com](http://www.mysql.com/)

**.安装apache**  
**1)解压源码包**  
   # tar -zxf httpd-2.2.9.tar.gz  
   # cd httpd-2.2.9

**2)修改worker.c (**worker是2.0 中全新的支持多线程和多进程混合模型的MPM**)**  
    
   # vi server/mpm/worker/worker.c

   找到下面几行，并改成如下的数值，其目的是在源码中修改apache可支持的最大线程数和最大客户端数目。

   # define DEFAULT\_SERVER\_LIMIT 256  
   # define MAX\_SERVER\_LIMIT 40000  
   # define DEFAULT\_THREAD\_LIMIT 256  
   # define MAX\_THREAD\_LIMIT 40000

PS: 如果不用--with-mpm显式指定某种MPM的话，那么prefork就是Unix平台上的缺省MPM.它所采用的方式是预派生子进程的方式，这事实上也就是Apache1.3中所采用的模式。它本身并没有使用到线程，2.0使用它是为了与1.3保持兼容性，另一方面，prefork用单独的子进程来处理不同的请求，进程之间是彼此独立的，这也使得它成为最稳定的MPM.对比prefork，worker是2.0 中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理请求，所以，可以处理相对海量的请求，而系统资源的开销小于基于进程的服务器。但是，它也使用了多进程，每个进程又生成多个线程，以获得基于进程的服务器的稳定性。笔者认为这种MPM的工作方式将是Apache 2.0以后的发展趋势。

**3) 编译和安装 apr**  
   # cd srclib/apr  
   # ./configure --prefix=/usr/local/apr  
   # make  
   # make install

**4) 编译和安装 apr-util**  
   # cd srclib/apr-util  
   # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  
   # make  
   # make install

**5) 更改2Glogs**

   # export CFLAGS="-O2 -D\_FILE\_OFFSET\_BITS=64 -D\_LARGEFILE\_SOURCE -D\_LARGEFILE64\_SOURCE"

**6)配置apache安装信息**  
   # CHOST="i686-pc-linux-gnu" \\  
   # CFLAGS="-march=pentium4 -O3 -pipe -fomit-frame-pointer" \\  
   # CXXFLAGS="-march=pentium4 -O3 -pipe -fomit-frame-pointer"  
       #./configure \\ (配置源代码树)  
       --prefix=/usr/local/apache2 \\ (体系无关文件的顶级安装目录PREFIX ，也就Apache的安装目录)  
       --enable-mods-shared=all \\ (编译所有so文件，去掉不经常用的模块，方便日后升级)  
       --enable-so \\ (Apache以动态共享对象(DSO)编译,为了能以Apache模块使PHP动态载入)  
       --enable-deflate \\  
       --enable-cache \\ (支持缓存)  
       --enable-disk-cache \\ (支持文件缓存)  
       --enable-mem-cache \\ (支持记忆缓存)  
       --enable-file-cache \\ (支持磁盘缓存)  
       --enable-proxy \\  
       --enable-rewrite \\ (支持重写规则)  
       --enable-charset-lite \\  
       --enable-ssl \\ (编译 ssl模块)  
       --enable-suexec --with-suexec-bin=/usr/sbin/suexec  
       --with-mpm=worker \\ (指定mpm方式)  
       --with-apr=/usr/local/apr \\  
       --with-apr-util=/usr/local/apr-util  
       --disable-dav \\  
       --disable-cgid \\ (禁止用一个外部 CGI 守护进程执行CGI脚本)  
       --disable-cgi \\ (禁止编译 CGI 版本的 PHP)

**7)执行make安装**  
   # gmake && make install

**8)优化apache配置选项**  
    **(1) 修改httpd.conf**  
   # vi /usr/local/apache2/conf/httpd.conf  
   **1&gt;** 找到 Listen 80     
        改为 Listen xxx.xxx.xxx.xxx:80 （服务器ip）

       找到 DocumentRoot   "/usr/local/apache2.2/htdocs"         
       &lt;Directory "/usr/local/apache2.2/htdocs"&gt;  
       设置你的 WEB 服务器的根目录 如 DocumentRoot "/home/myweb"  
        
        找到 DirectoryIndex index.html  
        改为 DirectoryIndex index.html index.htm index.php

        找到 AddType application/x-gzip .gz .tgz 在其下添加如下内容**，**让apache支持PHP:  
        AddType application/x-httpd-php .php  
        AddType application/x-httpd-php-source .phps

    **2&gt;** 一般说来，可以不需要的模块包括：  
    #LoadModule env\_module libexec/mod\_env.so  
    #LoadModule negotiation\_module libexec/mod\_negotiation.so  
    #LoadModule status\_module libexec/mod\_status.so  
    #server side include已经过时了  
    #LoadModule includes\_module libexec/mod\_include.so  
    #不需要将没有缺省index文件的目录下所有文件列出  
    #LoadModule autoindex\_module libexec/mod\_autoindex.so  
    #尽量不使用CGI：一直是Apache安全问题最多的地方  
    #LoadModule cgi\_module libexec/mod\_cgi.so  
    #LoadModule asis\_module libexec/mod\_asis.so  
    #LoadModule imap\_module libexec/mod\_imap.so  
    #LoadModule action\_module libexec/mod\_actions.so  
    #不使用安全认证可以大大提高访问速度  
    #LoadModule access\_module libexec/mod\_access.so  
    #LoadModule auth\_module libexec/mod\_auth.so  
    #LoadModule setenvif\_module libexec/mod\_setenvif.so

    可用可不用的有：  
    #比如：需要在~/username/下调试php     
    LoadModule userdir\_module libexec/mod\_userdir.so  
    #比如：需要将以前的URL进行转向或者需要使用CGI scrīpt-alias  
    LoadModule alias\_module libexec/mod\_alias.so

    最好保留的有：  
    #用于定制log格式  
    LoadModule config\_log\_module libexec/mod\_log\_config.so  
    #用于增加文件应用的关联  
    LoadModule mime\_module libexec/mod\_mime.so  
    #用于缺省index文件：index.php等  
    LoadModule dir\_module libexec/mod\_dir.so

    必须保留的有：  
    LoadModule authz\_host\_module modules/mod\_authz\_host.so

    **3&gt;** 找到：  
        #Include conf/extra/httpd-mpm.conf  
        #Include conf/extra/httpd-info.conf  
        #Include conf/extra/httpd-vhosts.conf   (虚拟主机配置文件存放目录.)      
        #Include conf/extra/httpd-default.conf      
        去掉前面的“#”号，取消注释。  
        注意：以上 4 个扩展配置文件中的设置请按照相关原则进行合理配置！

        再加上 Include conf/extra/mod\_cache.conf (配置mod\_cache模块)

   **(2)修改MPM（多道处理器）参数**  
    # vi conf/extra/httpd-mpm.conf  
    **1&gt;**找到如下选项，并改成对应的数值

> &lt;IfModule mpm\_prefork\_module&gt;  
>     ServerLimit        2000     
>     StartServers         10  
>     MinSpareServers      10  
>     MaxSpareServers      15  
>     MaxClients         2000  
>     MaxRequestsPerChild   10000  
> &lt;/IfModule&gt;
> 
> &lt;IfModule mpm\_worker\_module&gt;  
>       ServerLimit           64  
>       ThreadLimit          128  
>       StartServers           8  
>       MaxClients          8192  
>       MinSpareThreads       64  
>       MaxSpareThreads       256  
>       ThreadsPerChild       128  
>       MaxRequestsPerChild    0  
> &lt;/IfModule&gt;

   **2&gt;**修改apache2.2子进程所有者  
    # vi /usr/local/apache2.2/conf/httpd.conf  
    找到：  
    User daemon  
    Group daemon  
    把daemon改为nobody:  
    User nobody  
    Group nobody

    **(3) #vi conf/extra/httpd-default.conf**  
    找到如下选项，并改成对应的数值:  
    Timeout 15  
    KeepAlive Off  
    MaxKeepAliveRequests 50  
    KeepAliveTimeout 5  
    UseCanonicalName Off  
    AccessFileName .htaccess  
    ServerTokens Prod  
    ServerSignature Off  
    HostnameLookups Off

    **(4)语法检查及建立软链接**

    运行语法检查命令检查语法，出现错误后继续调整。  
    # /usr/local/apache2/bin/apachectl -t  
    没有错误后在 /usr/local/bin/ 目录下建立软链接  
    # cd /usr/local/bin  
    # ln -fs /usr/local/apache2/bin/httpd httpd

**9)启动WEB服务器**  
   #/usr/local/apache2/bin/apachectl start

   查看自己的站点是否正常 [http://www.mysite.com](http://www.mysite.com/) 也可用IP  
   (用# /usr/local/apache2/bin/apachectl stop 停止apache)  
   (用# /usr/local/apache2/bin/apachectl restart 重启apache)

**·配置Apache模块**  
1.配置mod\_cache模块  
    # cd /usr/local/apache2/conf/extra  
    # vi mod\_cache.conf

> &lt;IfModule mod\_cache&gt;  
> CacheForceCompletion 100  
> CacheDefaultExpire 3600  
> CacheMaxExpire 86400  
> CacheLastModifiedFactor 0.1
> 
> &lt;IfModule mod\_disk\_cache&gt;  
> CacheRoot /  
> CacheSize 256  
> CacheEnable disk /  
> CacheDirLevels 5  
> CacheDirLength 3  
> &lt;/IfModule&gt;
> 
> &lt;IfModule mod\_mem\_cache&gt;  
> CacheEnable mem /  
> MCacheSize 4096  
> MCacheMaxObjectCount 100  
> MCacheMinObjectSize 1  
> MCacheMaxObjectSize 2048  
> &lt;/IfModule&gt;  
> &lt;/IfModule&gt;

 

> 参数的解释：  
> mod\_cache：  
> CacheEnable: 启动 mod\_cache，其后接两个参数。第一个参数指定快取的种类，应设为 mem (记忆体快取) 或 disk (磁碟快取) 之其一；第二个参数指定使用快取的 URI 路径，如果对整个网站 (或虚拟主机) 进行快取，简单指定为根目录(/) 即可。  
> CacheForceCompletion: 这个值指定当 HTTP request 被取消时，内容的产生动作要完成的百分比；预设是 60(%)。  
> CacheDefaultExpire: 指定快取的预设过期秒数；预设值是一小时 (3600)。  
> CacheMaxExpire: 指定快取最大的过期秒数；预设值是一天 (86400)。  
> CacheLastModifiedFactor: 用来从回应里 Last Modified 资讯算出 expire date。  
> 计算方式是：  
> expire period (过期时距) = 最后更新后至今的时间间距 * CacheLastModifiedFactor  
> 而  
> expire date = 目前时间 + expire period  
> 不过无论如何，过期时间不能超过 CacheMaxExpire 的设定值。
> 
> 现在。如果用squid对该服务器进行反向代理的话，就可以通过cache 实现web的加速了

**.安装MYSQL**  
**1)Install**  
   # tar -zxf mysql-standard-5.0.15-linux-i686.tar.gz  
   # cp -r mysql-standard-5.0.15-linux-i686 /usr/local/mysql

**2)修改配置**  
   # vi /usr/local/mysql/support-files/my-medium.cnf 在后面加上  
   max\_connections = 1000  
   log-slow-queries  
   long\_query\_time = 5  
   (注:  
       max\_connections   为允许的最大连接数  
       log-slow-queries 打开低速查询日志  
       long\_query\_time 低速查询的秒数（运行一句sql达到此[时间](http://www.baidu.com/s?tn=cnscn_pg&ct=&lm=&z=&rn=&word=%CA%B1%BC%E4&_si=%CB%D1%CB%F7)记录在日志里）  
   )

   **然后COPY 它为 /etc/my.cnf 文件**  
   #cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf

**3)添加mysql用户及用户组**  
   # groupadd mysql  
   # useradd -g mysql mysql

**4)修改mysql**[**目录**](http://www.baidu.com/s?tn=cnscn_pg&ct=&lm=&z=&rn=&word=%C4%BF%C2%BC&_si=%CB%D1%CB%F7)[**权限**](http://www.baidu.com/s?tn=cnscn_pg&ct=&lm=&z=&rn=&word=%C8%A8%CF%DE&_si=%CB%D1%CB%F7)  
   # chown -R root /usr/local/mysql  
   # chgrp -R mysql /usr/local/mysql  
   # chown -R mysql /usr/local/mysql/data

**5)生成mysql系统数据库**  
   # /usr/local/mysql/scripts/mysql\_install\_db --user=mysql&

**6)启动mysql服务**  
   # /usr/local/mysql/bin/mysqld\_safe --user=mysql&  
   如出现 Starting mysqld daemon with databases from /usr/local/mysql/data   代表正常启动mysql服务了, 按Ctrl + C 跳出

重启mysql: /etc/rc.d/init.d/mysqld restart

**7)修改 mysql 的 root 密码**  
   # /usr/local/mysql/bin/mysqladmin -u root -p password 123456  
   回车出现 Enter password: 最开始密码默认为空继续回车即可 123456 即为你的新密码

**.安装PHP**  
**1)安装GD库(让PHP支持GIF,PNG,JPEG)**  
   首先下载 jpeg6,libpng,freetype 并安装[模块](http://www.baidu.com/s?tn=cnscn_pg&ct=&lm=&z=&rn=&word=%C4%A3%BF%E9&_si=%CB%D1%CB%F7)  
   wget [http://www.ijg.org/files/jpegsrc.v6b.tar.gz](http://www.ijg.org/files/jpegsrc.v6b.tar.gz)  
   wget [http://nchc.dl.sourceforge.net/sourc...g-1.2.8.tar.gz](http://nchc.dl.sourceforge.net/sourc...g-1.2.8.tar.gz)  
   wget [http://keihanna.dl.sourceforge.net/s...-2.1.10.tar.gz](http://keihanna.dl.sourceforge.net/s...-2.1.10.tar.gz)  
   wget [http://www.boutell.com/gd/http/gd-2.0.33.tar.gz](http://www.boutell.com/gd/http/gd-2.0.33.tar.gz)

     gd-2.0.33.tar.gz   http://www.boutell.com/gd/

     jpegsrc.v6b.tar.gz http://www.ijg.org/

     libpng-1.2.7.tar.tar  [http://www.libpng.org/pub/png/libpng.html](http://www.libpng.org/pub/png/libpng.html)

     zlib-1.2.2.tar.gz   http://www.zlib.net/

     freetype-2.1.9.tar.gz  [http://www.freetype.org/](http://www.freetype.org/)

**1.装zlib**

   # tar -zxvf zlib-1.2.2.tar.gz  
   # cd zlib-1.2.2  
   # ./configure  
   # make  
   # make install

**2.安装 jpeg6**  
   建立目录  
   # mkdir /usr/local/jpeg6  
   # mkdir /usr/local/jpeg6/bin  
   # mkdir /usr/local/jpeg6/lib  
   # mkdir /usr/local/jpeg6/include  
   # mkdir /usr/local/jpeg6/man  
   # mkdir /usr/local/jpeg6/man/man1  
   # cd /usr/local/src  
   # tar -zxf jpegsrc.v6b.tar.gz  
   # cd jpeg-6b  
   # ./configure --prefix=/usr/local/jpeg6/ --enable-shared --enable-static  
   # make  
   # make install

   **3.安装libpng**  
   # cd /usr/local/src  
   # tar -zxf libpng-1.2.8.tar.gz  
   # cd libpng-1.2.8  
   # cp scripts/makefile.std makefile  
   # make  
   # make install

   4**.安装 freetype**  
   # cd /usr/local/src  
   # tar -zxf freetype-2.1.10.tar.gz  
   # cd freetype-2.1.10  
   # ./configure --prefix=/usr/local/freetype  
   # make  
   # make install

 

5**.安装 autoconf**

   # cd /usr/local/src  
   # tar -zxf autoconf  
   # cd autoconf  
   # ./configure    # make  
   # make install

   6**.安装最新的GD库**  
   # cd /usr/local/src  
   # tar -zxf gd-2.0.33.tar.gz  
   # cd gd-2.0.33  
   # ./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg6/ --with-png --with-zlib --with-freetype=/usr/local/freetype/  
   # make  
   # make install

**6.安装最新的Curl库**  
   # cd /usr/local/src  
   # wget [http://curl.haxx.se/download/curl-7.15.0.tar.gz](http://curl.haxx.se/download/curl-7.15.0.tar.gz)  
   # tar -zxf curl-7.15.0.tar.gz  
   # ./configure --prefix=/usr/local/curl  
   # make  
   # make install

**2)安装PHP**  
  **1.下载并安装libxml2**  
   # cd /root/soft  
   # wget [http://ftp.gnome.org/pub/GNOME/sourc...-2.6.19.tar.gz](http://ftp.gnome.org/pub/GNOME/sourc...-2.6.19.tar.gz)  
   ([http://ftp.gnome.org/pub/GNOME/sources/](http://ftp.gnome.org/pub/GNOME/sources/))  
   # tar -zxf libxml2-2.6.19.tar.gz  
   # cd libxml2-2.6.19  
   # ./configure --prefix=/usr/local/libxml2  
   # make; make install

**2.安装 libxslt**  
   # cd /root/soft  
   # wget [http://ftp.gnome.org/pub/GNOME/sourc...-1.1.15.tar.gz](http://ftp.gnome.org/pub/GNOME/sourc...-1.1.15.tar.gz)  
   # tar -zxf libxslt-1.1.15.tar.gz  
   # cd libxslt-1.1.15  
   # ./configure --prefix=/usr/local/libxslt --with-libxml-prefix=/usr/local/libxml2  
   # make; make install

3**.开始安装PHP5**  
   # tar -zxf php-5.2.5.tar.gz \\  
   # cd php-5.2.5 \\  
   # ./configure --prefix=/usr/local/php5 \\  
   # --with-apxs2=/usr/local/apache2/bin/apxs \\  
   # --with-mysql=/usr/local/mysql \\  
   # --with-curl=/usr/local/curl \\  
   # --with-libxml-dir=/usr/local/libxml2 \\  
   # --with-xsl=/usr/local/libxslt \\  
   # --with-expat-dir=/usr/lib \\ (忽略)  
   # --with-iconv \\ (支持iconv库)  
   # --with-gd=/usr/local/gd2 \\  
   # --with-jpeg-dir=/usr/local/jpeg6 \\  
   # --with-zlib-dir=/usr/lib \\ (指定zlib模块位置)  
   # --with-png-dir=/usr/local/lib \\  
   # --with-freetype-dir=/usr/local/freetype \\  
   # --enable-mbstring \\ (支持mbstring模块)  
   # --enable-maintainer-zts \\ (apache mpm以worker方式)  
   # --enable-ftp \\  
   # --enable-soap \\  
   # --enable-xslt \\ (忽略)  
   # --disable-ipv6 \\  
   # --disable-cgi \\  
   # --disable-cli           #禁掉ipv6,禁掉cli模式，提升速度和安全性.  
   # make  
   # make install

   **必选项**  
       --prefix=/usr/local/php5  
       --with-apxs2=/usr/local/apache2.2/bin/apxs  
       --with-mysql=/usr/local/mysql/  
       --with-libxml-dir=/usr/local/libxml2  
    
   **GD库的配置选项**  
       --with-gd=/usr/local/gd2/  
       --with-jpeg-dir=/usr/local/jpeg6/  
       --with-png-dir  
       --with-zlib-dir=/usr/lib  
       --with-freetype-dir=/usr/local/freetype

   **可选项**    
       --with-curl=/usr/local/curl                                           支持CURL库  
       --enable-ftp                                                             打开FTP库函数支持  
       --enable-soap --with-xsl=/usr/local/libxslt --enable-xslt   让PHP支持SOAP

**.配置 httpd.conf 让apache支持PHP**  
   # vi /usr/local/apache/conf/httpd.conf  
       找到 AddType application/x-gzip .gz .tgz 在其下添加如下[内容](http://www.baidu.com/s?tn=cnscn_pg&ct=&lm=&z=&rn=&word=%C4%DA%C8%DD&_si=%CB%D1%CB%F7):  
       AddType application/x-httpd-php .php  
       AddType application/x-httpd-php-source .phps

   重启apache  
     # /usr/local/apache/bin/apachectl restart  
     在你Web[目录](http://www.baidu.com/s?tn=cnscn_pg&ct=&lm=&z=&rn=&word=%C4%BF%C2%BC&_si=%CB%D1%CB%F7)里建一[内容](http://www.baidu.com/s?tn=cnscn_pg&ct=&lm=&z=&rn=&word=%C4%DA%C8%DD&_si=%CB%D1%CB%F7)为   PHP文件, 输入URL地址查看PHP配置是否正确