1、前言
LAMP - Wikipedia 應該不用多說了吧,不過本次實作 Web Server 不是使用 Apache,而是使用小而輕巧的 Lighttpd 輕量級 Web Server,不知是不是應該改叫 LLMP (Linux Lighttpd MySQL PHP)。文章目錄
1、前言2、實作環境
3、MySQL 安裝及設定
步驟1.安裝 MySQL Database
步驟2.修改 /etc/my.cnf
步驟3.啟動 MySQL
步驟4.設定 MySQL 管理者密碼
步驟5.測試登入 MySQL
步驟6.建立其它 MySQL 帳號
補充:基本 MySQL 操作指令
4、Lighttpd 安裝及設定
步驟1.安裝 lighttpd、lighttpd-fastcgi 套件
步驟2.修改 lighttpd 設定檔
步驟3.設定開機自動啟動 lighttpd 服務
步驟4.啟動 lighttpd 服務
5、PHP 安裝及設定
步驟1.安裝 PHP 5
步驟2.查尋安裝 PHP 是否成功
步驟3.測試 Lighttpd 能否正確處理 php 檔案
6、參考
7、Me FAQ
Q1.rpm 安裝 MySQL-5.0.77-0.src.rpm 失敗?
Q2.configure: error: no acceptable C compiler found in $PATH?
Q3.checking for termcap functions library... configure: error: No curses/termcap library found?
Q4.mysqladmin: connect to server at 'localhost' failed?
Q5.ERROR 1064 (42000) at line 1: You have an error in your SQL syntax near?
Q6.如何移除預設的 mysql-5.0.45-7.el5.i386?
Q7.error: cannot create %sourcedir /usr/src/redhat/SOURCES?
Q8.phpinfo 無法顯示內容?
Q9.想要安裝指定的 MySQL 版本怎麼辦?
2、實作環境
- CentOS 5.1 (Linux 2.6.18-53.1.4.el5)
 - mysql-server-5.0.22-2.2.el5_1.1
 - lighttpd-1.4.18-1.el5.rf
 - php-5.1.6-15.el5
 - php-mysql-5.1.6-15.el5
 
3、MySQL 安裝及設定
步驟 1. 安裝 MySQL Database
我使用群組安裝把相關會用到的套件都裝入,在安裝以前先查看會安裝哪些套件。# yum groupinfo "MySQL Database"   //查尋 MySQL 群組套件會安裝哪些套件
 Group: MySQL Database
  Description: This package group contains packages useful for use with MySQL.
  Mandatory Packages:        //強制安裝套件
    mysql
  Default Packages:          //預設會安裝的套件
    unixODBC
    mysql-server
    MySQL-python
    mysql-connector-odbc
    libdbi-dbd-mysql
    perl-DBD-MySQL
  Optional Packages:         //建議安裝 (視個人需求自行安裝)
    mod_auth_mysql
    mysql-devel              //MySQL 程式開發套件及函式庫
    qt-MySQL
    mysql-bench              //MySQL 效能測試套件
    php-mysql接下來就利用 yum 來進行群組安裝 MySQL 吧。
# yum groupinstall "MySQL Database"你可能會問說怎麼沒有 MySQL-shared (MySQL 用戶端函式庫套件),這個套件若需要就到MySQL Download 找對應的 MySQL 版本及需要的 rpm 來安裝吧。
步驟 2. 修改 /etc/my.cnf
我想將 DB 放置資料夾從預設的 /var/lib/mysql 改放到 /home/db/mysql 下 (我喜歡集中管理)。# mkdir /home/db ; mkdir /home/db/mysql
# chown mysql:mysql /home/db/mysql
# vim /etc/my.cnf
 mysqld
 datadir=/var/lib/mysql     //預設值
 datadir=/home/db/mysql     //修改後再來就是執行 chkconfig 將 mysqld 設定在 runlevel 2 ~ 5 為啟動 MySQL 服務。
# chkconfig mysqld on
# chkconfig --list |grep mysqld     //查看設定是否生效
 mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off步驟 3. 啟動 MySQL
