树莓派从零开始搭建LNMP(centos7)

系统安装

  1. 下载镜像并解压
    http://mirrors.huaweicloud.com/centos-altarch/7.7.1908/isos/armhfp/CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-1908-sda.raw.xz
    下载完成后使用7-zip软件将镜像解压成raw文件
  2. 使用DiskGenius将SD卡(orU盘)格式化
    默认会添加盘符,如果没有,请手动添加
  3. 使用Win32DiskImager将raw文件刷入SD卡
    选择文件时默认为img文件,下拉选择所有文件即可选择刚才解压的raw文件
  4. 镜像刷入成功后,打开SD卡中文件cmdline.txt,在头部插入ip="你要设置的IP",后面加空格
    此步可以省略。树莓派IP可以登录路由器上查找。
  5. 默认用户名(root)和密码(centos)

扩展内存空间

使用终端登录之后执行以下命令

fdisk /dev/mmcblk0  # 进入磁盘分区shell
 # 查看目前分区情况,有三个mmcblk0p1~3
> p 
# 删除最后一个分区,之后直接按enter即可
> d  
# 查看是否删除成功
> p 
# 新建分区 
> n 
# 选择主分区,因为刚才删除的就是主分区,分区号(Partition number)默认是3 
> p  
# .......... 注意 .......
# 下面看清楚:默认的起始sector如果是2048,则需要输入分区2(mmcblk0p2)结束(End)块区+1,然后一直ender下去即可;否则一直enter即可
#退出并保存分区
> w 
# .......... 注意 .......
# 此时已经回到centos7的shell,执行以下命令,否则** 无法开机 **
touch /.rootfs-repartition  
# 重启系统
 systemctl reboot
# 开机之后执行,重新加载分区信息
resize2fs /dev/mmcblk0p3
# 查看分区信息
df -h
lsblk

更换yum源

mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak
cat >/etc/yum.repos.d/development.repo <<EOF
[base]
name=CentOS-\$releasever - Base
baseurl=http://mirrors.ustc.edu.cn/centos-altarch/\$releasever/os/\$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32

[updates]
name=CentOS-\$releasever - Updates
baseurl=http://mirrors.ustc.edu.cn/centos-altarch/\$releasever/updates/\$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32
 
[extras]
name=CentOS-\$releasever - Extras
baseurl=http://mirrors.ustc.edu.cn/centos-altarch/\$releasever/extras/\$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32
 
[centosplus]
name=CentOS-\$releasever - Plus
baseurl=http://mirrors.ustc.edu.cn/centos-altarch/\$releasever/centosplus/\$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32
EOF
yum makecache

暂时关闭selinux和防火墙

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'  /etc/selinux/config
setenforce 0
systemctl disable firewalld
systemctl stop firewalld

顺序部署php、nginx、mysql

php

  1. 编译安装
<!-- more -->

<!-- more -->

yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel
cd /usr/local/src
wget "https://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz"
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
cd /usr/local/src
wget "https://www.php.net/distributions/php-5.6.40.tar.gz"
tar -zxvf php-5.6.40.tar.gz
cd php-5.6.40
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-ctype --with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib --with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-ldap-sasl \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-fpm
make && make install
cp php.ini-production /usr/local/php/etc/php.ini
  1. 环境变量
echo "export PATH=\$PATH:/usr/local/php/sbin:/usr/local/php/bin" >>/etc/profile && source /etc/profile
  1. 配置修改
mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

注意将"user"和"group"这两个变量的值都改为root
检查配置文件:php-fpm -t

  1. systemctl管理
cat >/usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm -R
[Install]
WantedBy=multi-user.target
EOF
systemctl enable php-fpm
systemctl start php-fpm

nginx

  1. 编译安装
yum install -y wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /usr/local/src
wget "http://nginx.org/download/nginx-1.9.9.tar.gz"
tar -zxvf nginx-1.9.9.tar.gz
cd nginx-1.9.9
./configure --prefix=/usr/local/nginx
make && make install
  1. 环境变量
echo "export PATH=\$PATH:/usr/local/nginx/sbin" >>/etc/profile && source /etc/profile
  1. 配置修改
    配置文件:/usr/local/nginx/conf/nginx.conf
# 将第二个配置在配置文件中存在,需要去掉前面的注释,其次要修改fastcgi_param这一行
location / {
  root html;
  index index.html index.htm index.php;
}
location ~\.php${
  root html;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}
  1. systemctl管理
cat >/usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx
systemctl start nginx

mysql

  1. 编译安装
yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel
useradd -s /sbin/nologin mysql
cd /usr/local/src
wget "https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.47.tar.gz"
tar -zxvf mysql-5.6.47.tar.gz
cd mysql-5.6.47
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOACL_INFILE=1
make && make install
cp support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
  1. 环境变量
echo "export PATH=\$PATH:/usr/local/mysql/bin" >>/etc/profile && source /etc/profile
  1. 配置文件
cat >/etc/my.cnf <<EOF
[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
user=mysql
skip-name-resolve
long_query_time=2
slow_query_log_file=/data/mysql/mysql-slow.log
expire_logs_days=2
innodb-file-per-table=1
innodb_flush_log_at_trx_commit=2
log_warnings=1
max_allowed_packet=512M
connect_timeout=60
net_read_timeout=120
[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
EOF
  1. 初始化
mkdir -pv /data/mysql
chown -R mysql:mysql /usr/local/mysql /data/mysql
yum install -y perl-Module-Install
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql/
  1. systemctl管理
cat >/usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql
TimeoutSec=600
Restart=always
PrivateTmp=false
EOF
systemctl enable mysqld
systemctl start mysqld
  1. 安全设置
    设置密码和本地登录
mysqladmin -h 127.0.0.1 -uroot password "raspberrypwd"
mysql -h 127.0.0.1 -uroot -praspberrypwd