2014年7月30日 星期三

Centos6.5 中安裝 MariaDB 5.5 Galera Server (叢集版本)

下午3:35:00 Posted by Amos , , No comments

簡介

本文概述簡單的步驟,教導如何建置 Master-Master Replication 叢集(Galera Cluster)架構的MariaDB(請參考下圖),並安裝phpmyadmin管理MariaDB資 料庫。

Galera Cluster 介紹

為什麼要選擇 Galera Cluster Server,它有什麼優點及功能呢?MySQL/Galera 是一套可以同步多台 MySQL/InnoDB 機器的叢集系統,底下可以列出功能。
  • 同步複製資料
  • 可讀取和寫入叢集系統內任一節點
  • 自動偵測節點錯誤,如果有節點當機,則叢集系統自動移除該節點
  • 可任意擴充節點
  • 採用 row level 方式來平行複製資料
從上面功能看來,我們可以平行任意擴充節點,動態增加伺服器到叢集系統,要做到上面功能,就是利用 Galera library 來做到同步資料處理,同步的詳細細節,可以參考 Galera library 連結。這邊就不再多描述了。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

MariaDB資料庫版本 : 5.5.38 (Galera-Server 64位元)

PHP版本 : 5.4.X (REMI版本)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,>
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

設定Open File個數

編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4567 -j ACCEPT  使用於Galera
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4444 -j ACCEPT  使用於同步資料
-A INPUT -m state --state NEW -m udp -p udp --dport 4444 -j ACCEPT  使用於同步資料
/etc/init.d/iptables restart

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

啟用Apache時若有出現Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf
ServerName  localhost:80    #新增加這一項參數

重新啟動Apache Server。
/etc/init.d/httpd restart

設定php.ini檔案

編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini 
修改為: max_execution_time = 300 修改為: max_input_time = 600 修改為: post_max_size = 800M 修改為: upload_max_filesize = 800M 修改為: default_charset="utf8" 修改為: default_socket_timeout = 300 修改為: short_open_tag = On 修改為: date.timezone = "Asia/Taipei"

安裝及設定MariaDB資料庫

安裝 EPEL 套件

因安裝MariaDB-Galera需要socat套件,故需要先安裝EPEL套件來源檔,請參考以下步驟進行設定。
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm

設定MariaDB的來源碼

編輯 /etc/yum.repos.d/mariadb.repo檔案,若是32位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

若為64位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安裝MariaDB

安裝MariaDB相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install MariaDB-Galera-server MariaDB-client galera
service mysql start

駐: 安裝後啟動MariaDB,MariaDB的服務稱為mysql,與MySQL服務名稱(mysqld)不同

設定MariaDB自動啟動

chkconfig --level 345 mysql on
chkconfig --list mysql

MariaDB 設定檔的位置位於/etc/my.cnf.d

my.cnf.d目錄中有兩個檔案,mysql-clients.cnf和server.cnf檔案,可將設定檔之範例檔案拷貝至my.cnf.d/目錄中,範例檔位於 /usr/share/mysql/ 目錄中,可根據主機的記憶體規格選擇合適的設定檔
  • my-innodb-heavy-4G.cnf: 適合大於 4GB RAM的伺服器使用。
  • my-huge.cnf: 適合 1GB – 2GB RAM的伺服器使用。
  • my-large.cnf: 適合 512MB RAM的伺服器使用。
  • my-medium.cnf: 適合64MB~512 RAM 的伺服器使用。
  • my-small.cnf: 適合記憶體小於 64MB的伺服器。

拷貝範例設定檔至/etc/my.cnf.d/目錄中,命名為mysql-clients.cnf
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/mysql-clients.cnf
service mysql restart

設定MariaDB root帳號之密碼

/usr/bin/mysqladmin -u root password '新密碼'
重新啟動 MariaDB Server
/etc/init.d/mysql restart

啟動MariaDB並配置Galera同步配置

建立 cluster 使用者,密碼為p@ssw0rd,針對 % 跟 localhost 同時建立,帳號密碼配置(每一台資料庫中都建立同步用之帳號)
GRANT USAGE ON *.* to cluster@'%' IDENTIFIED BY 'p@ssw0rd';
GRANT ALL PRIVILEGES on *.* to cluster@'%';
GRANT USAGE ON *.* to cluster@'localhost' IDENTIFIED BY 'p@ssw0rd';
GRANT ALL PRIVILEGES on *.* to cluster@'localhost';
FLUSH PRIVILEGES;

建立Galera設定檔 wsrep.cnf

cp /usr/share/mysql/wsrep.cnf /etc/my.cnf.d/
於第一台主機(192.168.1.100)中編輯 /etc/my.cnf.d/wsrep.cnf 檔案,輸入以下設定
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_sst_auth=cluster:p@ssw0rd

# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://第二台主機IP"

# Galera Node Configuration
wsrep_node_address="192.168.1.100"
wsrep_node_name="this_node_name"

重新啟動 mysql 後並執行 netstat -na 看到多一個 listen 4567 port
service mysql start --wsrep-new-cluster
netstat -na
LISTEN      
tcp        0      0 0.0.0.0:4567                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:3306                0.0.0.0:*   
於第二台主機(192.168.1.101)中編輯 /etc/my.cnf.d/wsrep.cnf 檔案,將 cluster address 設定為第一台主機的IP,輸入以下設定
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_sst_auth=cluster:p@ssw0rd

# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://第一台主機IP"

# Galera Node Configuration
wsrep_node_address="192.168.1.101"
wsrep_node_name="this_node_name"
重新啟動 mysql 後並執行 netstat -na 看到多一個 listen 4567 port
/etc/init.d/mysql restart

於第一台和第二台主機中執行以下指令,確認Cluster設定完成並啟用

$ mysql -e "SHOW STATUS LIKE 'wsrep%';"

可看到如下結果
+------------------------------+--------------------------------------+
| Variable_name                | Value                                |
+------------------------------+--------------------------------------+
| wsrep_local_state_uuid       | b5fc5a4a-1782-11e4-8689-b3cbc5faf010 |
| wsrep_protocol_version       | 5                                    |
| wsrep_last_committed         | 0                                    |
| wsrep_replicated             | 0                                    |
| wsrep_replicated_bytes       | 0                                    |
| wsrep_repl_keys              | 0                                    |
| wsrep_repl_keys_bytes        | 0                                    |
| wsrep_repl_data_bytes        | 0                                    |
| wsrep_repl_other_bytes       | 0                                    |
| wsrep_received               | 2                                    |
| wsrep_received_bytes         | 216                                  |
| wsrep_local_commits          | 0                                    |
| wsrep_local_cert_failures    | 0                                    |
| wsrep_local_replays          | 0                                    |
| wsrep_local_send_queue       | 0                                    |
| wsrep_local_send_queue_avg   | 0.000000                             |
| wsrep_local_recv_queue       | 0                                    |
| wsrep_local_recv_queue_avg   | 0.000000                             |
| wsrep_local_cached_downto    | 18446744073709551615                 |
| wsrep_flow_control_paused_ns | 0                                    |
| wsrep_flow_control_paused    | 0.000000                             |
| wsrep_flow_control_sent      | 0                                    |
| wsrep_flow_control_recv      | 0                                    |
| wsrep_cert_deps_distance     | 0.000000                             |
| wsrep_apply_oooe             | 0.000000                             |
| wsrep_apply_oool             | 0.000000                             |
| wsrep_apply_window           | 0.000000                             |
| wsrep_commit_oooe            | 0.000000                             |
| wsrep_commit_oool            | 0.000000                             |
| wsrep_commit_window          | 0.000000                             |
| wsrep_local_state            | 4                                    |
| wsrep_local_state_comment    | Synced                               |
| wsrep_cert_index_size        | 0                                    |
| wsrep_causal_reads           | 0                                    |
| wsrep_cert_interval          | 0.000000                             |
| wsrep_incoming_addresses     | 210.71.253.55:3306,210.61.8.40:3306  |
| wsrep_cluster_conf_id        | 4                                    |
| wsrep_cluster_size           | 2                                    |
| wsrep_cluster_state_uuid     | b5fc5a4a-1782-11e4-8689-b3cbc5faf010 |
| wsrep_cluster_status         | Primary                              |
| wsrep_connected              | ON                                   |
| wsrep_local_bf_aborts        | 0                                    |
| wsrep_local_index            | 0                                    |
| wsrep_provider_name          | Galera                               |
| wsrep_provider_vendor        | Codership Oy     |
| wsrep_provider_version       | 25.3.5(rXXXX)                        |
| wsrep_ready                  | ON                                   |
| wsrep_thread_count           | 2                                    |
+------------------------------+--------------------------------------+

測試 Master-Master Replication 架構

登入其中一台主機,建立新的資料庫(playground)與表格(equipment)
mysql -u root -p
CREATE DATABASE playground;
use playground;
CREATE TABLE equipment ( id INT NOT NULL AUTO_INCREMENT, type VARCHAR(50), quant INT, color VARCHAR(25), PRIMARY KEY(id));
輸入資料至equipment表格中
INSERT INTO equipment (type, quant, color) VALUES ("slide", 2, "blue");
再登入第二台主機查看是否有新增playground資料庫及equipment資料表,並新增一筆資料至equipment資料表
use playground;
SELECT * FROM playground.equipment;
INSERT INTO equipment (type, quant, color) VALUES ("swing", 10, "yellow");
再登入至第一台主機查看是否equipment資料表有新增一筆資料
use playground;
SELECT * FROM playground.equipment;

增加新的Node至MariaDB Cluster

可任意新增多台 Node 到 Cluster 叢集系統裡,設置過程如下
  • 安裝 MariaDB Server
  • 安裝 Galera Library
  • 設定wsrep.cnf檔案內容

動態設定 gcomm://參數

若新增一台新的 Node,就必須修改其它 Node 的 gcomm:// 設定,並且重新啟動 MariaDB 服務,這樣會有DownTime時間影響服務,故可透過線上修改 GLOBAL wsrep_cluster_address 的參數值達到不須重新啟動服務,請參考如下設定
mysql -e "SHOW VARIABLES LIKE 'wsrep_cluster_address';"
mysql -u root -p
SET GLOBAL wsrep_cluster_address='gcomm://設定IP';

參考資料