# /etc/rc.d/init.d/mysqld start   //啟動 MySQL 服務
 Initializing MySQL database:  Installing all prepared tables Fill help tables
 Starting MySQL:                                              [OK] 檢查 MySQL 是否啟動成功並 Listen 相關 Port。
# netstat -tnl
 Active Internet connections (only servers)
 Proto Recv-Q Send-Q   Local Address    Foreign Address     State
 tcp     0      0      0.0.0.0:3306     0.0.0.0:*           LISTEN步驟 4. 設定 MySQL 管理者密碼
剛安裝完 MySQL 後記得趕快設定管理者密碼 (這裡的 root 跟 CentOS 的 SuperUser Root 沒關係哦!!)。# mysqladmin -u root password YOUR_PASSWORD     //設定新密碼
# mysqladmin -u root -p password NEW_PASSWORD   //日後若要更改密碼步驟 5. 測試登入 MySQL
設定好 MySQL 管理者密碼後試著登入看看吧。#mysql -u root -p
 Enter password:               //輸入剛才設定的 MySQL 密碼
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1581 to server version: 5.0.27
 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 mysql>                        //成功登入 mysql步驟 6. 建立其它 MySQL 帳號
以下命令為表示允許連線主機來源是 localhost 以 sqluser1 的 MySQL 帳號及密碼為 1234 登入的使用者,能對指定的資料庫 testdb 擁有所有權限。mysql> grant all privileges on testdb.* to sqluser1@localhost IDENTIFIED BY '1234';
 Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES ;             //重新載入(使變更生效)
 Query OK, 0 rows affected (0.01 sec)補充:基本 MySQL 操作指令
以下記錄常用基本 MySQL 指令及語法,詳細內容可參考 MySQL :: MySQL 5.1參考手冊 :: 13. SQL語句語法。- 登入 MySQL: mysql -u root -p
 - 顯示所有資料庫: mysql> show databases;
 - 切換資料庫: mysql> use databases_name;
 - 顯示 Tables: mysql> show tables;
 - 查詢指令的 Tables 內容: select * from tables_name;
 - 插入新資料列: mysql> INSERT INTO `Table_Name` ( `欄位1` , `欄位2`) VALUES ( `值1` , `值2` );
 - 更新資料列: mysql> UPDATE `Table_Name` SET `欄位1` = `值1` , `欄位2` = `值2` WHERE where_definition;
 - 更新資料庫: mysql> FLUSH PRIVILEGES;
 - 匯入 Dump Files 至指定的資料庫: mysql -u root -p databases_name < backup1.sql
 
4、Lighttpd 安裝及設定
步驟 1. 安裝 lighttpd、lighttpd-fastcgi 套件
因為 Lighttpd 使用 FastCGI 來處理 PHP 所以要一併安裝。# yum -y install lighttpd lighttpd-fastcgi步驟 2. 修改 lighttpd 設定檔
修改 lighttpd 設定檔來支援 PHP (FastCGI)。# vim /etc/lighttpd/lighttpd.conf
 server.modules = (
                   "mod_fastcgi",   //這行註解拿掉(支援 PHP-FastCGI)
                  )
#### fastcgi module
## read fastcgi.txt for more info
## for PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini
 fastcgi.server = ( ".php" =>
                    ( "localhost" =>
                     (
                      "socket" => "/var/run/lighttpd/php-fastcgi.socket", //預設值
                      "socket" => "/tmp/php-fastcgi.socket",              //修改後
                                   "bin-path" => "/usr/bin/php-cgi"
                      )
                     )
                   )步驟 3. 設定開機自動啟動 lighttpd 服務
使用 chkconfig 指令來查看 lighttpd 在各 runlevel 下狀態。# chkconfig --list |grep lighttpd
 lighttpd        0:off   1:off   2:off   3:off   4:off   5:off   6:off設定 lighttpd 在 runlevel 為 2、3、5 時會啟動服務。
# chkconfig --levels 235 lighttpd on     檢查剛才的設定是否生效。
# chkconfig --list |grep lighttpd
 lighttpd        0:off   1:off   2:on    3:on    4:off   5:on    6:off步驟 4. 啟動 lighttpd 服務
