CentOS上部署Apache、MySQL和PHP

centos上yum安装很方便,下面介绍编译安装的方式。

第一步要在CentOS上安装gcc、g++等开发工具

可以从系统光盘上安装,或者

  #yum groupinstall "Development tools"

第二步 Apache安装

Apache是Apache HTTP Server的简称,是世界使用排名第一的web服务器软件。官网是http://www.apache.org/

首先检查系统已安装的httpd。

rpm -qa | grep httpd

如果已安装旧版本的httpd,卸载命令如下:

rpm -e httpd-version.el5.centos --nodeps

如果不带“--nodeps”,系统会提示有依赖关系,不能卸载。加上“--nodeps”后,不会检查依赖关系,只是卸载httpd,跟它有依赖关系的其它软件是不会被删除的。如果使用"yum -y remove httpd",会将与httpd有依赖关系的其它软件也删除。

接下来进行相关软件的安装,包括:

  apr: http://apr.apache.org/download.cgi
  apr-util: http://apr.apache.org/download.cgi
  pcre: http://pcre.org/、 http://sourceforge.net/projects/pcre/

如果不安装,会提示

checking for APR... no
configure: error: APR not found. Please read the documentation.

先选择下载apr,编译安装:

tar zxvf apr-version.tar.gz
cd apr-version
./configure --prefix=/usr/local/apr
make
make install

 接着安装apr-util和pcre

tar zxvf apr-util-version.tar.gz
cd apt-util-version
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install

tar zxvf pcre-version.tar.gz
cd pcre-version
./configure --prefix=/usr/local/pcre
make
make install

然后安装httpd。

tar zxvf httpd-version.tar.gz
cd httpd-version
./configure --prefix=/usr/local/apache2 --with-ssl=/usr/local/openssl --enable-ssl --enable-so --enable-dav --enable-dav-fs --enable-dav-lock --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre
make
make install

注:

1、如果想要启用所有模块:--enable-mods-shared=all

2、一定要加--enable-so,核心才能够装载DSO

3、--enable-dav是安装mod_dav_svn.so跟mod_authz_svn.so这两个模块

4、如果要编译一个多线程版本的Apache,可以在configure时添加--with-mpm=worker

5、如果在安装过程中出现这些错误

make[4]: *** [mod_ssl.la] 错误 1
make[4]: Leaving directory `/root/httpd-2.4.7/modules/ssl'
make[3]: *** [shared-build-recursive] 错误 1
make[3]: Leaving directory `/root/httpd-2.4.7/modules/ssl'
make[2]: *** [shared-build-recursive] 错误 1
make[2]: Leaving directory `/root/httpd-2.4.7/modules'
make[1]: *** [shared-build-recursive] 错误 1
make[1]: Leaving directory `/root/httpd-2.4.7'
make: *** [all-recursive] 错误 1

这是因为openssl的版本过低,不需要卸载,可以直接安装新版的openssl。源码安装openssl时,configure是这样的

./config --prefix=/usr/local/openssl/ -fPIC

Position Independent Code is necessary for a module like mod_ssl which may get placed anywhere in memory at runtime. It is not necessary for the standalone OpenSSL test program, which is why make test worked OK without -fPIC.

然后在安装apache时configure参数中加上

  --with-ssl=/usr/local/openssl

最后,将apache设为开机启动(两种方法):

一是在/etc/rc.d/rc.local文件中加入一行

/usr/local/apache2/bin/apachectl start

二是将apache安装位系统服务

cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
vim /etc/rc.d/init.d/httpd
#在(#! /bin/sh)下面添加
chkconfig: 2345 10 90
description:Activates/Deactivates Apache Web Server

最后,运行chkconfig把apache添加到系统的启动服务组里面:

chkconfig --add httpd
chkconfig httpd on

第三步 安装MySql

可以直接用yum安装mysql-client、mysql-server、mysql-devel。

或者下载源码安装。其它博文里说的很清楚了,这里不再赘述,只是介绍一下在CentOS下用yum安装。

