安裝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