# /etc/rc.d/init.d/lighttpd start
 Starting lighttpd:                               [OK]  檢查一下 lighttpd process 是否執行。
# ps ax |grep lighttpd
 4266 ?        S      0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf檢查是否 LISTEN Port 80。
# netstat -tnl |grep :80
 tcp        0      0 0.0.0.0:80     0.0.0.0:*        LISTEN5、PHP 安裝及設定
步驟 1. 安裝 PHP 5
安裝 php 及 php-mysql 套件。# yum -y install php php-mysql步驟 2. 查尋安裝 PHP 是否成功
查看安裝的 PHP 版本。# php -v
 PHP 5.1.6 (cli) (built: Sep 20 2007 10:16:10)
 Copyright (c) 1997-2006 The PHP Group
 Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies查看目前 PHP 模組。
# php -m
 PHP Modules
 bz2
 ...略
 xml
 zlib
 Zend Modules步驟 3. 測試 Lighttpd 能否正確處理 php 檔案
隨意建立一個檔名為 test.php 的檔案,然後放到 lighttpd 的 web 目錄而 test.php 內容如下如果能看到 php 的預設頁就表示 lighttpd 能正確處理 php 檔案了。 <?
 phpinfo();
 ?> 6、參考
- 台灣PHP聯盟 - 在 RHEL 修改 mysql 放置資料的路徑
 - MySQL :: MySQL 5.1參考手冊
 - MySQL Bugs:#15818: error: No curses/termcap library found
 
7、Me FAQ
Q1. rpm 安裝 MySQL-5.0.77-0.src.rpm 失敗?
Error Message:利用 rpm 指令來手動安裝 MySQL-5.0.77-0.src.rpm 失敗,並出現如下錯誤訊息?
# rpm -ivh MySQL-5.0.77-0.src.rpm
   1:MySQL        warning: user mysqldev does not exist - using root
 warning: user mysqldev does not exist - using root
 warning: user mysqldev does not exist - using root
#################### 100%Ans:
代表目前用來安裝 MySQL-5.0.77-0.src.rpm 使用者為 root 機於安全性的考量,系統您建立一個用來安裝 mysql 的使用者帳號 (mysqldev),在新增 mysqldev 使用者之後便可順利執行 rpm 指令來安裝 MySQL-5.0.77-0.src.rpm。
# useradd mysqldev
# rpm -ivh MySQL-5.0.77-0.src.rpm
   1:MySQL      #################################### 100%Q2. configure: error: no acceptable C compiler found in $PATH?
