CentOS6.5下安装Apache2.4+PHP5.6

来源:互联网 发布:tensorflow api 下载 编辑:程序博客网 时间:2024/05/21 04:20

可能立刻会有人要问:为啥不装MySql,这是因为本次项目准备购买云RDS,所以就不在系统中自己安装MySql了。

 

言归正传,开始安装系统。

 

一,卸载系统自带Apache

首先我个人觉得应该要卸载掉系统中自带的apache软件:

 

首先我们检查系统中是否已经安装了httpd服务:

root@server ~]# rpm -qa|grephttpd

httpd-2.2.3-11.el5_2.centos.4

httpd-manual-2.2.3-11.el5_2.centos.4

 

说明:rpm –qa | grep mysql 命令是为了把mysql相关的包都列出来,我上面的例子是Linux默认安装apache的rpm软件包列表,如果是别的Linux版本列出来的列表有可能会不一样,不过不用担心,不管是什么,卸载都从最下面的一个包开始,直到卸载掉第一个为止。

 

比如:在这个例子中,我们应该先卸载httpd-manual-2.2.3-11.el5_2.centos.4方法如下:

rpm –ehttpd-manual-2.2.3-11.el5_2.centos.4

 

如果卸载不掉,则会显示软件的依赖关系,则可以删除掉依赖的软件,然后再来卸载当前软件包。

 

如果实在觉得依赖软件的关系链太长太复杂,则可以强行删除,添加—nodeps参数即可,指令如下:

rpm –ehttpd-manual-2.2.3-11.el5_2.centos.4 --nodeps

 

个人观点:删除掉自带的apache对于今后确认apache出现的问题有好处。

 

1.1,准备工作

首先要下载所需软件的源码包,有如下这些:

apr-1.5.2.tar.gz

apr-util-1.5.4.tar.gz

pcre-8.36.tar.gz

httpd-2.4.17.tar.gz

 

php-5.6.15.tar.gz

 

把所有的源码包上传到服务器上。

 

1.2,安装Apache2.4

 

首先要安装Apache的依赖库

apr-1.5.2.tar.gz

apr-util-1.5.4.tar.gz

pcre-8.36.tar.gz

 

 

tar zxvf apr-1.5.2.tar.gz

cd apr-1.5.2

./configure--prefix=/usr/local/apr

make && make install

 

tar zxvfapr-util-1.5.4.tar.gz

cd apr-util-1.5.4

./configure--prefix=/usr/local/apr-util --with-apr=/usr/local/apr

make && make install

 

tar zxvf pcre-8.36.tar.gz

cd pcre-8.36

./configure --prefix=/usr/local/pcre--with-apr-util=/usr/local/apr-util --with-apr=/usr/local/apr

make && make install

 

安装PCRE的时候遇到如下错误:

You need a C++ compiler forC++ support

解决方案是:

yum install -y gcc gcc-c++

注意:这个-y千万不能少。

 

 

可以开始安装Apache了,

解压缩

cd httpd-2.4.17

 

./configure--prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util--with-pcre=/usr/local/pcre --enable-so --enable-rewrite

 

make && make install

 

注意:之前安装的时候从windows上复制的./configure配置参数,结果中间不知为何多出来一些换行符,导致运行结果出错了,所以大家拷贝指令的时候一定要小心。

 

【报错】/usr/bin/ld: cannotfind -l*

主要的原因是库文件并没有导入的ld检索目录中

 

比如说我就遇到了如下两个错误:

/usr/bin/ld: cannot find -lssl

/usr/bin/ld: cannot find -lcrypto

这两个错误就表示:libssl.so和libcrypto.so这两个文件并不在ld检索的目录下面。

 

这两个so文件经过查找,其实就在/usr/local/ssl/lib文件夹下面,并且/usr/local/ssl/lib也已经存在于ld的配置文件中:/etc/ld.so.conf文件。但是就是没有起作用。

 

我的解决方案是:我没有去学习ld的工作机制,我在ld默认的Lib检查目录之一的/usr/local/lib中增加了以上两个so文件的外链,指令如下:

 

cd /usr/local/lib

ln -sv/usr/local/ssl/lib/libssl.so   libssl.so

ln -sv/usr/local/ssl/lib/libcrypto.so  libcrypto.so

 

这样的话,apahce的报错问题就解决了。

 

 

1.3,将Apache添加成httpd服务并开机自启动

如果没有httpd 服务的时候,每次启动都要运行如下指令:

/usr/local/apache/bin/apachectl start

好难受的说,下面就将httpd装到服务中,同理也可以用到其他服务的操作。

 