安裝及設定phpMyAdmin

下載phpMyAdmin-4.0.10.1-all-languages.tar.gz並安裝

解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz

搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1

複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php

修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart

修改config.inc.php檔案的權限
chmod 644 config.inc.php

移除setup安裝資料夾
mv setup setup.org

設定phpMyAdmin網站別名(可視情況設定)

編輯 /etc/httpd/conf/httpd.conf 檔案

#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
  Options -Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫

Troubleshooting

假設您有遇到 Native table 'performance_schema'.'???' has the wrong structure 等的錯誤訊息LOG (位於/var/lib/mysql/) 時, 表示資料表因為資料庫升版造成異常,可使用以下的指令進行修復
mysql_upgrade -u root -p

2014年7月28日 星期一

Centos6.5 中安裝MariaDB 10.0.12 資料庫與phpMyAdmin軟體

下午1:50:00 Posted by Amos , , No comments

簡介

本文概述簡單的步驟,教導如何建置LAMP環境(Apache、PHP、MariaDB),並安裝phpmyadmin管理MariaDB資料庫。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

MariaDB資料庫版本 : 10.0.12 (64位元)

PHP版本 : 5.4.X (REMI版本)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,>
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

設定Open File個數

編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
/etc/init.d/iptables restart 

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

啟用Apache時若有出現Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf
ServerName  localhost:80    #新增加這一項參數

安裝及設定php

安裝php套件

安裝PHP相關套件(套件來源使用remi與EPEL),並設定於開機時自動啟用,請參考以下步驟進行設定。
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

yum install --enablerepo=remi php
yum install --enablerepo=remi php-mysql
yum install --enablerepo=remi php-mbstring
yum install --enablerepo=remi php-mcrypt

重新啟動Apache Server。
/etc/init.d/httpd restart

設定php.ini檔案

編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini 
修改為: max_execution_time = 300 修改為: max_input_time = 600 修改為: post_max_size = 800M 修改為: upload_max_filesize = 800M 修改為: default_charset="utf8" 修改為: default_socket_timeout = 300 修改為: short_open_tag = On 修改為: date.timezone = "Asia/Taipei"

安裝及設定MariaDB資料庫

設定MariaDB的來源碼

編輯 /etc/yum.repos.d/mariadb.repo檔案,若是32位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

若為64位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安裝MariaDB

安裝MariaDB相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install MariaDB-devel MariaDB-client MariaDB-server  MariaDB-common MariaDB-shared MariaDB-devel MariaDB-compat

service mysql start

駐: 安裝後啟動MariaDB,MariaDB的服務稱為mysql,與MySQL服務名稱(mysqld)不同

設定MariaDB自動啟動

chkconfig --level 345 mysql on
chkconfig --list mysql

MariaDB 設定檔的位置位於/etc/my.cnf.d

my.cnf.d目錄中有兩個檔案,mysql-clients.cnf和server.cnf檔案,可將設定檔之範例檔案拷貝至my.cnf.d/目錄中,範例檔位於 /usr/share/mysql/ 目錄中,可根據主機的記憶體規格選擇合適的設定檔
  • my-innodb-heavy-4G.cnf: 適合大於 4GB RAM的伺服器使用。
  • my-huge.cnf: 適合 1GB – 2GB RAM的伺服器使用。
  • my-large.cnf: 適合 512MB RAM的伺服器使用。
  • my-medium.cnf: 適合64MB~512 RAM 的伺服器使用。
  • my-small.cnf: 適合記憶體小於 64MB的伺服器。

拷貝範例設定檔至/etc/my.cnf.d/目錄中,命名為mysql-clients.cnf
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/mysql-clients.cnf
service mysql restart

設定MariaDB root帳號之密碼

/usr/bin/mysqladmin -u root password '新密碼'

修改/etc/my.cnf.d/mysql-clients.cnf檔案,設定資料庫預設的字元碼為UTF8

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

重新啟動 MariaDB Server
/etc/init.d/mysql restart

安裝及設定phpMyAdmin

下載phpMyAdmin-4.0.10.1-all-languages.tar.gz並安裝

解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz

搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1

複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php

修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart

修改config.inc.php檔案的權限
chmod 644 config.inc.php

移除setup安裝資料夾
mv setup setup.org

設定phpMyAdmin網站別名(可視情況設定)

編輯 /etc/httpd/conf/httpd.conf 檔案

#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
  Options -Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫

Troubleshooting

假設您有遇到 Native table 'performance_schema'.'???' has the wrong structure 等的錯誤訊息LOG (位於/var/lib/mysql/) 時, 表示資料表因為資料庫升版造成異常,可使用以下的指令進行修復
mysql_upgrade -u root -p

MariaDB 參考資料

2014年7月27日 星期日

Centos6.5 中安裝MariaDB 5.5 資料庫與phpMyAdmin軟體

上午11:54:00 Posted by Amos , , No comments

簡介

本文概述簡單的步驟,教導如何建置LAMP環境(Apache、PHP、MariaDB),並安裝phpmyadmin管理MariaDB資料庫。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

MariaDB資料庫版本 : 5.5.38 (64位元)

PHP版本 : 5.4.X (REMI版本)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,>
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

設定Open File個數

