安裝 LAMP – 在 CentOS 6.3 裡安裝 Apache、Mysql、Php

安裝 LAMP – 在 CentOS 6.3 裡安裝 Apache、Mysql、Php

 

全文引自:http://blog.xuite.net/tolarku/blog/65424317-%E5%AE%89%E8%A3%9D+LAMP+-+%E5%9C%A8+CentOS+6.3+%E8%A3%A1%E5%AE%89%E8%A3%9D+Apache%E3%80%81Mysql%E3%80%81Php

LAMP = Linux + Apache + Mysql + PhP。說明安裝這三個網頁項目,早就讓掉落的牙齒變成化石了。如果你也是這麼想,說不定你的部分記憶跟我一樣,停留在自己逐步的修改 conf 檔,使 apache php 能合作處理網頁的查詢。今天要說的事簡單利用 yum 等套件管理程式來安裝所需的環境,所以這篇純粹是做紀錄用的,就是讓我下次可以直接複製、貼上、執行就搞定的紀錄。

一般來說安裝 網頁伺服器 httpd (apache) ,你只需要 yum install httpd -y 就搞定了,但為了能搭配 MySQL PHP 你還得多安裝一些套件。目前 20130107 yum 可以安裝的各版本為

 

軟體套件

目前版本

說明

httpd

Apache 2.2.15 15.el6.centos.1

The Apache HTTP Server is a powerful, efficient, and extensible web server.

PHP

5.3.3 14.el6_3

http://www.php.net/ PHP is an HTML-embedded scripting language

MySQL

5.1.66 2.el6_3

MySQL is a multi-user, multi-threaded SQL database server. MySQL is a client/server implementation consisting of a server daemon (mysqld) and many different client programs and libraries.

mysql-server

5.1.66 2.el6_3

The MySQL server and related files

mysql-libs

5.1.66 2.el6_3

The shared libraries required for MySQL clients

 

第一步安裝我們就來安裝上說明的項目

yum  -y  install  httpd  php  php-mysql  mysql  mysql-server

 

批哩啪啦的亂跑一堆,正常安裝話最後就會出現

Installed:

  mysql.x86_64 0:5.1.66-2.el6_3           mysql-server.x86_64 0:5.1.66-2.el6_3

  php.x86_64 0:5.3.3-14.el6_3             php-mysql.x86_64 0:5.3.3-14.el6_3

Dependency Installed:

  perl-DBD-MySQL.x86_64 0:4.013-3.el6         perl-DBI.x86_64 0:1.609-4.el6

  php-cli.x86_64 0:5.3.3-14.el6_3             php-common.x86_64 0:5.3.3-14.el6_3

  php-pdo.x86_64 0:5.3.3-14.el6_3

Complete!

 

表示已經都安裝好了!!!

 

接下來你有幾個步驟你需要做

啟動服務

設定防火牆

測試 php apache 的環境

進一步設定 MySQL

 

★啟動服務