首先在http://dev.mysql.com/downloads/repo/页面选择下载你所需要的yum repository。然后,

对基于el6的系统,命令如下

sudo yum localinstall mysql-community-release-el6-{version-number}.noarch.rpm

对Fedora19,命令如下

sudo yum localinstall mysql-community-release-fc19-{version-number}.noarch.rpm

第四步 安装PHP

在PHP官网下载源代码安装。

安装过程中,因为PHP依赖很多包,所以总是回不断报错,安装时要耐心。

tar zxvf php-version.tar.gz
cd php-version
./configure --prefix=/usr/local/php --with-mysql --with-mysqli --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib --with-bz2  --with-gd --enable-gd-native-ttf --enable-gd-jis-conv  --enable-mbstring --with-iconv --with-curl --enable-static --enable-zend-multibyte --enable-inline-optimization --enable-zend-multibyte --enable-sockets --enable-soap --with-openssl=/usr/local/openssl --with-gettext --enable-ftp
make
make test
make install

下面是我在安装PHP过程中碰到的错误以及解决方法。

xml2-config not found. Please check your libxml2 installation.
yum install libxml2-devel -y
configure: error: Please reinstall the BZip2 distribution
yum install bzip2 bzip2-devel
configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/
 yum install libcurl-devel
configure: error: png.h not found.
yum install libpng-devel
configure: error: Cannot find MySQL header files under /usr/local/mysql.
yum install mysql-devel

可以看出,很多错误是因为依赖开发版的软件。目前各种Linux套件在发行时,都会将软件的内容分为一般使用和开发使用(development)两大类。而默认情况下,开发使用的都不会安装,因此我们在安装一些软件时,不得不去安装开发版的依赖软件包(真不明白,使用Linux的大部分不都是苦逼的程序员吗,自己人何必要为难自己人)。

最后是整合apache和php。

编辑httpd.conf文件以调用PHP模块。LoadModule达式右边的路径必须指向系统中的PHP模块,make install命令可能已经完成了这些,但最好检查一下。

在httpd.conf中找到下面这一行

#LoadModule rewrite_module modules/mod_rewrite.so

去掉左侧的“#”,改为

LoadModule php5_module     modules/libphp5.so

同时要告知Apache将特定的扩展名解析成PHP,例如,让Apache将扩展名.php解析成PHP。为了避免潜在的危险,例如上传或者创建类似exploit.php.jpg的文件并被当作PHP执行,这里不再使用Apache的AddType指令来设置。参考下面的例子,可以简单的将需要的扩展名解释为PHP。

<FilesMatch .php$>
    SetHandler application/x-httpd-php
</FilesMatch>

如果想将.php,.php2,.php3,.php4,.php5,.php6,以及.phtml文件都当作PHP来运行,无需额外的设置,只需将上面的那段改为

<FilesMatch ".ph(p[2-6]?|tml)$">
        SetHandler application/x-httpd-php
</FilesMatch>

或者想要将.phps文件由PHP源码过滤器处理,使得其在显示时可以高亮代码,可以增加下面的设置

<FilesMatch ".phps$">
        SetHandler application/x-httpd-php-source
</FilesMatch>

mod_rewrite也有助于将那些不需要运行的.php文件的源码高亮显示,而并不需要将他们更名为.phps文件

RewriteEngine on
RewriteRule (.*.php)s$ $1 [H=application/x-httpd-php-source]

不要在正式生产运营的系统上启动 PHP 源码过滤器,因为这可能泄露系统机密或者嵌入的代码中的敏感信息。

之后重启Apache服务:

/usr/local/apache2/bin/apachectl restart

或者

service httpd restart

第五步 测试

在htdocs/下,

  vim test.php

输入

  <?php echo phpinfo();?>

然后浏览器中查看test.php。会显示PHP的配置参数等,如下图

原文地址:https://www.cnblogs.com/xianzhedeyu/p/3587458.html