編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
/etc/init.d/iptables restart 

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

啟用Apache時若有出現Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf
ServerName  localhost:80    #新增加這一項參數

安裝及設定php

安裝php套件

安裝PHP相關套件(套件來源使用remi與EPEL),並設定於開機時自動啟用,請參考以下步驟進行設定。
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

yum install --enablerepo=remi php
yum install --enablerepo=remi php-mysql
yum install --enablerepo=remi php-mbstring
yum install --enablerepo=remi php-mcrypt

重新啟動Apache Server。
/etc/init.d/httpd restart

設定php.ini檔案

編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini 
修改為: max_execution_time = 300 修改為: max_input_time = 600 修改為: post_max_size = 800M 修改為: upload_max_filesize = 800M 修改為: default_charset="utf8" 修改為: default_socket_timeout = 300 修改為: short_open_tag = On 修改為: date.timezone = "Asia/Taipei"

安裝及設定MariaDB資料庫

設定MariaDB的來源碼

編輯 /etc/yum.repos.d/mariadb.repo檔案,若是32位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

若為64位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安裝MariaDB

安裝MariaDB相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install MariaDB-devel MariaDB-client MariaDB-server  MariaDB-common MariaDB-shared MariaDB-devel

service mysql start

駐: 安裝後啟動MariaDB,MariaDB的服務稱為mysql,與MySQL服務名稱(mysqld)不同

設定MariaDB自動啟動

chkconfig --level 345 mysql on
chkconfig --list mysql

MariaDB 設定檔的位置位於/etc/my.cnf.d

my.cnf.d目錄中有兩個檔案,mysql-clients.cnf和server.cnf檔案,可將設定檔之範例檔案拷貝至my.cnf.d/目錄中,範例檔位於 /usr/share/mysql/ 目錄中,可根據主機的記憶體規格選擇合適的設定檔
  • my-innodb-heavy-4G.cnf: 適合大於 4GB RAM的伺服器使用。
  • my-huge.cnf: 適合 1GB – 2GB RAM的伺服器使用。
  • my-large.cnf: 適合 512MB RAM的伺服器使用。
  • my-medium.cnf: 適合64MB~512 RAM 的伺服器使用。
  • my-small.cnf: 適合記憶體小於 64MB的伺服器。

拷貝範例設定檔至/etc/my.cnf.d/目錄中,命名為mysql-clients.cnf
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/mysql-clients.cnf
service mysql restart

修改/etc/my.cnf.d/mysql-clients.cnf檔案,設定資料庫預設的字元碼為UTF8

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

重新啟動 MariaDB Server
/etc/init.d/mysql restart

設定MariaDB root帳號之密碼

/usr/bin/mysqladmin -u root password '新密碼'

安裝及設定phpMyAdmin

下載phpMyAdmin-4.0.10.1-all-languages.tar.gz並安裝

解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz

搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1

複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php

修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart

修改config.inc.php檔案的權限
chmod 644 config.inc.php

移除setup安裝資料夾
mv setup setup.org

設定phpMyAdmin網站別名(可視情況設定)

編輯 /etc/httpd/conf/httpd.conf 檔案

#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
  Options -Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫

2014年7月22日 星期二

MySQL、MariaDB 資料庫備份、還原備份指令

下午3:11:00 Posted by Amos , No comments

簡介

MySQL以前是熱門的開源資料庫,但因被知名大廠ORACLE收購後,MySQL需要支付顧問費用才可使用,因此慢慢出現和MySQL類似的開源資料庫-MariaDB,Google也將數千台的MySQL資料庫伺服器更改為MariaDB資料庫,MariaDB取代MySQL非常簡單,只需要備份之前的資料庫,安裝MariaDB後恢復備份即可,管理指令及操作方式和MySQL幾乎相同,本教學將教導如何將MySQL資料庫中的資料匯入至MariaDB之指令

資料庫備份指令

MySQL資料庫備份: 備份phpbb資料庫,匯出檔案名稱為mysql_backup.sql

mysqldump -uroot -p --default-character-set=utf8 phpbb > mysql_backup.sql

MariaDB資料庫備份: 備份phpbb資料庫,匯出檔案名稱為mariadb_backup.sql

mysqldump -uroot -p --default-character-set=utf8 phpbb > mariadb_backup.sql

資料庫還原指令

MySQL資料庫還原: 還原phpbb資料庫,還原檔案名稱為mysql_backup.sql

mysql -uroot -p phpbb < mysql_backup.sql

MariaDB資料庫還原: 還原phpbb資料庫,還原檔案名稱為mariadb_backup.sql

mysql --uroot -p phpbb < mariadb_backup.sql

如何安裝 EPEL Repository for RHEL/CentOS 7.x/6.x/5.x

下午2:57:00 Posted by Amos No comments

What is EPEL

EPEL (Extra Packages for Enterprise Linux) is open source and free community based repository project from Fedora team which provides 100% high quality add-on software packages for Linux distribution including RHEL (Red Hat Enterprise Linux), CentOS, and Scientific Linux. Epel project is not a part of RHEL/Cent OS but it is designed for major Linux distributions by providing lots of open source packages like networking, sys admin, programming, monitoring and so on. Most of the epel packages are maintained by Fedora repo.

