若非需客製化編譯參數,yum 安裝 LAMP 環境穩定、簡單、易維護,推薦生產環境使用。
CentOS 官方源缺少部分 PHP 擴展(如 php-mcrypt),需加入 rpmforge 與 epel:
# 32 位元系統範例(請自行換用最新版本)
rpm -ivh https://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
rpm -ivh https://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm
# Apache
yum -y install httpd httpd-devel
# MySQL
yum -y install mysql mysql-devel mysql-server mysql-libs
# PHP 核心
yum -y install php
# PHP 常用擴展
yum -y install php-mysql php-gd php-imap php-ldap php-odbc php-mbstring php-devel php-soap php-cli php-pdo
yum -y install php-mcrypt php-tidy php-xml php-xmlrpc php-pear
# 選擇性擴展(視需求)
yum -y install php-pecl-memcache php-eaccelerator
# phpMyAdmin
yum -y install phpmyadmin
# 啟動服務
service httpd start
service mysqld start
chkconfig mysqld on
chkconfig httpd on
編輯 /usr/share/phpmyadmin/config.inc.php:
$cfg['blowfish_secret'] = 'your_random_string_here'; // 自訂任意字串
安裝 epel 來源後執行 yum install php-mcrypt。
預設僅允許 localhost。修改 /etc/httpd/conf.d/phpmyadmin.conf:
<Directory "/usr/share/phpmyadmin">
Order Deny,Allow
# Deny from all
Allow from All
</Directory>
⚠ 安全警告:直接開放存取有風險,建議搭配 IP 限制、VPN 或 SSH Tunnel。
編輯 httpd.conf 加入:
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps
# 設定 root 密碼
mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD('newpassword') WHERE user='root';
mysql> FLUSH PRIVILEGES;
# 允許遠端登入
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;