1.将apachectl文件copy一分到/etc/rc.d/init.d中,然后再/etc/rc.d/rc5.d中加入链接。

其中init.d中的脚本就相当于window中的注册表,在系统启动的时候某些指定的脚本被执行。而rc5.d就和rc3.d差不多吧。也都是一些脚本只是执行级别不同。

命令如下:

cp/usr/local/apache/bin/apachectl /etc/init.d/httpd

ln -s /etc/init.d/httpd/etc/rc.d/rc5.d/S85httpd

 

2.运行chkconfig --list 发现列表中没有httpd,通过chkconfig --add httpd来添加,可能会提示httpd服务不支持chkconfig,需要编辑/etc/rc.d/init.d/httpd

 

在第二行添加以下注视信息:

# chkconfig: 345 85 15

# description:Activates/Deactivates Apache Web Server

345代表哪些linux级别需要启动httpd,启动序号是85,关闭序号是15。

保存以后执行 chkconfig --addhttpd 添加成功

 

3.运行chkconfig --list httpd 基本就存在了。然后就可以用了。service httpd start 和 service httpd stop

 

 

二,安装PHP5.6.15

2.1 源代码安装PHP

解压缩

Cd php-5.6.15

配置参数太复杂于是去网上找了一个大牛的推荐,如下:

./configure--prefix=/usr/local/php--with-apxs2=/usr/local/apache2/bin/apxs--with-libxml-dir=/usr/include/libxml2 --with-config-file-path=/usr/local/apache2/conf--with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config--with-gd--enable-gd-native-ttf --with-zlib--with-mcrypt--with-pdo-mysql=/usr/local/mysql --enable-shmop --enable-soap--enable-sockets--enable-wddx --enable-zip --with-xmlrpc --enable-fpm--enable-mbstring--with-zlib-dir --with-bz2 --with-curl --enable-exif--enable-ftp--with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib--with-freetype-dir=/usr/lib/

 

于是乎遇到了一系列的报错,推荐我之前的一篇文章介绍了常见错误的解决办法:

http://blog.csdn.net/dodott/article/details/49664379

 

我遇到的问题如下:

【报错】configure errorxml2-config not found. please check your libxml2 installation

解决方案:

Centos: yum install libxml2

yum install libxml2-devel -y

 

【报错】Configure: error:Please reinstall the BZip2 distribution

解决方案:

centos: yum install bzip2bzip2-devel

debian: apt-get installbzip2-devel

 

 

【报错】

configure: error: Pleasereinstall the libcurl distribution -

    easy.h should bein<curl-dir>/include/curl/

解决方案:

centos: yum install curlcurl-devel (For Redhat & Fedora)

 

 

【报错】

configure: error: mcrypt.hnot found. Please reinstalllibmcrypt.

解决方案:

网上大部分给的方法是使用如下指令

yum install libmcryptlibmcrypt-devel (For Redhat & Fedora)

但是基本上都没有作用,系统甚至会提示:nothingto do。估计可能和YUM源的软件版本太低有关系。

 

正确做法是自己下载源码来安装:

libmcrypt-2.5.7.tar.gz

 

cd libmcrypt-2.5.7

#编译(默认安装到/usr/local/lib/)

./configure--prefix=/usr/local/libmcrypt

 

#执行安装

make && make install

 

注意:这里的安装路径要记住,等会安装PHP的时候会用到。

 

 

继续回到PHP的安装,此时的配置参数修改为:

./configure--prefix=/usr/local/php--with-apxs2=/usr/local/apache2/bin/apxs--with-libxml-dir=/usr/include/libxml2--with-config-file-path=/usr/local/apache2/conf--with-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config--with-gd --enable-gd-native-ttf--with-zlib --with-pdo-mysql=/usr/local/mysql--enable-shmop --enable-soap--enable-sockets --enable-wddx --enable-zip--with-xmlrpc --enable-fpm--enable-mbstring --with-zlib-dir --with-bz2--with-curl --enable-exif--enable-ftp --with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib--with-freetype-dir=/usr/lib/--with-mcrypt=/usr/local/libmcrypt

 

修改内容是:

去掉了--with-mcrypt,在最后增加了--with-mcrypt=/usr/local/libmcrypt

 

【报错】configure: error:libjpeg.(a|so) not found

configure: error: png.h not found.

解决方法:

关于jpeg的问题,安装如下软件包

yum -y install libjpeg-devel

 

关于png的问题,安装如下软件包

yum -y install libpng-devel

 

【报错】

configure: error: Cannot findMySQL header files under/usr/local/mysql.

Note that the MySQL clientlibrary is not bundled anymore!

 