Why we use EPEL repository?

  1. Provides lots of open source packages to install via Yum.
  2. Epel repo is 100% open source and free to use.
  3. It does not provide any core duplicate packages and no compatibility issues.
  4. All epel packages are maintained by Fedora repo.

How To Enable EPEL Repository in RHEL/CentOS 7/6/5?

RHEL/CentOS 7 64 Bit

## RHEL/CentOS 7 64-Bit ##
wget http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm
rpm -ivh epel-release-7-0.2.noarch.rpm

RHEL/CentOS 6 32-64 Bit

## RHEL/CentOS 6 32-Bit ##
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm

## RHEL/CentOS 6 64-Bit ##
wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm

RHEL/CentOS 5 32-64 Bit

## RHEL/CentOS 5 32-Bit ##
wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -ivh epel-release-5-4.noarch.rpm

## RHEL/CentOS 5 64-Bit ##
wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
rpm -ivh epel-release-5-4.noarch.rpm

How Do I Verify EPEL Repo?

yum repolist

How Do I Use EPEL Repo?

Checking the software package exist

yum --enablerepo=epel info mysql5.5

Install mysql5.5 package using EPEL repo

# yum --enablerepo=epel install 

2014年7月16日 星期三

使用 Composer 管理PHP套件及安裝Laravel Framework

晚上8:21:00 Posted by Amos , , No comments

簡介

本文概述簡單的步驟,教導如何安裝PHP Framework: Laravel,進行開發。
官方網站: Composer - Dependency Manager for PHP
Composer 是一個 Phar 的執行程式(Phar 會將程式打包成一隻執行檔), 可以安裝於自己目錄或Global環境,在此介紹一下安裝方式及簡單的用法
註: Amazon AWS官方PHP套件使用 Composer 管理函式厙

環境介紹及前置作業

作業系統版本 : Centos 6.4 (64位元)

PHP版本 : PHP 5.4.30 (cli)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟 yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。 # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟防火牆,設定完成後請重新啟動防火牆。 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables restart

安裝EPEL , REMI套件

RHEL/CentOS 7 64 Bit

## RHEL/CentOS 7 64-Bit ## wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-2.noarch.rpm rpm -ivh epel-release-7-2.noarch.rpm wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm rpm -Uvh remi-release-7*.rpm

RHEL/CentOS 6 32-64 Bit

## RHEL/CentOS 6 32-Bit ## wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm ## RHEL/CentOS 6 64-Bit ## wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm rpm -Uvh remi-release-6*.rpm

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。 yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。 vi /etc/httpd/conf.d/welcome.conf

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

安裝及設定php

安裝php套件

安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。 yum --enablerepo=remi install php php-mysql
yum --enablerepo=remi install php-mcrypt
yum --enablerepo=remi install php-json

設定php.ini檔案

vi /etc/php.ini
修改為: post_max_size = 800M
修改為: upload_max_filesize = 800M
修改為: default_charset="utf8"
修改為: default_socket_timeout = 300
修改為: max_execution_time = 1200
修改為: memory_limit = 1024M

重新啟動Apache Server。 /etc/init.d/httpd restart

Composer安裝

Composer管理套件安裝方式請參考如下步驟
#產生 composer.phar, 產生完就可以執行操作
curl -k -sS https://getcomposer.org/installer | php
#搬到全域環境並改名成 composer 方便操作
mv composer.phar /usr/local/bin/composer
補充說明:
1. 註: 安裝於全域環境(Globally),升級就會需要 composer self-update
2. 套件升級: composer update

安裝 Laravel 套件

下載 Laravel 套件及安裝

下載Laravel PHAR套件,將laravel.phar檔案更改名稱為larave,並放置於/usr/local/bin wget http://laravel.com/laravel.phar
mv laravel.phar /usr/local/bin/laravel
chmod 755 /usr/local/bin/laravel

建立 Laravel Web Applicaiton

cd /var/www/html
laravel new laravel_helloworld

建立完成後會顯示如下的訊息,將會於/var/www/html中建立laravel_helloworld資料夾 Crafting application...
Application ready! Build something amazing.

laravel_helloworld專案的結構如下
app/ 應用程式的目錄,大部份的程式碼都會被放置此目錄底下
  • app/config/ 控制應用程式相關參數的目錄
  • app/config/app.php 設定 Debug,URL,Timezone,Locale,KeyAutoloaded Service Providers 的檔案
  • app/storage/ 紀錄暫存檔的目錄,Laravel 需要擁有此目錄的寫入權限以寫入暫存記錄。
  • app/controllers/ 放置 Controller 的資料夾
  • app/views/ 放置 View 的資料夾
  • app/routes.php 控制應用程式相關 Route 的檔案
  • app/filters.php 控制應用程式相關 Route Filter 的檔案
bootstrap/ 控制應用程式啟動的目錄
  • bootstrap/paths.php 設定基本資料夾路徑的檔案 (EX: app, public...etc)
public Web 應用程式的入口

修改 Laravel 資料夾權限

chgrp -R apache /var/www/html/laravel_helloworld/app/storage
chmod -R 775 /var/www/html/laravel_helloworld/app/storage

設定網站之虛擬目錄