到上面的步驟你已經安裝好網頁伺服器,但是你還未執行他,也還沒將他設成預設啟動(意即:開機後會自動執行 apache

>>> 啟動 MySQL

1. sudo chkconfig mysqld on          //設定 MySQL 開機預設啟動,當然你也可以更進一步設定哪些 rc level 才執行他,例如「 chkconfig –levels 235 mysqld on  

2. sudo /etc/init.d/mysqld start      // 執行 mysqld ,第一次會「正在初始化 MySQL 資料庫……..」,且記得是 mysql + d 唷!

 

>>> 啟動 Apache

1. sudo  chkconfig httpd on

2. sudo /etc/init.d/httpd start

註:如果你跟我一樣還沒設定網域名稱,你可能會得到這提示訊息「正在啟動 httpdhttpd: apr_sockaddr_info_get() failed for hostname httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName

 

sudo vi /etc/httpd/conf/httpd.conf  然後尋找字串,增加一行
ServerName your.domain.com.tw:port
就不會有這個錯誤訊息了。 

 

★設定防火牆

這個部分就看你這個 web 的開放對象是誰?如果只允許特定IP,那你就可以增加設定

-A Fw-rules-INPUT -s 140.112.1.0/24 -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

-A Fw-rules-INPUT -s 140.112.1.0/24 -m state –state NEW -m tcp -p tcp –dport 443 -j ACCEPT 

針對 140.112.1.* 開放 port 80 443 SSL port

1. 修改sudo vi  /etc/sysconfig/iptables 內容

2. 重新啟動 FW   sudo /etc/init.d/iptables restart

3. 觀察執行設定  sudo /sbin/iptables -L -n

 

 

★測試 php apache 的環境

觀察你的 apache web server 是否正確執行「http://your_ip_address

觀察你的 php 程式碼是否正確解析執行「http://your_ip_address/phpinfo.php

 

★進一步設定 MySQL

設定 MySQL 資料庫管理者 root 帳戶的密碼,執行「sudo mysql_secure_installation」,他會問你底下幾件事

1. Enter current password for root (enter for none):  // 直接按 Enter,因為預設沒密碼

2. Set root password? [Y/n]                     //輸入  Y 來設定密碼

3. New password:                                  // 輸入 root 新密碼

4. Re-enter new password:                      // 再一次輸入 root 新密碼

5. Remove anonymous users? [Y/n]          //預設 Yes ,直接按 Enter

6. Disallow root login remotely? [Y/n]       //預設 Yes ,直接按 Enter

7. Remove test database and access to it? [Y/n]    //預設 Yes ,直接按 Enter

8. Reload privilege tables now? [Y/n]       //預設 Yes ,直接按 Enter

 

接著利用剛剛設定的 root 密碼登入 MySQL,執行「sudo mysql -u root -p」,密碼正確就會進入「mysql>」這樣的提示符號。「參考

 執行選擇要用那個資料庫

mysql> use mysql

 

 執行「select host,user from user ;」要有分號結尾才會執行。

mysql> select host,user from user ;

+———–+——+

| host      | user |

+———–+——+

| 127.0.0.1 | root |

| localhost | root |

+———–+——+

2 rows in set (0.00 sec)

 

 新增一個使用者

insert into user (host,user,password) values (‘%’,’your_account’,password(‘your_password’));

 

※授與該使用者管理者權限,這樣你才能有辦法遠端處理 (因為 root 的遠端登入被停掉了) 

GRANT ALL ON *.* TO ‘your_account’@140.112.1.1 IDENTIFIED BY ‘your_password’ WITH GRANT OPTION;

不然剛剛那個帳號,只會是一般資料庫使用者的帳號,而且沒任何權限。

※讓剛剛的設定生效

FLUSH PRIVILEGES;

 

——-

若只是要單純的授與某位使用者為某個資料庫的管理者(非整個 MySQL 管理者),你可以使用

sudo mysql -u root -p
mysql> use mysql;
mysql> create database your_db_name;
mysql> insert into user (host,user,password) values (‘%’,’your_account’,password(‘your_password’));
mysql> grant usage on *.* to your_account@localhost identified by ‘your_password’;
mysql> grant all privileges on your_db_name.* to your_account@localhost ;
mysql> FLUSH PRIVILEGES;

—-

接著遠端測試連接你的 MySQL ….

啥?連不到….你的 Firewall 有開嗎? Port: 3306

 

正確連上後你就可以看到兩個基本的資料表 mysql information_schema

——————————————————————-

以上的步驟就完成了 apache + php + mysql 的安裝了,接下來就是思考你的 PHP 網頁環境是要執行那個套件或者需要安裝哪些額外的 php 函示庫呢?這時你可以參考下列的說明。

 

安裝基本需要的套件

yum -y install  php-mbstring php-mcrypt php-gd php-xml

底下PHP 相關套件是 for x86_64 5.3.3 14.el6_3 所舉例說明的,不代表全部。

PHP相關套件

建議安裝

說明

php-bcmath

A module for PHP applications for using the bcmathlibrary

php-cli

Command-line interface CLI for PHP

php-common

Common files for PHP

php-dba

A database abstraction layer module for PHP applications

php-devel

Files needed for building PHP extensions

php-embedded

PHP library for embedding in applications

php-enchant

Human Language and Character Encoding Support

php-gd

Y

A module for PHP applications for using the gd graphics library

php-imap

A module for PHP applications that use IMAP

php-intl

Internationalization extension for PHP applications

php-ldap

A module for PHP applications that use LDAP

php-mbstring

Y

A module for PHP applications which need multi-bytestring

php-mysql

Y

A module for PHP applications that use MySQLdatabases

php-odbc

A module for PHP applications that use ODBC databases

php-pdo

A database access abstraction module for PHP applications

php-pear

PHP Extension and Application Repository framework

php-pecl-apc

APC caches and optimizes PHP intermediate code

php-pecl-apc-devel

APC developer files (header)

php-pecl-memcache

Extension to work with the Memcached caching daemon

php-pgsql

A PostgreSQL database module for PHP

php-process

Modules for PHP script using system process interfaces

php-pspell

A module for PHP applications for using pspell interfaces

php-recode

A module for PHP applications for using the recode library

php-snmp

A module for PHP applications that query SNMP-managed devices

php-soap

A module for PHP applications that use the SOAPprotocol

php-tidy

Standard PHP module provides tidy library support

php-xml

Y

A module for PHP applications which use XML

php-xmlrpc

A module for PHP applications which use the XML-RPCprotocol

php-zts

Thread-safe PHP interpreter for use with the Apache HTTP Server

當然 MySQL 的部分你也可能要做其他運用,例如搭配 rsyslog 而需要再安裝「rsyslog-mysql」套件MySQL support for rsyslog.  http://www.rsyslog.com/ 

~ Apache 也是有額外安裝的模組,例如你要用 https SSL 時候,你就必須安裝 mod_ssl

yum  -y  install  mod_ssl  mod_perl  mod_auth_mysql

你也可以再安裝 phpmyadmin 來管理你的 MySQL ,只是這樣被 try 的機率很大。

~ End