这个问题是因为没有安装mysql,所以找不到mysql的运行库。

但是本次安装本身就不想安装完整的mysql软件,去php官网查了资料后找到如下一段翻译文字:

“对于 php-5.3.0或更新版本,mysqli 默认使用Mysql Native Driver作为驱动。 这个驱动比libmysql会有一些优势, --with-mysql=mysqlnd”

 

最终configure参数修改为:

./configure--prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs--with-libxml-dir=/usr/include/libxml2--with-config-file-path=/usr/local/apache2/conf --with-mysql=mysqlnd--with-mysqli=mysqlnd --with-gd --enable-gd-native-ttf --with-zlib--with-pdo-mysql=mysqlnd --enable-shmop --enable-soap --enable-sockets--enable-wddx --enable-zip --with-xmlrpc --enable-fpm --enable-mbstring  --with-zlib-dir --with-bz2 --with-curl--enable-exif --enable-ftp --with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib--with-freetype-dir=/usr/lib/ --with-mcrypt=/usr/local/libmcrypt

 

注意:上面红色标记出来的目录就是后面php.ini需要放置的目录。

 

 

到此终于把PHP的configure成功通过。

make 和 makeinstall。PHP安装完毕。

 

2.2,修改PHP的配置文件php.ini

进入php源码目录,选择php.ini-development复制一份到/usr/local/apache2/conf,并改名为php.ini使用vi打开,查找extension_dir,修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20131226",读者根据自己的PHP安装目录结构配置,目的是找到PHP的扩展库。

查找extension=php_,去掉extension=php_curl.dll,extension=php_gd2.dll,extension=php_mbstring.dll,extension=php_mysql.dll,extension=php_mysqli.dll,extension=php_pdo_mysql.dll,extension=php_xmlrpc.dll前面 的分号。查找short_open_tag= Off把它修改成short_open_tag = On,让其支持短标签(我看注释这个默认是打开的)。

 

 

从别人的服务器上我还拷贝了如下文件放到

/usr/local/php/lib/php/extensions/no-debug-zts-20131226目录,

文件如下:

Imap.so

Mcrypt.so

Memcache.so

Openssl.so

Zip.so

 

然后在php.ini的最后增加如下配置文字:

extension=memcache.so

extension=openssl.so

extension=mcrypt.so

extension=zip.so

  

 

 

 

2.3,修改Apache配置文件httpd.conf相关修改以支持PHP

vi/usr/local/apache/conf/httpd.conf

 

Ø  添加php支持。

【添加字段一】

AddTypeapplication/x-httpd-php .php .phtml

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

 

【添加字段二】

<FilesMatch \.php$>

SetHandlerapplication/x-httpd-php

</FilesMatch>

 

 

Ø  添加默认索引页面index.php,再找到“DirectoryIndex”,在index.html后面加上“ index.php”

DirectoryIndex index.htmlindex.php

 

Ø  3. 不显示目录结构,找到“Options Indexes FollowSymLinks”,修改为

Options FollowSymLinks

 

Ø  4. 开启Apache支持伪静态,找到“AllowOverride None”,修改为

AllowOverride All

 

重启Apache

service httpd restart

 

提醒:实在不知道怎么配置,就找个已经搭建成功的服务器把配置文件弄过来对比一下。

 

此时还会遇到如下报错:

 

httpd: Could not reliablydetermine the server's fully qualified domain name

 

解决办法:

 

linux :/usr/local/apache/conf

 

用记事本打开httpd.conf

 

将里面的#ServerNamelocalhost:80注释去掉即可。

 

【报错】:我也曾经配置成了ServerName127.0.0.1:80,结果局域网其他电脑就没法访问了,原因不清楚。

 

 

 到此,整个Apache+PHP5.6的环境搭建完毕。

 

 

2.4,使用小技巧

【查看Apache的版本号】

运行apache安装目录下的/bin/httpd -v,具体实践后的指令是:

#进入apache安装目录

#cd  /usr/local/apache2/bin

#./httpd -v

Server version: Apache/2.4.17(Unix)

Server built:   Feb 23 2016 15:21:50

 

三,防火墙的管理

1) 重启后生效

开启: chkconfig iptables on

关闭: chkconfig iptables off

 

2) 即时生效,重启后失效

开启: service iptables start

关闭: service iptables stop

 

需要说明的是对于Linux下的其它服务都可以用以上命令执行开启和关闭操作。

====================================================================

写的比较乱,基本上就是把遇到的问题的解决方案一一罗列了一下,

从中还是可以学习到很多操作系统的技巧的,大家共勉。




0 0
原创粉丝点击