編輯/etc/httpd/conf/httpd.conf檔案,設定虛擬網站及其根目錄為/var/www/html/laravel_helloworld/public

<VirtualHost *:80>
        ServerName yoursite.com
        DocumentRoot /var/www/html/laravel_helloworld/public
       <Directory /var/www/html/laravel_helloworld/public>
          <IfModule mod_rewrite.c>
          Options -MultiViews
          RewriteEngine On
          RewriteCond %{REQUEST_FILENAME} !-f
         RewriteRule ^ index.php [L]
       </IfModule>
</Directory>
</VirtualHost>

測試Laravel是否無問題

開啟瀏覽器輸入 http://您的主機IP,將會看到如下畫面,表示安裝成功,若出現Error in exception handler.表示app/storage權限不可寫入,必須將app/storage/ 暫存檔的目錄額外設定寫入權限讓 Web 能夠寫入相關紀錄,請輸入以下指令修改權限。 cd /var/www/html/laravel_helloworld
chgrp -R apache app/storage


在 app/config/app.php 裡的 debug 參數設定為 true 可顯示錯誤訊息,於系統正式上線時,建議將 debug 參數設定為 false。

Composer 補充說明

Composer 基本指令操作使用

  • init: php composer init
  • install: php composer install
  • update:
    • php composer update
    • php composer update vendor/package vendor/package2
    • php composer update vendor/*
  • require:
    • php composer require
    • php composer require vendor/package:2.* vendor/package2:dev-master
  • search: php composer search monolog
  • show:
    • php composer show
    • php composer show monolog/monolog
    • php composer show monolog/monolog 1.0.2
  • depends: php composer depends --link-type=require monolog/monolog
  • validate: php composer validate # It will check if your composer.json is valid
  • status: php composer status # php composer.phar status -v
  • self-update: php composer self-update
  • Config: php composer config --list
  • Modifying Repositories: php composer config repositories.foo vcs http://github.com/foo/bar
  • create-project: php composer create-project doctrine/orm path 2.2.0
  • help: php composer help install

Centos6.4安裝Mongo DB與RockMongo管理工具

下午3:35:00 Posted by Amos , No comments

簡介

本文概述簡單的步驟,教導如何建置Mongo DB及安裝RockMongo(PHP MongoDB Administrator)管理工具,透過管理工具網頁管理MongoDB資料庫。

環境介紹及前置作業

作業系統版本 : Centos 6.4 (64位元)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim 
yum install gcc  (在安裝PHP PEAR時會用到)

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system. 
# SELINUX= can take one of these three values: 
#     enforcing - SELinux security policy is enforced. 
#     permissive - SELinux prints warnings instead of enforcing. 
#     disabled - No SELinux policy is loaded. 
SELINUX=disabled 
# SELINUXTYPE= can take one of these two values: 
#     targeted - Targeted processes are protected, 
#     mls - Multi Level Security protection. 
SELINUXTYPE=targeted  

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟27017防火牆(Mongo DB的Port Number為27017),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT 
/etc/init.d/iptables restart 

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd* 
chkconfig --level 345 httpd on 
chkconfig --list httpd 
為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

安裝及設定php

安裝php套件

安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install php php-devel php-pear
yum -y install libmcrypt* 
#Install the mongo extension on PHP 
pecl install mongo 
安裝mongo for php Driver後,會顯示如下資訊,表示已安裝完成,請於/etc/php.ini檔案中加入extension=mongo.so
Build process completed successfully 
Installing '/usr/lib64/php/modules/mongo.so' 
install ok: channel://pecl.php.net/mongo-1.5.4 
configuration option "php_ini" is not set to php.ini location 
You should add "extension=mongo.so" to php.ini 

設定php.ini檔案

vi /etc/php.ini 
修改為: post_max_size = 800M 
修改為: upload_max_filesize = 800M 
修改為: default_charset="utf8" 
修改為: default_socket_timeout = 300 
修改為: max_execution_time = 1200 
修改為: memory_limit = 1024M 
#Then enable the mongo extension. 
extension=mongo.so 
重新啟動Apache Server。
/etc/init.d/httpd restart

安裝Mongo DB (2.6.3-1)

建立Mongo DB軟體的來源路徑檔案

建立/etc/yum.repos.d/10gen.repo檔案,輸入以下內容並存檔
[10gen] 
name=10gen Repository 
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64 
gpgcheck=0 
enabled=1 

安裝 Mongo DB

請輸入以下指令進行安裝
yum install mongo-10gen mongo-10gen-server

將Mongo DB設定為開機時自動啟用

service mongod start 
chkconfig --level 345 mongod on 
chkconfig --list mongod 

設定Mongo DB設定檔

可根據需求自行編輯MongoDB設定檔 (/etc/mongod.conf),資料庫檔案位置預設為 dbpath=/var/lib/mongo
bind_ip=127.0.0.1  可修改監聽的IP
logpath=/var/log/mongo/mongod.log 
port=27017 
dbpath=/var/lib/mongo 

測試Mongo DB

輸入mongo指令,內建有名稱為test資料庫,針對該資料庫進行新刪修
mongo 
> use test 
switched to db test 
> db.foo.find() 
> db.foo.save({a: 1}) 
> db.foo.find() 
{ "_id" : ObjectId("4b8ed53c4f450867bb35a1a9"), "a" : 1 } 
> db.foo.update( {a: 1}, {a: 5}) 
> db.foo.find() 
{ "_id" : ObjectId("4b8ed53c4f450867bb35a1a9"), "a" : 5 } 

利用 root 新增 Database 的管理帳號

# 切換到 test 
use test 
# 先用 root 帳號認證 
db.getSisterDB("admin").auth("root", "root_password"); 
# 新增 jason 帳號 
db.addUser("jason", "jason_password"); 

新增 Database 的管理帳號

# 切換到 test 
use test 
# 先用 jason 帳號認證 
db.auth("jason", "jason_password"); 
# 新增 leo 帳號 
db.addUser("leo", "leo_password"); 

插入新增資料(Insert)

# 插入一筆新資料到 test Collection (相當於傳統 SQL 裡的 Table 角色) 
db.test.insert({ username: "jason", password: "12345678" }) 
# 含當前時間 
db.test.insert({ username: "jason", password: "12345678", created: new Timestamp() }) 

查詢(Query)

# 查詢所有 username 為 jason 的資料 
db.users.find({ username: "jason" }) 
# 查詢單筆 
db.users.find_one({ username: "jason" }) 

修改更新資料(Update)

# 更新 username 為 jason 的 password 欄位 
db.users.update({ username: "jason" }, { $set: { password: "87654321" }}) 
# 以email欄位取代 username 為 jason 的資料(username 和 password 欄位會因此不見,只剩下 email 欄位) 
db.users.update({ username: "jason" }, { email: "jason@gmail.com.test"}) 

刪除資料(Delete)

# 刪除jason使用者資料 
db.users.remove({ username: "jason" }) 
# 根據ObjectId刪除資料 
db.test.remove({'_id':ObjectId('4f29e4860b2e2ecb9910e304')}) 

測試遠端連線至資料庫功能

若無法連線時,請編輯/etc/mongod.conf檔案,將 bind_ip=127.0.0.1 改成 bind_ip=0.0.0.0,並重新啟動MongoDB
mongo SERVER_IP:27017/test

安裝RockMongo - PHP MongoDB Administrator

下載RockMongo

安裝RockMongo

上傳至root目錄並解壓縮rockmongo-1.1.7.zip檔案並拷貝到/var/www/html資料夾(Aapache Web Root DIR)中,請參考以下步驟
unzip rockmongo-1.1.7.zip 
mv /root/rockmongo-1.1.7 /var/www/html 
ln -s rockmongo-1.1.7 rockmongo (建立rockmongo Link檔案) 

設定RockMongo的設定檔

開啟瀏覽器輸入http://IP/rockmongo,會看到如下登入畫面

於UserName欄位中輸入admin,密碼輸入admin,DB Name輸入test資料庫(內建資料庫),點選登入即可進入管理網頁,請參考如下畫面

設定RockMongo的設定檔

若有需要可自行編輯config.php檔案,修改使用者登入帳號及密碼
$MONGO["servers"][$i]["control_users"]["myusername"] = "mypassword"; 
$MONGO["servers"][$i]["control_users"]["iwind"] = "123456";
若發生 PHP Error: Fatal error: Allowed memory size when export a large DB 的問題,請修改/etc/php.ini檔案中的兩個參數值,請參考如下修改
max_execution_time = 1200 
memory_limit = 1024M

2014年7月15日 星期二

Centos6.5中安裝LAMP與phpMyAdmin軟體

下午4:00:00 Posted by Amos , No comments

簡介

本文概述簡單的步驟,教導如何建置LAMP環境(Apache、Php、MySQL),並安裝phpmyadmin管理MySQL資料庫。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system. 
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

設定Open File個數

編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT 
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
/etc/init.d/iptables restart

安裝及設定Apache

輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd* 
chkconfig --level 345 httpd on
chkconfig --list httpd

為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf 

#<LocationMatch "^/+$">
    #Options -Indexes
    #ErrorDocument 403 /error/noindex.html
#</LocationMatch>

啟用Apache時若有出現Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf 
ServerName localhost:80 #新增加這一項參數

安裝及設定php

安裝php套件

安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install php* 

安裝 mcrypt 以支援phpMyAdmin高級功能
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm 
yum install php-mcrypt

重新啟動Apache Server。
/etc/init.d/httpd restart

設定php.ini檔案

編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini 
修改為: post_max_size = 800M
修改為: upload_max_filesize = 800M
修改為: default_charset="utf8"
修改為: default_socket_timeout = 300

安裝及設定MySQL資料庫

安裝MySQL

安裝MySQL相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install mysql-server 

設定MySQL自動啟動

chkconfig --level 345 mysqld on 
chkconfig --list mysqld

設定MySQL root帳號之密碼

/usr/bin/mysqladmin -u root password '新密碼'

重新啟動 MySQL Server
/etc/init.d/mysqld restart

安裝及設定phpMyAdmin

下載phpMyAdmin-4.0.10.1-all-languages.tar.gz並安裝

解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz

搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1

複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php

修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */ 
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart

修改config.inc.php檔案的權限
chmod 644 config.inc.php

移除setup安裝資料夾
mv setup setup.org

設定phpMyAdmin網站別名(可視情況設定)

編輯 /etc/httpd/conf/httpd.conf 檔案

#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
  Options -Indexes FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫

2014年7月14日 星期一

Centos6.5中安裝node.js並使用Express執行node App

晚上9:25:00 Posted by Amos , No comments

簡介

本文概述了簡單的步驟,教導如何安裝node.js並於該環境中執行一個 Hello World Applicaiton,租用Centos6.4 64位元版本的雲端伺服器進行測試及安裝,Node.js主要是採用Chrome JavaScript之運行引擎,是一個快速,事件驅動的平台和運行於服務器端之Javascript 語言,用於構建Web應用程式。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing 改成 SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
/etc/init.d/iptables restart

設定主機執行服務之open file的個數

編輯/etc/security/limits.conf檔案,輸入以下指令並存檔
* hard nofile 65535
* soft nofile 65535

更改主機(hostname)名稱

編輯 /etc/sysconfig/network檔案,輸入hostname的名稱(如: localhost.localdomain )
localhost.localdomain
編輯 /etc/hosts檔案,修改如下
127.0.0.1       localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6

安裝 Node.js

下載EPEL和REMI擴充套件,並使用rpm安裝套件
wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -ivh epel-release-6-8.noarch.rpm
rpm -ivh remi-release-6.rpm

