要真正掌握 LAMP 的運作,自己從 tarball 編一次最有效果。先準備三個套件:httpd-2.0.55.tar.gz、mysql-4.1.22.tar.gz、php-5.0.5.tar.gz。
tar zxvf mysql-4.1.22.tar.gz -C mysqlcd mysql-4.1.22/./configure --prefix=/usr/local/mysql --sysconfdir=/etc --localstatedir=/var/lib/mysqlmake && make install
複製預設設定檔並初始化資料庫:
cp mysql-4.1.22/support-files/my-medium.cnf /etc/my.cnfcd /usr/local/mysql/bin./mysql_install_db./mysqld_safe
修正資料目錄擁有者,並製作服務腳本:
chown -R mysql.mysql /var/lib/mysqlcp mysql-4.1.22/support-files/mysql.server /etc/rc.d/init.d/mysqldchkconfig --add mysqldchmod +x /etc/rc.d/init.d/mysqld
驗證:service mysqld restart 後用 netstat -antp | grep 3306 應能看到 mysqld 正在監聽。
tar zxvf httpd-2.0.55.tar.gz -C httpdcd httpd-2.0.55/./configure --prefix=/usr/local/httpd --with-mpm=prefork --with-mods-shared=more --enable-somake && make install
編輯 /usr/local/httpd/bin/apachectl,在檔頭補上 chkconfig 註解,才能被 service 管理:
#!/bin/sh
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# pidfile: /var/run/httpd.pid
註冊服務並啟動:
cp /usr/local/httpd/bin/apachectl /etc/rc.d/init.d/httpdchkconfig --add httpdchkconfig httpd onservice httpd restart
用 netstat -antp | grep 80 確認埠口已開。
tar zxvf php-5.0.5.tar.gz -C phpcd php-5.0.5/./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-apxs2=/usr/local/httpd/bin/apxsmake && make install
關鍵是 --with-apxs2,它讓 PHP 以模組形式掛進 Apache。用 ls /usr/local/httpd/modules 應能看到 libphp5.so。
複製設定檔並讓 Apache 認得 PHP:
cp php-5.0.5/php.ini-dist /etc/php.ini
編輯 /usr/local/httpd/conf/httpd.conf,在 LoadModule php5_module 那行之後加入:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
寫一個測試頁 /usr/local/httpd/htdocs/test.php,內容為 <?php phpinfo(); ?>,改為 apache 使用者所有,重啟服務後瀏覽 https://192.168.1.100/test.php。能看到 PHP 資訊頁,代表 LAMP 環境完成。