http://down.chinaz.com/server/201109/1064_1.htm
一、安装 MySQL
首先来进行 MySQL 的安装。打开超级终端,输入:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># yum install mysql mysql<span class="sh_symbol">-</span>server
安装完毕,让 MySQL 能够随系统自动启动:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># chkconfig <span class="sh_symbol">--</span>levels <span class="sh_number">235</span> mysqld on
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># <span class="sh_regexp">/etc/i</span>nit<span class="sh_symbol">.</span><span class="sh_normal">d</span><span class="sh_symbol">/</span>mysqld start
设置 MySQL 数据 root 账户的密码:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># mysql_secure_installation
当出现如下提示时候直接按回车:
Enter current password <span class="sh_keyword">for</span> root
出现如下再次回车:
Set root password<span class="sh_symbol">?</span> <span class="sh_symbol">[</span><span class="sh_normal">Y</span><span class="sh_symbol">/</span>n<span class="sh_symbol">]</span>
出现如下提示输入你需要设置的密码,回车后在输入一次确认:
New password<span class="sh_symbol">:</span>
接下来还会有四个确认,分别是:
Remove anonymous users<span class="sh_symbol">?</span> <span class="sh_symbol">[</span><span class="sh_normal">Y</span><span class="sh_symbol">/</span>n<span class="sh_symbol">]</span>
Disallow root login remotely<span class="sh_symbol">?</span> <span class="sh_symbol">[</span><span class="sh_normal">Y</span><span class="sh_symbol">/</span>n<span class="sh_symbol">]</span>
Remove test database and access to it<span class="sh_symbol">?</span> <span class="sh_symbol">[</span><span class="sh_normal">Y</span><span class="sh_symbol">/</span>n<span class="sh_symbol">]</span>
Reload privilege tables now<span class="sh_symbol">?</span> <span class="sh_symbol">[</span><span class="sh_normal">Y</span><span class="sh_symbol">/</span>n<span class="sh_symbol">]</span>
直接回车即可。
二、安装 Apache 组件
由于 CentOS 已经封装了 Apache,直接运行安装:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># yum install httpd
同样配置系统让 Apache 随系统启动:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># chkconfig <span class="sh_symbol">--</span>levels <span class="sh_number">235</span> httpd on
配置完毕,启动 Apache:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># <span class="sh_regexp">/etc/i</span>nit<span class="sh_symbol">.</span><span class="sh_normal">d</span><span class="sh_symbol">/</span>httpd start
此时已经可以访问你的服务器,如“http://127.0.0.1/”不出意外的话,能够看到 “Apache 2 Test Page powered by CentOS” 的测试页面。注意,如果其他机器访问这台服务无法显示这个页面,而直接在这台服务器上可以访问的话,一般情况下是 CentOS 自带的防火墙禁止了。你只需要进入防火墙,将 “WWW” 对应的 “80” 端口打开即可。
注意:在 CentOS 中 Apache 的默认根目录是 /var/www/html,配置文件 /etc/httpd/conf/httpd.conf。其他配置存储在 /etc/httpd/conf.d/ 目录。
三、安装 PHP
输入如下指令安装 PHP:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># yum install php
需要重新启动 Apache 服务:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># <span class="sh_regexp">/etc/i</span>nit<span class="sh_symbol">.</span><span class="sh_normal">d</span><span class="sh_symbol">/</span>httpd restart
四、测试 PHP 相关信息
这步实际上可以省略,但是为了测试是否安装成功,你可以新建一个 PHP 页面进行测试,使用 vim 编辑器新建:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># <span class="sh_normal">vi </span><span class="sh_symbol">/</span><span class="sh_keyword">var</span><span class="sh_regexp">/www/</span><span class="sh_normal">html</span><span class="sh_symbol">/</span>info<span class="sh_symbol">.</span>php
按 “i” 键进行编辑,输入:
<span class="sh_symbol"><?</span>php
<span class="sh_function">phpinfo</span><span class="sh_symbol">();</span>
<span class="sh_symbol">?></span>
编辑完毕,按 “ESC” 键退出编辑模式,接着输入:
<span class="sh_symbol">:</span>wq
然后回车,即保存并退出。
此时你可以访问你的站点地址,例如 “http://127.0.0.1/info.php”,查看是否能看到相关的 PHP 信息。
看到这样的图,就说明 PHP 安装成功了。
五、将 PHP 模块和 MySQL 模块关联起来
还需要将 PHP 和 MySQL 关联起来,才能正常工作。搜索模块:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># yum search php
安装相关模块:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># yum install php<span class="sh_symbol">-</span>mysql php<span class="sh_symbol">-</span>gd php<span class="sh_symbol">-</span>imap php<span class="sh_symbol">-</span>ldap php<span class="sh_symbol">-</span>odbc php<span class="sh_symbol">-</span>pear php<span class="sh_symbol">-</span>xml php<span class="sh_symbol">-</span>xmlrpc
需要重启 Apache 模块才能生效:
<span class="sh_symbol">[</span>root@localhost <span class="sh_symbol">~]</span># <span class="sh_regexp">/etc/i</span>nit<span class="sh_symbol">.</span><span class="sh_normal">d</span><span class="sh_symbol">/</span>httpd restart
再次刷新刚才建立的 “info.php” 页面,往下拉找到相关 MySQL 模块,看是否检测到相关信息。
如果看到如上图相关信息,则说明 MySQL 工作正常了。