Centos6.5下安装php

安装php

yum -y install php

重启httpd服务激活php:
/etc/init.d/httpd restart

测试php是否安装完成
vim /var/www/html/info.php

<?php
phpinfo();
?>
安装php的组件:

搜索php组件:
yum search php

安装常用组件:
yum -y install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc  php-bcmath php-mbstring

记得安装完要重启httpd服务激活组件:
/etc/init.d/httpd restart

php配置文件:

vi /etc/php.ini

memory_limit=128M

post_max_size=8M

upload_max_filesize=2M

一条命令安装

yum -y install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel libxml2-devel gettext-devel pcre-devel mysql-devel net-snmp-devel curl-devel perl-DBI mysql mysql-server httpd php php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc  php-bcmath php-mbstring

第一步:配置Apache

修改Apache配置文件:vi /etc/httpd/conf/httpd.conf,并在文件中查找下面所在行,进行修改(查找可以在vi的一般模式下输入“/要查找的字符”进行查找):

ServerTokens OS           修改为:     ServerTokens Prod                       (在出现错误页的时候不显示服务器操作系统的名称)
ServerSignature On        修改为:     ServerSignature Off                      (在错误页中不显示Apache的版本)
Options Indexes FollowSymLinks     修改为:     Options Includes ExecCGI FollowSymLinks             (允许服务器执行CGI及SSI,禁止列出目录)
#AddHandler cgi-script .cgi           修改为:     AddHandler cgi-script .cgi .pl                                       (允许扩展名为.pl的CGI脚本运行)
AllowOverride None                      修改为:     AllowOverride All                                 (允许.htaccess)
AddDefaultCharset UTF-8            修改为:     AddDefaultCharset GB2312                                     (添加GB2312为默认编码)

Options Indexes MultiViews FollowSymLinks      修改为      Options MultiViews FollowSymLinks        (不在浏览器上显示树状目录结构)
DirectoryIndex index.html index.html.var             修改为:  DirectoryIndex index.html index.htm Default.html Default.htm index.php Default.php index.html.var  (设置默认首页文件,增加index.php)
KeepAlive Off                                  修改为:     KeepAlive On                                (允许程序性联机)
MaxKeepAliveRequests 100           修改为:     MaxKeepAliveRequests 1000   (增加同时连接数)

修改好之后保存配置,重新启动Apache:/etc/init.d/httpd restart

建议删除默认的测试页面:rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html 

第二步:配置PHP

修改PHP配置文件:vi /etc/php.ini,以下需要修改的行的位置可以通过vi的查找命令来查找:

date.timezone = PRC                    #把前面的分号去掉,改为date.timezone = PRC

disable_functions =                             passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru, stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

#列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。

expose_php = Off                                     #禁止显示php版本的信息

display_errors = OFF                               #关闭错误提示
register_globals = OFF                           #关闭注册全局变量
magic_quotes_gpc = On                        #打开magic_quotes_gpc来防止SQL注入
log_errors = On                                         #记录错误日志
error_log = /var/log/php/error_log.log   #设置错误日志存放目录,文件必须允许apache用户的和组具有写的权限(注意,在修改之前,要先创建文件/var/log/php/error_log.log,然后在修改其属性,使其属于apache用户和用户组。chown apache /var/log/php/error_log.log和chgrp apache /var/log/php/error_log.log)

open_basedir = .:/tmp/                             #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站

经过安装和配置之后,web服务器基本上已经搭建起来了,可以对其进行访问。

测试篇

在目录/var/www/html下:cd /var/www/html

创建php文件:vi index.php

<?php
phpinfo();
?>

那么,在浏览器中输入本机地址的时候,就可以访问到刚才创建的index.php网页文件了。

注意:apache的默认程序目录是:/var/www/html,网页文件放在这里就可以被访问到。需要保证这个目录是属于用户apache和用户组apache的。

         MySQL的数据库目录是:/var/lib/mysql

 GPG key retrieval failed: [Errno 14] Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-

今天更新为163的源后,yum的时候报错:GPG key retrieval failed: [Errno 14] Could not open/read file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5.

解决方案:vi /etc/yum.repos.d/CentOS-Base.repo 

把文件里所有的RPM-GPG-KEY-CentOS-5更改为RPM-GPG-KEY-CentOS-6就可以了

vim 替换 :%s/RPM-GPG-KEY-CentOS-5/RPM-GPG-KEY-CentOS-6