顯示具有 VirtualBox 標籤的文章。 顯示所有文章
顯示具有 VirtualBox 標籤的文章。 顯示所有文章

2018年3月28日 星期三

VirtualBox 安裝CentOS-6.9

使用VirtualBox(5.2.8 -> 4.3.10) 安裝CentOS 6.9
PHP 5.5 Redis Memcache Supervisor Beanstalk
MySQL 5.6(略,裝在Windows7 host機上 )

開機自動啟動eth0網路
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
ONBOOT=no
ONBOOT=yes

6.9 yum預設安裝的php是5.3.3版本
因為CentOS通常提供7-10年的生命週期,所以這很難讓php(和其他packages)維持在最新的版本,你能這麼做:
1. 裝5.3.3版
2. 使用源碼(source)去編譯安裝(build)
3. 使用有提供可選擇的三方源( 3rd party repository )

我選擇#3

使用webtatic.com源
https://webtatic.com/packages/php55/
Webtatic has now ended support for PHP 5.5
Webtatic 已終止PHP 5.5的支持

使用remirepo源
https://blog.remirepo.net/post/2016/09/10/PHP-5.5-is-dead
安裝remirepo源
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm -Uvh remi-release-6*.rpm
warning: remi-release-6.rpm: Header V4 DSA/SHA1 Signature, key ID 00f97f56: NOKEY
Preparing...                ########################################### [100%]
   1:remi-release           ########################################### [100%]
查remirepo源版本
# yum info remi-release
...
Name        : remi-release
Version     : 6.9
如果你要最新的php 5.5.x,必須這樣做
# yum --enablerepo=remi,remi-php55 update -y
安裝PHP 5.5
# yum install php55-php

因為安裝的是remi源的php 5.5,所以命令行直接輸入php會找不到指令,要輸入php55
解法
# source /opt/remi/php55/enable

安裝php-fpm(yum預設的php-fpm是5.3.3的,所以要照下面這樣裝)
# yum install php55-php-fpm

安裝nginx
# yum install nginx

安裝memcached
# yum install memcached

安裝redis
# yum install redis

安裝supervisor
# yum install supervisor
啟動supervisor
# supervisord -c /etc/supervisord.conf
只有supervisor比較特殊,如果用service supervisor start的話,只會執行php artisan queue:listen tube-name 但不會生成 php artisan queue:work tube-name

安裝beanstalk
# yum install beanstalkd

