樹莓派raspberry pi 安裝CentOS+PHP+MariaDB+Redis+Memecache
前言
入手了一個樹莓派,想做個自動餵魚器。但是看了GPIO後,感覺沒啥難度,就是放棄了。
然後順便想自己搭個產品運行環境算了,以前用Vagrant 在本機太喫性能了。
本文應該是國內最全文檔,不接受反駁。
設備
RaspBerry Pi 3B+
安裝 CentOS
下載CentOS Armhfp 鏡像
鏡像列表
http://isoredirect.centos.org/altarch/7/isos/armhfp/
wget https://mirror.xtom.com.hk/centos-altarch/7.6.1810/isos/armhfp/CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-1810-sda.raw.xz
重要: Armhfp 是32位的,所以有些64位特性會有細節差別,比如bigint
安裝鏡像
下面只列出在MacOS下的方法
其他操作系統可以參考Ubuntu 鏡像的安裝方法
https://www.ubuntu.com/download/iot/installation-media
- 列出設備
$diskutil list
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *121.3 GB disk0
1: EFI EFI 209.7 MB disk0s1
2: Apple_APFS Container disk1 121.1 GB disk0s2
/dev/disk1 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +121.1 GB disk1
Physical Store disk0s2
1: APFS Volume Macintosh HD 74.0 GB disk1s1
2: APFS Volume Preboot 45.0 MB disk1s2
3: APFS Volume Recovery 517.0 MB disk1s3
4: APFS Volume VM 3.2 GB disk1s4
/dev/disk2
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *7.9 GB disk3
1: DOS_FAT_32 NO NAME 7.9 GB disk3s1
- 找到插入的SD卡 /dev/disk2 (因人而異),然後卸載設備
$diskutil unmountDisk /dev/disk2
- 將鏡像拷貝到SD卡里,注意需要 sudo 權限,xz 命令需要單獨安裝( $brew install dx )
$sudo sh -c 'xzcat ~/Downloads/CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-1810-sda.raw.xz | sudo dd of=/dev/disk2 bs=32m'
- 一直等到出現
3719+1 records in
3719+1 records out
3899999744 bytes transferred in 642.512167 secs (6069924 bytes/sec)
這樣的提示出現,具體數據可能不一樣。
- 然後將CeotOS SD卡插入樹莓派,插入鍵盤、顯示器,啓動樹莓派。
登錄名:root
登錄密碼:centos
擴展磁盤空間
如果不擴展,默認的使用容量很小,大概只有幾百兆
參考:https://blog.csdn.net/qq_36731677/article/details/78764941
使用fdisk命令格式化剩餘磁盤。
fdisk /dev/mmcblk0
1. 輸入 d , 刪除分期,使用默認的第三分區即可
2. 輸入 n,添加分區,使用默認 第三 primary主分區即可
3. 輸入 w,寫入修改。如果推出不寫入,則修改無效
reboot 重啓系統後,執行
resize2fs /dev/mmcblk0p3
重置磁盤空間後,執行 df -h
查看空間大小即可
連接Wifi
使用CentOS自帶的圖形管理軟件
nmtui
選擇 Activate a connection
(第二個)
然後進入二級頁面後,選擇要連接的wifi、輸入密碼,退出即可。
關閉防火牆
systemctl stop firewalld.service
Yum命令修復
如果直接使用Yum命令,是不能用的,會提示缺少armhfp 。因爲epel裏的源沒有arm源。解決方案就是刪除epel,或者修改epel文件。
如果要換國內源,參考 https://blog.csdn.net/lhj_168/article/details/85805799
- 將epel源修改爲armhfp支持
cat > /etc/yum.repos.d/epel.repo << EOF
[epel]
name=Epel rebuild for armhfp
baseurl=https://armv7.dev.centos.org/repodir/epel-pass-1/
enabled=1
gpgcheck=0
EOF
- 同時加入php7.2源
cat > /etc/yum.repos.d/php72-testing.repo << EOF
[php72-testing]
name=Remi php72 rebuild for armhfp
baseurl=https://armv7.dev.centos.org/repodir/community-php72-testing/
enabled=1
gpgcheck=0
EOF
- 清除yum緩存 重要,不執行的話,yum修改不生效
yum clean up
yum makecache
節後閱讀: 此階段參考:https://seven.centos.org/2018/01/php-7-2-for-centos-7-armhfp/
安裝一些常用的命令
- 自由發揮
yum install wget git vim -y
- 安裝一些前置依賴
yum install libevent-devel openssl openssl-devel telnet gcc+ gcc-c++ cmake ncurses-devel libaio -y
- 安裝php
yum install php-devel php-mysql php-pecl-redis php-fpm php-opcache php-bcmath php-pecl-memcached
- 查看php
$php -v
PHP 7.2.16 (cli) (built: Mar 20 2019 00:26:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.16, Copyright (c) 1999-2018, by Zend Technologies
- 運行PHP
sudo mkdir -p /run/php-fpm
sudo php-fpm
- 修改一些小配置
php.ini 裏的 timezone 默認改爲 PRC
php-fpm.d/www.conf 裏面的user和group改爲運行時用戶
安裝 Nginx
- 下載 nginx
$wget http://nginx.org/download/nginx-1.12.2.tar.gz
$tar -zxf nginx-1.12.2.tar.gz
$cd nginx-1.12.2
- 編譯
下面的{path}爲nginx放置的目錄
$./configure --prefix={path}/nginx --pid-path={path}/nginx/nginx.pid --lock-path={path}/nginx/nginx.lock --user=work --group=work --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
$make && make install
- 測試
./sbin/nginx -t
如果出現
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
nginx: configuration file /home/work/nginx/conf/nginx.conf test failed
在nginx.conf
的http裏面加入 server_names_hash_bucket_size 512;
即可
- 啓動
sudo ./sbin/nginx
安裝 Redis
- 下載 Redis 穩定版(截至發稿前 5.0)
wget http://download.redis.io/releases/redis-stable.tar.gz
tar -zxf redis-stable.tar.gz
cd redis-stable
make
mkdir logs
#編譯後redis不需要install
cd ..
mv redis-stable redis
- 配置到個人小癖好 redis.conf
配置對應的log和 bind ip
daemonize yes
pidfile {path}/redis/redis_6379.pid
logfile " {path}/redis/logs/redis.log"
bind xxx.xxx.xxx
#後臺運行
daemonize yes
# 如果Log配置了目錄,要創建一下
mkdir logs
啓動
./src/redis-server redis.conf
安裝 MariaDB (MySQL)
- 安裝
sudo yum install mariadb-server
- 啓動
#啓動
systemctl start mariadb.service
#簡單快速配置
mysql_secure_installation
#登錄
mysql -u root -p
#設置root 可以從任何地方登錄
use mysql;
update user set host = '%' where user = 'root' and host='localhost';
flush privileges;
安裝 Memcache
- 下載編譯
wget http://memcached.org/files/memcached-1.5.7.tar.gz
tar zxf memcached-1.5.7.tar.gz
cd memcached-1.5.7
./configure --prefix={path}/memcached
make && make install
- 啓動
./bin/memcached &
Last. Enjoy yourself. 樹莓派。。。真的沒什麼用啊
參考
https://www.ubuntu.com/download/iot/installation-media
https://blog.csdn.net/qq_36731677/article/details/78764941
https://seven.centos.org/2018/01/php-7-2-for-centos-7-armhfp/
https://blog.csdn.net/lhj_168/article/details/85805799