Error Message:順利解開 MySQL-5.0.77-0.src.rpm 後執行 ./configure 指令來進行環境配置時失敗,並出現如下錯誤訊息?
# cd mysql-5.0.77
# ./configure
 checking build system type... i686-pc-linux-gnu
 checking host system type... i686-pc-linux-gnu
 checking target system type... i686-pc-linux-gnu
 checking for a BSD-compatible install... /usr/bin/install -c
 checking whether build environment is sane... yes
 checking for a thread-safe mkdir -p... /bin/mkdir -p
 checking for gawk... gawk
 checking whether make sets $(MAKE)... yes
 checking "character sets"... default: latin1, collation: latin1_swedish_ci; compiled in: latin1 latin1 utf8
 checking whether to compile national Unicode collations... yes
 checking whether build environment is sane... yes
 checking whether make sets $(MAKE)... (cached) yes
 checking for gawk... (cached) gawk
 checking for gcc... no
 checking for cc... no
 checking for cl.exe... no
 configure: error: in `/usr/src/redhat/SOURCES/mysql-5.0.77':
 configure: error: no acceptable C compiler found in $PATH  //系統沒有安裝 gcc 套件 (無法編譯)Ans:
原因在於目前系統並沒有安裝 gcc 套件所以進行環境設定 (./configure) 時並出現錯誤訊息,安裝好 gcc 套件後即可順利執行。
# yum -y install gcc-c++Q3. checking for termcap functions library... configure: error: No curses/termcap library found?
Error Message:順利解開 MySQL-5.0.77-0.src.rpm 後執行 ./configure 指令來進行環境配置時失敗,並出現如下錯誤訊息?
# cd mysql-5.0.77
# ./configure
 ...略...
 checking for termcap functions library... configure: error: No curses/termcap library foundAns:
請使用如下參數來配合 ./configure 指令即可
# ./configure --with-named-curses-libs=/usr/lib/libncursesw.so.5Q4. mysqladmin: connect to server at 'localhost' failed?
Error Message:剛安裝好 MySQL 並啟動 MySQL 服務成功之後,使用 mysqladmin 設定 MySQL 管理者 (root) 密碼時失敗,並出現如下錯誤訊息。且也無法使用 mysql -u root -p 來登入 MySQL?
# mysqladmin -u root password 123456
 mysqladmin: connect to server at 'localhost' failed
 error: 'Access denied for user 'root'@'localhost' (using password: NO)'Ans:
請依如下步驟來重新設定 MySQL 管理者密碼 (也適用於密碼忘記時,前提是擁有主機系統管理權限),完成如下步驟後即可使用 mysql -u root -p 來登入 MySQL。
停止 MySQL 服務。
# /etc/rc.d/init.d/mysql stop
 Shutting down MySQL..                             [OK]  執行指令 mysqld_safe 啟動 MySQL 服務於安全模式。
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
 [1] 28107執行 mysql 指令來登入 MySQL。
# mysql -u root mysql
 Reading table information for completion of table and column names
 You can turn off this feature to get a quicker startup with -A
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1
 Server version: 5.0.77-community MySQL Community Edition (GPL)
 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 mysql>執行 UPDATE 指令重新設定 MySQL 管理者密碼為 123456。
 mysql> UPDATE user SET Password=PASSWORD('123456') where USER='root';
 Query OK, 3 rows affected (0.00 sec)
 Rows matched: 3  Changed: 3  Warnings: 0執行 FLUSH 指令更新 MySQL 設定。
 mysql> FLUSH PRIVILEGES;
 Query OK, 0 rows affected (0.00 sec)執行 quit 指令離開 MySQL。
 mysql> quit
 Bye執行指令重新啟動 MySQL 服務。
# /etc/rc.d/init.d/mysql restart
 Shutting down MySQL..                 [OK] 
 Starting MySQL                        [OK]  Q5. ERROR 1064 (42000) at line 1: You have an error in your SQL syntax near?
Error Message:匯入 Dump Files 至指定的資料庫時失敗並出現如下錯誤訊息。
# mysql -u root -p databases_name < backup1.sql
 Enter password:
 ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '嚜?
 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"' at line 1使用 vi 來查看 backup1.sql 內容時為正常,若使用 less 指令來查看 backup1.sql 內容時則看到第一行有此字 嚜?
# less backup1.sql
 嚜?- phpMyAdmin SQL Dump
 -- version 2.11.3
 -- http://www.phpmyadmin.net
 --
 -- Host: localhost
 ...略...Ans:
主因 MySQL 資料庫編碼格式設定為 UTF-8 但匯入的 backup1.sql 檔案編碼格式不是 UTF-8 所導致的。請參考 資安論壇 • 檢視主題 - 求助 ~ FC3 MySQL 3.x匯入到F7 MySQL 5.x字元集問題。
Q6. 如何移除預設的 mysql-5.0.45-7.el5.i386?
Error Message:CentOS 5.2 安裝時有勾選 KDE 及 Server 選項,但當嘗試要安裝 MySQL-server-community-5.0.82-0.rhel5.i386.rpm 時出現如下錯誤訊息:
# rpm -ivh MySQL-server-community-5.0.82-0.rhel5.i386.rpm
 error: Failed dependencies:
        MySQL conflicts with mysql-5.0.45-7.el5.i386系統顯示與目前的 mysql-5.0.45-7.el5.i386 產生衝突,嘗試使用 rpm -e 指令來移除它但出現如下錯誤訊息 (相依套件關系)。
# rpm -e mysql-5.0.45-7.el5
 error: Failed dependencies:
        libmysqlclient.so.15 is needed by (installed) dovecot-1.0.7-2.el5.i386
        libmysqlclient.so.15(libmysqlclient_15) is needed by (installed) dovecot-1.0.7-2.el5.i386Ans:
由於有套件相依性的關系因此必須加上 --nodeps 參數來強制移除 mysql-5.0.45-7.el5,移除後即可順利安裝 MySQL-server-community-5.0.82-0.rhel5.i386.rpm。
# rpm -e mysql-5.0.45-7.el5 --nodeps
# rpm -ivh MySQL-server-community-5.0.82-0.rhel5.i386.rpm
 Preparing...               ############################ 100%
   1:MySQL-server-community ############################ 100%Q7. error: cannot create %sourcedir /usr/src/redhat/SOURCES?
Error Message:安裝 MySQL-5.0.82-0.src.rpm 套件時出現如下錯誤訊息?
# rpm -ivh MySQL-5.0.82-0.src.rpm
 error: cannot create %sourcedir /usr/src/redhat/SOURCESAns:
此訊息為系統找不到該資料夾 (/usr/src/redhat/SOURCES) 或資料夾權限錯誤所引起,此次狀況為系統不存在該資料夾,因此建立該資料夾後即可順利安裝 src rpm.。
# mkdir -p /usr/src/redhat/SOURCES
# rpm -ivh MySQL-5.0.82-0.src.rpm
   1:MySQL                 ########################## 100%下列為相關參考網址:
- rpm error: cannot create %sourcedir - LinuxQuestions.org
 - 再次求助高手指点关于error: cannot create %sourcedir /usr/src/redhat/SOURCES - Oracle技术专区
 - 酷!學園 - 安裝MailScanner-4.11-1及SOPHOS心得報告
 
Q8. phpinfo 無法顯示內容?
Error Message:安裝 httpd-2.2.15-5、php-5.3.2-6 版本,test.php 內容如下但無法顯示 phpinfo 頁面內容?
 <?
 phpinfo();
 ?>Ans:
將 test.php 內容由原本的 <? 修改為 <?php 後即可顯示網頁。
 <?php
 phpinfo();
 ?>Q9. 想要安裝指定的 MySQL 版本怎麼辦?
Ans:因為採用 CentOS 5.6 的 YUM 所安裝的 MySQL 版本為 5.0.x,但需求是要安裝 MySQL 5.1.44 版本才行,那麼該如何達成? 最簡單的方法是直接去找 RPM 來進行安裝即可。
- 由於 MySQL 官網找不到舊版,此例是去 Download SkySQL Archive Service: MySQL Server 5.1.44 找到的 RPM。
 - 請下載三個 RPM 分別是 MySQL-server-community-5.1.44-1.rhel5.x86_64.rpm、MySQL-client-community-5.1.44-1.rhel5.x86_64.rpm、MySQL-devel-community-5.1.44-1.rhel5.x86_64.rpm。
 - 執行指令進行 RPM 的安裝「rpm -ivh MySQL-server-community-5.1.44-1.rhel5.x86_64.rpm MySQL-client-community-5.1.44-1.rhel5.x86_64.rpm MySQL-devel-community-5.1.44-1.rhel5.x86_64.rpm」。
 - 安裝完畢後檢查是否可進入 MySQL 資料庫、版本是否正確、設定 MySQL 資料庫管理密碼。
 
# mysql                    //進入 MySQL 資料庫 (預設沒有密碼)
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 5
  Server version: 5.1.44-community MySQL Community Server (GPL)
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  mysql> exit             //離開 MySQL 資料庫
# mysqladmin -u root password 'weithenn168'  //設定 MySQL 資料庫管理密碼為 weithenn168
# mysql -u root -p         //再次登入 MySQL 資料庫
  Enter password:         //輸入 MySQL 資料庫管理密碼
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 5
  Server version: 5.1.44-community MySQL Community Server (GPL)
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  mysql> exit             //離開 MySQL 資料庫
  Bye
