萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Solaris 介紹

Solaris 10 快速部署 Apache + MySQL + PHP (AMP) 生產環境

前言

Solaris 10 內建 Apache 2.x 與 MySQL 4.x,PHP 需自行編譯或透過 Blastwave (pkg-get) 安裝。本文針對已具備基礎操作經驗的管理員,追求快速、正確部署。

一、 Apache 2 啟用與配置

# 1. 複製範本配置
cp /etc/apache2/httpd.conf-example /etc/apache2/httpd.conf

# 2. 編輯關鍵參數
vi /etc/apache2/httpd.conf
# ServerName www.example.com:80
# ServerAdmin admin@example.com
# DocumentRoot "/var/apache2/htdocs" (預設)

# 3. 啟動服務 (SMF)
svcadm enable apache2

# 4. 驗證
curl https://localhost
# 設定開機自啟: svcadm enable -s apache2 (預設已啟用)

二、 MySQL 初始化與安全配置

# 1. 初始化資料目錄 (以 root 執行)
/usr/sfw/bin/mysql_install_db

# 2. 建立 mysql 用戶/群組並設定權限
groupadd mysql
useradd -g mysql -d /var/mysql -s /bin/false mysql
chgrp -R mysql /var/mysql
chmod -R 770 /var/mysql
installf SUNWmysqlr /var/mysql d 770 root mysql

# 3. 複製配置範本
cp /usr/sfw/share/mysql/my-medium.cnf /var/mysql/my.cnf

# 4. 手動啟動測試
/usr/sfw/sbin/mysqld_safe --user=mysql &

# 5. 設定 root 密碼
cd /usr/sfw/bin
./mysqladmin -u root password 'StrongPass123'
./mysqladmin -u root -h `hostname` password 'StrongPass123'

# 6. 驗證連線
./mysqlshow -p
./mysql -u root -p -e "show databases;"

# 7. 設定開機自啟 (SysV 風格,Solaris 10 相容)
ln -s /etc/sfw/mysql/mysql.server /etc/rc3.d/S99mysql
ln -s /etc/sfw/mysql/mysql.server /etc/rc0.d/K00mysql
ln -s /etc/sfw/mysql/mysql.server /etc/rc1.d/K00mysql
ln -s /etc/sfw/mysql/mysql.server /etc/rc2.d/K00mysql
ln -s /etc/sfw/mysql/mysql.server /etc/rcS.d/K00mysql

三、 PHP 編譯安裝 (透過 Blastwave pkg-get)

# 1. 確認 PATH 包含 /opt/csw/bin /usr/sfw/bin
export PATH=/opt/csw/bin:/usr/sfw/bin:$PATH

# 2. 安裝 pkg-get 工具
mkdir -p /usr/local/src && cd /usr/local/src
wget https://www.blastwave.org/pkg_get.pkg
pkgadd -d pkg_get.pkg all

# 3. 安裝完整 wget (取代系統精簡版)
pkg-get -i wget
mv /usr/sfw/bin/wget /usr/sfw/bin/wget.orig
# 重新登入 shell 使新 wget 生效

# 4. 檢查/安裝建構工具鏈 (GNU Autotools)
which autoconf && autoconf --version | head -1  # 需 2.59+
which automake && automake --version | head -1 # 需 1.8.3+
which sed && sed --version | head -1           # 需 4.1.4+
# 缺失者: pkg-get -i autoconf automake sed

# 5. 下載 PHP 原始碼並編譯 (範例 PHP 5.2)
cd /usr/local/src
wget https://museum.php.net/php5/php-5.2.17.tar.gz
tar xzf php-5.2.17.tar.gz
cd php-5.2.17
./configure --prefix=/usr/local/php \
  --with-apxs2=/usr/apache2/bin/apxs \
  --with-mysql=/usr/sfw \
  --with-zlib=/usr \
  --with-gd --enable-mbstring
make && make install

# 6. 複製 php.ini
cp php.ini-recommended /usr/local/php/lib/php.ini
# 編輯: date.timezone, memory_limit, error_reporting 等

# 7. 整合 Apache: 編輯 httpd.conf 載入模組
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php .phtml
DirectoryIndex index.php index.html

# 8. 重啟 Apache
svcadm restart apache2

驗證 AMP 整合

echo "" > /var/apache2/htdocs/info.php
curl https://localhost/info.php | grep -E "PHP Version|MySQL Support|Apache"

生產環境加固建議

  • Apache: ServerTokens Prod, ServerSignature Off, 啟用 mod_security。
  • MySQL: mysql_secure_installation、禁用遠端 root、啟用慢查詢日誌、配置 InnoDB Buffer Pool。
  • PHP: expose_php=Off, display_errors=Off, open_basedir 限制目錄、啟用 OPcache。
  • 定期修補:關注 Oracle Solaris Security Bulletin、Apache/MySQL/PHP 安全公告。
copyright © 萬盛學電腦網 all rights reserved