前言 在前一章,我们搭建了操作系统并且做了一些基本设置,接下来需要部署一个LNMP环境,提供云盘运行基础。
准备 为了方便起见,我们将所有的源代码文件直接放到/root/src文件夹下。笔者基本想法是:编译安装Nginx和PHP,YUM安装MariaDB。至于为何如此做法,主要有如下原因:
CentOS ARM软件库内,PHP版本最新为5.4.x,达不到NextCloud的最低版本要求(5.6); 编译安装更容易控制和操作; MariaDB作为数据库存储,没必要做编译安装这些复杂步骤,特别是在树莓派下,编译安装MariaDB花费的时间简直令人瞠目结舌。 编译安装NGINX 本身来说,笔者对于Apache更加熟悉一点。不过,最近Nginx貌似有越来越火的倾向,笔者也就趁着这个机会熟悉一下Nginx。
下载Nginx 既然已经选择了编译安装,那么自然就选择Nginx的最新版本。从以下地址下载最新版本的Nginx:
Nginx 1.13.7 
1 2 3 4 cd ~/src # root 用户 wget http://nginx.org/download/nginx-1.13.7.tar.gz tar -zxvf nginx-1.13.7.tar.gz rm -rf nginx-1.13.7.tar.gz 
下载相关依赖包 由于我们需要使用的一些特性需要额外包的支持,所以,我们需要下载这些包:
openssl(openssl-1.1.0g) pcre-8.41 zlib-1.2.11 并将下载的包防止并且解压到/root/src文件夹下。
编译 在编译之前,我们需要了解下具体的编译参数,才能根据我们自己的需要进行相关设置。官网为我们提供了参数的具体作用说明:https://www.nginx.com/resources/wiki/start/topics/tutorials/installoptions/。 
在这里,笔者只需要设置以下参数:
1 2 3 4 5 6 7 8 9 10 11 ./configure \ --prefix=/home/web/nginx \                            # nginx安装根目录 --conf-path=/home/web/conf/nginx \                    # nginx配置文件路径 --error-log-path=/var/log/nginx/error.log \           # nginx错误日志 --http-log-path=/var/log/nginx/access.log \           # nginx访问日志 --user=web \                                          # nginx运行系统用户 --group=web \                                         # nginx运行用户所属组 --with-http_ssl_module \                              # 启用nginx的ssl模组 --with-openssl=/root/src/openssl-1.1.0g \             # 设置ssl的来源源码路径 --with-pcre=/root/src/pcre-8.41 \                     # 设置pcre的源码路径 --with-zlib=/root/src/zlib-1.2.11                     # 设置zlib的源码路径 
在编译的过程中,可能会出现一些ERROR的情况,大部分原因是缺少相关依赖包。只要根据提示安装就可以了。
安装 编译完成后,安装即可:
1 make -j4 && make install  # -j4 代表以4进程并发编译,大大提升编译速度 
启动 1 /home/web/nginx/sbin/nginx -c /home/web/conf/nginx/nginx.cnf  # 因为我们设置了额外的配置文件路径,注意,这里可能需要将`/home/web/conf/`文件夹下的所有文件除了nginx文件夹,全部移动到nginx文件夹内。即与nginx.cnf文件同级。至于为啥会出现这种情况,笔者也搞不明白。如果这里启动不成功的话,可以使用`/home/web/nginx/sbin/nginx -c /home/web/conf/nginx/nginx.cnf -t`命令查看配置文件的详细问题描述。 
开启端口 安装后,如果想要访问树莓派,需要开启80端口:
1 2 firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload 
测试 打开浏览器输入树莓派的IP地址:http://your-rasp-pi-ip 
编译安装PHP7.1 既然自己使用,本着折腾的想法,笔者选择了PHP的最新版本7.1.
编译 如何下载就不具体描述了,直接进入正题:编译。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 ./configure \ --prefix=/home/web/php \                            # PHP安装路径 --with-config-file-path=/home/web/conf/php \        # PHP.ini所处文件夹 --with-mysql-sock=/var/lib/mysql/mysql.sock \       # mysql进程文件 --with-mcrypt \ --with-mhash \ --with-openssl \ --with-mysqli=shared,mysqlnd \                      # PHP7不再使用mysql而是使用mysqli --with-pdo-mysql=shared,mysqlnd \ --with-gd \ --with-iconv \ --with-zlib \ --enable-zip \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-ftp \ --enable-gd-native-ttf \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --without-pear \ --with-gettext \ --enable-session \ --with-curl \ --with-freetype-dir \ --enable-opcache \ --enable-fpm \ --enable-cgi \   # PHP 7.0以后,不再是enable-fastcgi这点需要注意 --with-fpm-user=web \ --with-fpm-group=web \ --without-gdbm \ --with-fpm-user=web \ --with-fpm-group=web \ --disable-fileinfo 
各位如果想要具体了解编译参数的信息,可以查看这个链接:http://php.net/manual/zh/configure.about.php。不过有些参数没在这个文档中找到,笔者通过shell命令查看了具体的参数列表,以下是参数查看命令: 
通过这个命令可以了解所有的配置参数,虽然是英文的。
编译安装 好了,如果没有错误的话,执行如下命令进行编译安装吧:
配置 复制php-fpm以及web的相关配置文件:
1 2 3 4 5 6 cd /home/web/php/etc cp php-fpm.conf.default php-fpm.conf cd /home/web/php/etc/php-fpm.d cp www.conf.default www.conf cd /root/src/php-7.1.12 cp php.ini-production /home/web/conf/php/php.ini 
启动 安装完成后,直接通过php-fpm启动:
1 /home/web/php/sbin/php-fpm 
安装MariaDB 这个就简单了,直接通过CentOS的yum进行安装:
1 2 yum install mariadb mariadb-server                    # 安装mariadb systemctl start mariadb && systemctl enable mariadb   # 设置开机启动 
安装成功后执行:
1 mysql_secure_installation 
根据提示,完成mysql初始化设置。
启动一下mysql -u root -pPASSWORD看下是否能够正常访问。
之后,我们需要配置php的mysql连接。之前,打开之前我们复制过来的php.ini文件,启用mysqli和pdo_mysql扩展。
1 2 extension =mysqli.soextension =pdo_mysql.so
需要注意:复制过来的php.ini文件是以windows为环境作为样例的,所以,不能直接将;去掉,需要使用linux的扩展文件类型.so。
之后,重启PHP-FPM
1 /home/web/php/sbin/php-fpm restart 
测试LNMP环境 OK,LNMP环境基本搭建完毕,接下来,我们测试下基础的环境。
配置nginx.conf文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 http {     include       mime.types;     default_type  application/octet-stream;     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                       '$status $body_bytes_sent "$http_referer" '                       '"$http_user_agent" "$http_x_forwarded_for"';     sendfile        on;     tcp_nopush      on;     tcp_nodelay     on;     keepalive_timeout  65;     client_header_timeout 30;     client_body_timeout 3m;     client_max_body_size 10m;     client_body_buffer_size 256k;     send_timeout 3m;     connection_pool_size 256;     client_header_buffer_size 32k;     large_client_header_buffers 4 64k;     request_pool_size 4k;     output_buffers 4 32k;     postpone_output 1460;     client_body_temp_path /tmp/nginx/client_body;     proxy_temp_path       /tmp/nginx/proxy;     fastcgi_temp_path     /tmp/nginx/fastcgi;     gzip  on;     gzip_min_length 1k;     gzip_buffers 4 16k;     gzip_comp_level 3;     gzip_http_version 1.1;     gzip_types text/plain application/x-javascript text/css text/htm application/xml;     gzip_vary on;     fastcgi_connect_timeout 300;     fastcgi_send_timeout 300;     fastcgi_read_timeout 300;     fastcgi_buffer_size 64k;     fastcgi_buffers 4 64k;     fastcgi_busy_buffers_size 128k;     fastcgi_temp_file_write_size 128k;     fastcgi_intercept_errors on;     server {         listen       80;         server_name  localhost;         charset UTF8;         #access_log  logs/host.access.log  main;         location / {             root   html;             index  index.html index.htm index.php index.jsp;         }         #error_page  404              /404.html;         # redirect server error pages to the static page /50x.html         #         error_page   500 502 503 504  /50x.html;         location = /50x.html {             root   html;         }         # proxy the PHP scripts to Apache listening on 127.0.0.1:80         #         #location ~ \.php$ {         #    proxy_pass   http://127.0.0.1;         #}         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000         #         location ~ \.php$ {             root           html;             fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;             fastcgi_index  index.php;             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;             include        fastcgi_params;         }         # deny access to .htaccess files, if Apache's document root         # concurs with nginx's one         #         location ~ /\.ht {             deny  all;         }     } } 
1 2 touch /home/web/nginx/html/info.php vim /home/web/nginx/html/info.php 
输入如下内容:
重启nginx
1 /home/web/nginx/sbin/nginx -c /home/web/conf/nginx/nginx.conf -s reload 
浏览器访问http://your-rasp-ip/  就可以看到php的信息界面了。
参考资料