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