配置nignx
/etc/nginx/conf.d/default.conf
server {
    ...

    location / {
        index index.php  index.html index.htm;
    }
    ...
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
s

啟動php-fpm、nginx、memcached、redis、beanstalkd
# service php55-php-fpm start
# service nginx start
# service memcached start
# service redis start
# service beanstalkd start

開啟80 port
# iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
# service iptables save
如不開啟 Windows chrome訪問 VM會報錯 ERR_CONNECTION_TIMED_OUT
telnet 會不通,但是在VM上 curl http://localhost 能訪問
配置後,記得換瀏覽器試試,不然chrome配置前訪問過,配置後仍然會卡住
看iptables有哪些規則
# iptables -S
# iptables --line -vnL

開機自動啟動php-fpm、nginx、memcached、redis、beanstalkd
# chkconfig nginx on
# chkconfig php55-php-fpm on
# chkconfig memcached on
# chkconfig redis on
# chkconfig beanstalkd on
檢查
#  chkconfig  --list nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
0~6是linux的runlevel

Virtualbox降版本
原本使用最新版Virtualbox 5.2.8
因為重裝一台Virtualbox也會遇到開機卡在creating process for virtual machine (gui/qt) ... (1/2) 的問題
https://www.youtube.com/watch?v=20p-r5WzYXg
這邊說要降版本到4.3.10
降到4.3.10後打開虛擬機會有連不上網路的問題
# ifup eth0
Determining IP information for eth0... failed; no link present. Check cable?
解法
Virtualbox選你的虛擬機 => 設置 => 網路 => 選你的網卡 => 高級 => 勾選"接入網線"
降版本後開機報錯HostAudioNotResponding
解法(因為當虛擬機當web server用不需要聲音)
Virtualbox選你的虛擬機 => 設置 =>聲音 => 取消勾選"啟用聲音"

安裝php所需擴展
# yum install php55-php-mcrypt
# yum install php55-php-pdo
# yum install php55-php-pecl-memcached
# yum install php55-php-gd  //防止圖片驗證碼報錯:Call to undefined function Johntaa\Captcha\imagecreatefrompng()
# yum install php55-php-pecl-xdebug
# service php55-php-fpm restart


laravel 連不上redis
網頁報錯 Permission denied [tcp://127.0.0.1:6379]
但是 telnet 127.0.0.1 6379 和 redis-cli 可以通
因為
# getsebool httpd_can_network_connect
httpd_can_network_connect --> off
解法
# /usr/sbin/setsebool httpd_can_network_connect=1
Use -P option makes the change permanent. Without this option, the boolean would be reset to 0 at reboot.
使用-P選項使其永久更改,否則重開機設定會被重置
# /usr/sbin/setsebool -P httpd_can_network_connect=1  => VM下實測跑不過
解法2
把它寫到 /etc/rc.d/rc.local 下 => 解決

laravel虛擬域名千萬不能配置有底線(_) 否則會被轉跳到
http://x_web.centos/http://x_web.centos/auth/signin

讓php能在command line使用
因為用remi裝的php55,所以命令行輸入php會找不到,只能用php55
解法:
# source /opt/remi/php55/enable
因為supervisor執行時吃不到這句,所以command路徑要寫完全
command = /opt/remi/php55/root/usr/bin/php /path/artisan queue:listen ga2-xxx

查remi裝的php.ini在哪
# php -i | grep "Loaded Configuration File"
Loaded Configuration File => /opt/remi/php55/root/etc/php.ini

解決 could not find driver 錯誤
# php -m | grep pdo
pdo_sqlite  // 只有這個,沒有pdo_mysql所以出錯
安裝php55-php-mysqlnd(其實我安裝的是php55-php-pecl-mysqlnd-qc,但是會一起裝php55-php-mysqlnd)
# yum install php55-php-pecl-mysqlnd-qc
為什麼會裝php55-php-pecl-mysqlnd-qc?
因為
# yum search pdo_mysql
...
php55-php-pecl-mysqlnd-qc.x86_64 : A query cache plugin for mysqlnd


參考資料:
https://unix.stackexchange.com/questions/138019/php-5-5-13-on-centos-6-5-with-yum  PHP 5.5.13 on CentOS 6.5 with yum
https://webtatic.com/packages/php55/  PHP 5.5 on CentOS/RHEL 7.2, 6.8 and 5.11 via Yum
https://www.binarytides.com/open-http-port-iptables-centos/  Open http port ( 80 ) in iptables on CentOS
https://zh.wikipedia.org/wiki/%E8%BF%90%E8%A1%8C%E7%BA%A7%E5%88%AB  執行級別
https://www.shellhacks.com/ubuntu-centos-enable-disable-service-autostart-linux/  Ubuntu, CentOS – Enable or Disable Service Autostart in Linux
https://stackoverflow.com/questions/27982024/php-command-not-found-after-yum-install-php55-php-cli  php command not found after yum install php55-php-cli
https://stackoverflow.com/questions/8765848/troubleshooting-permission-denied-when-attempting-to-connect-to-redis-from-php  Troubleshooting “Permission denied” when attempting to connect to Redis from PHP script
https://stackoverflow.com/questions/27982024/php-command-not-found-after-yum-install-php55-php-cli  php command not found after yum install php55-php-cli
https://stackoverflow.com/questions/2852748/pdoexception-could-not-find-driver  PDOException “could not find driver”
https://stackoverflow.com/questions/17850787/where-is-the-php-ini-file-on-a-linux-centos-pc  Where is the php.ini file on a Linux/CentOS PC? [duplicate]
https://stackoverflow.com/questions/36019111/pdo-mysql-is-not-available-on-centos-6-7  pdo_mysql is not available on centos 6.7
https://serverfault.com/questions/240015/how-do-i-allow-mysql-connections-through-selinux  how do I allow mysql connections through selinux





2016年8月16日 星期二

MySQL SSH Tunnel

聽Jethro和Ike討論 Naticat的強大,如:SSH Tunnel、structure sync 

因為我常用的是phpmyadmin,所以找一下SSH Tunnel + phpmyadmin的解決方法

我有一台VPS機器,ssh需要私鑰登入,MySQL port沒有對外,如何使用phpmyadmin連線?

打開ssh通道
$ ssh -N <username>@<Bastian server IP> -L 3307:<internal ip of mysql server>:3306
-N 你不執行任何命令,僅端口轉發( port forward )
3307 本地要轉發的port
Bastian server IP 必須能連到MySQL 伺服器
3306 遠端機器的MySQL端口

因為我電腦使用Windows 7 ,所以這道命令在虛擬機上下的
ex. 
$ ssh -i vps.key -N user@vps.ip -L 3307:localhost:3306
3307:localhost:3306 是指虛擬機的3307端口 轉發到 vps.ip上的localhost 3306端口

檢測
$ telnet localhost 3307
...
5.6.28-76.1-log ... mysql_native_password
VPS上的mysql版本是5.6.28 沒錯

連線
$ mysql -u root -p  -h localhost -P 3307
經過幾次測試後, -P 是無效的,因為虛擬機和VPS的MySQL密碼和版本不一樣,但是都要輸入虛擬機的密碼,只能連到虛擬機的MySQL
原因:
當使用 localhost 參數時, MySQL使用sockets。請改用127.0.0.1
$ mysql -u root -p  -h 127.0.0.1 -P 3307

後記,因為虛擬機上沒裝phpmyadmin,所以Windows 7 在 config.inc.php 設不了 127.0.0.1:3307,只能設 192.168.x.x:3307 (虛擬機IP) ,去連VPS的MySQL。但是VPS的MySQL只允許 localhost或127.0.0.1登入

結論,使用Naticat的SSH Tunnel功能在此場景會比較方便,不過私鑰記得轉成ppk去連線





2015年12月15日 星期二

VirtualBox 虛擬機跑MySQL

寫下我最近用VirtualBox跑測試環境過慢的問題

虛擬機
ubuntu 14.04
mysql  Ver 14.14 Distrib 5.5.41, for debian-linux-gnu (x86_64) using readline 6.3

問題一
靜態網頁phpinfo()跑很慢
因為我的虛擬機測試環境已經很髒了,之前在這篇 haproxy使用心得 設定了load balance,所以
先用telnet xxx 80 查連不連得到 => 連不到
用kill把nginx進程(process)全殺了
# killall nginx
再重啟
# /etc/init.d/nginx start
robert:可能是進程被咬死,用service nginx restart 是起不了作用的

問題二
開有連虛擬機上資料庫的網頁非常慢,但設定改為連其他機器時,速度正常
在mysql用show processlist檢查後發現
mysql> show processlist;
...
| 10 | unauthenticated user | 192.168.169.147:51391 | NULL | Connect | NULL | login          | NULL

這個連線在卡
解法:
http://stackoverflow.com/questions/8034706/access-mysql-server-on-linux-throu-virtualbox-windows7-laggs
在 /etc/mysql/my.cnf 的 [mysqld] 中新增
skip-name-resolve

然後重啟mysql
# service mysql restart

問題三
phpmyadmin 編輯欄位速度過慢
mysql> show processlist;
...
| 19 | root | 192.168.169.140:51359 | progmate_test | Query   |   39 | NULL  | SELECT `column_name`, `table_name`, `table_schema`, `referenced_column_name` FROM `information_schem |
卡在SELECT information_schema這個表

簡單的測試
https://www.percona.com/blog/2011/12/23/solving-information_schema-slowness/  Solving INFORMATION_SCHEMA slowness
mysql> select count(*),sum(data_length) from information_schema.tables;
+----------+------------------+
| count(*) | sum(data_length) |
+----------+------------------+
|      769 |       5880654489 |
+----------+------------------+
1 row in set (24.77 sec)

mysql> set global innodb_stats_on_metadata=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select count(*),sum(data_length) from information_schema.tables;
+----------+------------------+
| count(*) | sum(data_length) |
+----------+------------------+
|      769 |       5880654489 |
+----------+------------------+
1 row in set (0.07 sec)

解法:
http://stackoverflow.com/questions/7283915/poor-performance-of-information-schema-key-column-usage-in-mysql  Poor performance of INFORMATION_SCHEMA.key_column_usage in MySQL
/etc/mysql/my.cnf 的 [mysqld] 中新增
innodb_stats_on_metadata=0

然後重啟mysql
# service mysql restart

什麼是innodb_stats_on_metadata?
https://www.percona.com/blog/2013/12/03/innodb_stats_on_metadata-slow-queries-information_schema/  innodb_stats_on_metadata and slow queries on INFORMATION_SCHEMA
When the option is set to ON, InnoDB index statistics are updated when running SHOW TABLE STATUS, SHOW INDEX or when querying INFORMATION_SCHEMA.TABLES or INFORMATION_SCHEMA.STATISTICS. These statistics include the cardinality and the number of entries, they are used by the optimizer to find an optimal execution plan.
當這個選項被設置為ON時。當執行 SHOW TABLE STATUS、SHOW INDEX、操作INFORMATION_SCHEMA.TABLES或INFORMATION_SCHEMA.STATISTICS表時, InnoDB的索引統計正被更新。這個統計包括基數( cardinality ) 和 entry數,他們用於優化找到一個最佳的執行計畫
So even if SELECT statements cannot change the real statistics, MySQL updates the statistics for InnoDB tables. This is counterintuitive.
因此,如果SELECT語句不能改變真正的統計(MySQL更新InnoDB表的統計)。這是違反直覺的。
Is it useful? Not really, because InnoDB will always compute statistics when you open a table for the first time and when significant portions of the table have been modified (and when you run ANALYZE TABLE).
它有用嗎?不是,因為InnoDB總是計算統計當你第一次打開一個表和當一個表顯著的部分被修改了(和當你運行ANALYZE TABLE)
The problem was magnified in the example because the whole dataset was not fitting in memory, the number of tables was high and the I/O subsystem was not very powerful.
該問題是被放大的例子,因為整個dataset未嵌合在記憶體中,表的數量很大而子系統的I/O不夠強

值得提出來的是,這個值在 MySQL 5.6 內的 InnoDB 預設是關閉的,在 5.1 與 5.5 則是開啟的。

參考資料:
http://sourceforge.net/p/phpmyadmin/bugs/4592/  #4592 (ok 4.4) Editing columns (tbl_structure.php) is incredibly slow
https://blog.gslin.org/archives/2013/12/09/3918/innodb_stats_on_metadata-%E5%B0%8D%E6%95%88%E8%83%BD%E7%9A%84%E5%BD%B1%E9%9F%BF/  innodb_stats_on_metadata 對效能的影響...