---
title: " Red Hat Linux下MySql+Apache+PHP配置"
author: "jontan"
date: "2014-01-10T13:35:46Z"
tags: []
categories: ["企业架构"]
slug: "red-hat-linux-mysql-apache-php"
draft: false
lastmod: "2014-03-10T10:16:22Z"
id: 2002289
url: "https://summary.iteye.com/blog/2002289"
---

详细介绍：Apache+PHP+MySQL配置攻略

 

所需安装源文件  
apache-2.0.63  
freetype-2.1.10  
gd-2.0.28  
jpeg-7  
libmcrypt-2.5.7  
libpng-1.2.5  
libxml2-2.6.11  
mcrypt-2.6.4  
mhash-0.9.9.9  
mysql-5.0.81  
php-5.3.3  
zlib-1.2.3

1\. 安装mysql  
　　安装步骤： 

```
cd mysql-5.0.81
./configure --prefix=/usr/local/mysql --sysconfdir=/etc --localstatedir=/var/lib/mysql/
make
make install  
```

   
　　根据机子的性能，编译过程要花费一定的时间，编译完成后系统默认把mysql安装在/usr/local下。  
　　安装完成后，先把mysql的进程启起来   
　　cd /usr/local/bin   
　　./mysql\_install\_db(安装mysql的基本数据库)   
　　./safe\_mysqld & (把进程启起来并转入后台)   
　　到这里mysql算安装完成了，试试运行./mysql看看能不能链接数据库，如果正常的话，应该可以看到这样的界面  　　   
Welcome to the MySQL monitor.Commands end with ; or g.   
Your MySQL connection id is 880 to server version: 3.22.40   
Type 'help' for help.   
mysql&gt;   
　　到这恭喜你完成了mysql的安装 

#启动mysql server   
/usr/mysql/safe\_mysqld &  
mysql #运行mysql 客户端,并开放root用户的远程访问权限。以便调试   
use mysql   
update user set host = '%' where user = 'root' and host &lt;&gt; 'localhost';   
flush privileges;   
quit 

2\. 安装apache 

　　在如下页面下载apache的for linux 的源码包   
　　http://www.apache.org/dist/httpd/;   
　　存至/home/tmp目录   
　　命令列表： 

```
./configure --prefix=/usr/local/apache \
--enable-dav  \
--enable-module=rewrite \
--enable-so  \
--enable-maintainer-mode  \
--enable-mods-shared=all  \
--enable-ssl=shared  \
--with-ssl ;

make 
make install   
```

   
安装apache至/usr/local/apache 并配置apache支持dso方式   
 /usr/local/apache/bin/apachectl -M  -t -D DUMP\_MODULES 安装了哪些模块  
/usr/local/apache/bin/apachectl -t    测试安装是否成功  
/usr/local/apache/bin/apachectl -k start|restart|stop 查看详细

五 .  安装整合 PHP  apache、php

安装支持库

　**编译和安装libxml2 XML解析器**，这一解析器提供PHP 5.0新的XML APL：  
　　

```
tar zxvf libxml2-2.6.32.tar.gz
cd libxml2-2.6.32
./configure --prefix=/usr/local/libxml
make
make install
```

 　　  
　　这一步结束时，libxml2被安装在/usr/local/下。如果你想把它安装在其它地方，你应该在先前步骤中明确指定prefix选项到configure设置中。　  
　　

 

 

**安装加密模块mcrypt**

 

 

源码编译安装，去http://www.sourceforge.net下载Libmcrypt,mhash,mcrypt安装包   
libmcrypt(libmcrypt-2.5.8.tar.gz )：  
mcrypt(mcrypt-2.6.8.tar.gz ):  
  mhash(mhash-0.9.9.9.tar.gz ):

a .先安装Libmcrypt

```
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install 
```

b.安装mhash

```
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make
make install
```

c.安装mcrypt

```
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
LD_LIBRARY_PATH=/usr/local/lib ./configure
make
make install
```

 

 

 

**安装gd**   需要一些依赖库，安装步骤如下：

1、 安装 zlib