使用 yum 指令安裝 node.js 和 NPM套件管理工具,npm主要提供node.js許多好用的第三方lib套件
yum install nodejs
yum install npm

安裝 Express.js

現在,我們已經將Node.js的安裝完成,可以開始開發及部署已完成的應用程式。首先,我們將使用NPM,安裝express套件。

 npm -g install express express-generator supervisor
安裝完成後將會顯示安裝之目錄結構與檔案,如下
supervisor@0.6.0 /usr/lib/node_modules/supervisor 

express-generator@4.2.0 /usr/lib/node_modules/express-generator
├── mkdirp@0.3.5
└── commander@1.3.2 (keypress@0.1.0)

express@4.6.1 /usr/lib/node_modules/express
├── merge-descriptors@0.0.2
├── utils-merge@1.0.0
├── parseurl@1.1.3
├── cookie@0.1.2
├── escape-html@1.0.1
├── finalhandler@0.0.3
├── cookie-signature@1.0.4
├── range-parser@1.0.0
├── fresh@0.2.2
├── vary@0.1.0
├── qs@0.6.6
├── media-typer@0.2.0
├── methods@1.1.0
├── serve-static@1.3.2
├── buffer-crc32@0.2.3
├── depd@0.3.0
├── path-to-regexp@0.1.3
├── type-is@1.3.2 (mime-types@1.0.1)
├── accepts@1.0.7 (negotiator@0.4.7, mime-types@1.0.1)
├── proxy-addr@1.0.1 (ipaddr.js@0.1.2)
├── debug@1.0.3 (ms@0.6.2)
└── send@0.6.0 (ms@0.6.2, mime@1.2.11, finished@1.2.2)

於這個指令中的 -g 參數的意思是指安裝至global位置(本安裝位置位於:/usr/lib/node_modules)。

建立一個Express架構的 Web Application

Express是一個Web App Framework,提供許多功能(如:restful等),首先使用以下指令建立express framework的專案名稱為: hello
express express-helloworld 

create : express-helloworld
create : express-helloworld/package.json
create : express-helloworld/app.js
create : express-helloworld/public
create : express-helloworld/public/javascripts
create : express-helloworld/public/images
create : express-helloworld/public/stylesheets
create : express-helloworld/public/stylesheets/style.css
create : express-helloworld/routes
create : express-helloworld/routes/index.js
create : express-helloworld/routes/users.js
create : express-helloworld/views
create : express-helloworld/views/index.jade
create : express-helloworld/views/layout.jade
create : express-helloworld/views/error.jade
create : express-helloworld/bin
create : express-helloworld/bin/www
install dependencies:
$ cd express-helloworld && npm install

run the app:
$ DEBUG=express-helloworld ./bin/www

建立完成後將會產生如下的檔案結構,檔案結構說明如下:
app.js : 主程式
bin : 內有www的檔案,負責啟動node.js server的檔案,該檔案會呼叫app.js
package.json : 專案設定檔案,紀錄專案需要使用到的套件,透過輸入 npm install 指令可自動下載相關套件
public : 存放靜態網頁 (CSS,Javascript,圖檔等)
routes : 設定restful網頁路徑js檔案,屬於MVC中的Control部分
views : 存放顯示UI的網頁,預設使用Jude Template設計網頁UI,屬於MVC中的View部分

切換目錄至 express-helloworld 資料夾,並執行以下指令安裝相關Framework套件
 
cd express-helloworld
npm install

切換目錄至 express-helloworld/bin 資料夾,編輯www檔案,將3000 Port 改成 8080 Port
app.set('port', process.env.PORT || 3000); 修改成 
app.set('port', process.env.PORT || 8080);

最後執行以下指令啟動node.js應用程式
 node ./bin/www 
現在我們就可以開啟瀏覽器執行 http://雲端伺服器的IP:8080/