```
mkdir /usr/local/zlib
cd zlib-1.2.5
./configure
make
make install
```

  **2、**安装 libpng  **下载地址** [http://www.libpng.org/pub/png/libpng.html](http://www.libpng.org/pub/png/libpng.html)

 

```
#tar -xzf libpng-1.2.23.tar.gz    
#cd libpng-1.2.23                 
#cp scripts/makefile.linux makefile
#make       
#make install
```

   3、 安装 freetype

```
mkdir /usr/local/freetype
tar -xf freetype-2.3.12.tar.gz
cd freetype-2.3.12
./configure --prefix=/usr/local/freetype
make
make install
```

     4、 安装 jpeg  下载地址 [http://www.ijg.org/](http://www.ijg.org/) 

```
cd jpeg-6b
./configure --enable-shared --enable-static
make
make install
```

   5、 安装 gd

```
tar -zxf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure --with-jpeg --with-png --with-freetype
make
make install
```

 　

**php 安装**

 

```
./configure --prefix=/usr/local/php --enable-soap --enable-sockets --enable-mbstring \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-curl \
--with-libxml-dir=/usr/local/libxml \
--with-mcrypt \
--with-zlib \
--with-gd \
--with-zlib-dir=/usr/local/lib \
--with-png-dir=/usr/local/lib \
--with-jpeg-dir=/usr/local/lib \
--with-freetype-dir=/usr/local/lib \
;
make 
make install 
```

 　　  
　　以上例子看起来相当复杂，然而事实并非如此：　　  
　　--prefix设置PHP5 的安装路径。　　  
　　--with-apxs2告诉PHP查找Apache 2.0的地方。　　  
　　--with-libxml-dir和 --with-zlib-dir告诉PHP放置libxml2和zlib库的地方。　　  
　　--with-mysql变量激活regularmySQL扩展功能。　　  
　　--with-mysqli变量激活新增加的MySQL功能。　　  
　　--with-gd变量激活 GD 扩展功能。　　  
　　--with-zlib变量激活ZLIB 压缩库。　　  
　　--enable-sockets变量激活socket通讯特性。　　  
　　--enable-soap变量激活SOAP和Web services支持。　　  
　　当然，也可以尝试其它选项和扩展功能：　　  
　　可以注意到，这些安装过程能够自动将PHP模块安装在正确目录下，以便Apache 2.0的查找。

在 httpd.conf 配置文件中修改如下:

 

加载php模块  
**LoadModule php5\_module modules/libphp5.so**

#将ServerAdmin mailto:linux@linuxidc.com一行改为您的邮箱地址　   
#DocumentRoot "/home/httpd/html/" 此处为html文件主目录　　   
\# 同上　　   
#Options FollowSymLinks MultiViews 为安全起见，去掉"Indexes" 　　   
#   
\# DirectoryIndex default.php default.phtml default.php3 default.html default.htm   
#   
#设置apache的默认文件名次序　　   
AddType application/x-httpd-php .php .phtml .php3 .inc   
AddType application/x-httpd-php-source .phps  
#设置php文件后缀　　   
另外还要修改系统默认的首页类型，建议把DirectoryIndex index.htm改成这样:   
DirectoryIndex index.htm index.html index.php3 index.php default.php  

#ServerName \[url]www.example.com:80\[/url]

ServerName 127.0.0.1

 

修改网站主目录如下

#DocumentRoot "/usr/local/apache/htdocs"

DocumentRoot "/var/www/html"

 

\# Each directory to which Apache has access can be configured with respect

\# to which services and features are allowed and/or disabled in that

\# directory (and its subdirectories).

\#

\# First, we configure the "default" to be a very restrictive set of

\# features.

\# 首先配置的缺省的限制,这个限制是非常严格的

&lt;Directory /&gt;

    Options FollowSymLinks

    AllowOverride None

  Order deny,allow  次序是先拒绝,再允许

    Deny from all  默认是拒绝所有

&lt;/Directory&gt;

 

\# This should be changed to whatever you set DocumentRoot to.

#这里应该改为你设的DocumentRoot

&lt;Directory "/usr/local/apache22/htdocs"&gt;  修改为网站主目录

    #

    # Possible values for the Options directive are "None", "All",

    # or any combination of:

    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

    #

    # Note that "MultiViews" must be named \*explicitly* --- "Options All"

    # doesn't give it to you.

    #

    # The Options directive is both complicated and important.  Please see

    # \[url]http://httpd.apache.org/docs/2.2/mod/core.html#options\[/url]

    # for more information.

    #

    Options Indexes FollowSymLinks

 

    #

    # AllowOverride controls what directives may be placed in .htaccess files.

    # It can be "All", "None", or any combination of the keywords:

    #   Options FileInfo AuthConfig Limit

    #

    AllowOverride None

 

    #

    # Controls who can get stuff from this server.

    #控制谁能访问这个网站

    Order allow,deny  顺序是先允许再拒绝

    Allow from all  默认是允许所有

 

&lt;/Directory&gt;

 

 

5.测试 　   
然后写个php测试页info.php:内容如下  
〈?php  
phpinfo();   
?&gt;   
正常的话，应该能看到php的信息了，恭喜你的Apche+Mysql+PHP安装成功。   
6、apache自动启动- -   
下面用自启动apache为例;  
自启动脚本:  
/usr/local/apache/bin/apachectl start  
文件位于/etc/rc.d/init.d下,名为apached,注意要可执行.  
#chmod +x /etc/rc.d/init.d/apached //设置文件的属性为可执行  
#ln -s /etc/rc.d/init.d/apached /etc/rc3.d/S90apache //建立软连接,快捷方式   
#ln -s /etc/rc.d/init.d/apached /etc/rc0.d/K20apache

　　6.备注   
　 　apache在linux下的默认最大进程数为256，无论如何修改httpd.conf都不能超过这个限制。如果想加大这个限制，在编译apache 前编辑/home/tmp/apache/src/include/httpd.h，将其中#define HARD\_SERVER\_LIMIT 256 一行改为#define HARD\_SERVER\_LIMIT 2048后再编译apache， 

　　apache 1.3.26中mod\_so似乎已经不是默认模块了。编译时候需加上--enable-module=so，我第一次编译没加此参数，结果php编译时无法找到apxs 

　　php 4.2.3中默认配置文件路径似乎变了。编译时需加上 --with-config-file-path=/usr/local/lib 参数，我第一次编译完php.ini放了n个地方都没有用。不得已。只能加上这个参数。