越简单越好!

树莓派CentOS-RaspberryPi3系统下编译安装php7+nginx环境

发表于 2017-12-28 15:45 | 2030次阅读 1次点赞   树莓派
1. 安装依赖包
[root@rpi3 ~]# yum -y install gc gcc gcc++ gcc-c++ wget make libxml2  libxml2-devel  openssl  openssl-devel  curl-devel  libjpeg-devel  libpng-devel freetype-devel bison autoconf openldap-devel pcre-devel zlib-devel
加密支持扩展库libmcrypt
[root@rpi3 ~]# wget -c https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
[root@rpi3 ~]# tar -zxvf libmcrypt-2.5.8.tar.gz
[root@rpi3 ~]# cd libmcrypt-2.5.8/
[root@rpi3 libmcrypt-2.5.8]# ./configure
[root@rpi3 libmcrypt-2.5.8]# make && make install
加密方式扩展库mhash
[root@rpi3 ~]# cd ~/
[root@rpi3 ~]# wget -c https://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
[root@rpi3 ~]# tar -zxvf mhash-0.9.9.9.tar.gz 
[root@rpi3 ~]# cd mhash-0.9.9.9/
[root@rpi3 mhash-0.9.9.9]# ./configure
[root@rpi3 mhash-0.9.9.9]# make && make install
加密方式扩展库mcrypt
[root@rpi3 ~]# cd ~/
[root@rpi3 ~]# wget -c https://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
[root@rpi3 ~]# tar -zxvf mcrypt-2.6.8.tar.gz
[root@rpi3 ~]# cd mcrypt-2.6.8
[root@rpi3 mcrypt-2.6.8]# ./configure
[root@rpi3 mcrypt-2.6.8]# make && make install
如果在安装 mhash 或 mcrypt 出现类似以下错误:
*** Could not run libmcrypt test program, checking why...
*** The test program compiled, but did not run. This usually means
*** that the run-time linker is not finding LIBMCRYPT or finding the wrong
*** version of LIBMCRYPT. If it is not finding LIBMCRYPT, you'll need to set your
*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point
*** to the installed location  Also, make sure you have run ldconfig if that
*** is required on your system
*** If you have an old version installed, it is best to remove it, although
*** you may also be able to get things to work by modifying LD_LIBRARY_PATH
configure: error: *** libmcrypt was not found
加上:
[root@rpi3 ~]# export LD_LIBRARY_PATH=/usr/local/lib
或者
[root@rpi3 ~]# vi /etc/ld.so.conf.d/local.conf
添加 /usr/local/lib 
使之生效
[root@rpi3 ~]# ldconfig -v 

2. 安装php7.1
下载php7.1并解压安装
[root@rpi3 ~]# wget -c https://github.com/php/php-src/archive/php-7.1.12.tar.gz
[root@rpi3 ~]# tar -zxvf php-7.1.12.tar.gz
[root@rpi3 ~]# cd php-src-php-7.1.12/
[root@rpi3 php-src-php-7.1.12]# ./buildconf --force
[root@rpi3 php-src-php-7.1.12]# ./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include  --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --with-mysql-sock=/var/run/mysql/mysql.sock --with-mcrypt --with-mhash --with-openssl --with-mysqli=shared,mysqlnd --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-sysvsem --enable-mbstring --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --without-gdbm --enable-fast-install --disable-fileinfo
具体的扩展可以根据自己的情况选择安装
./configure \
--prefix=/usr/local/php \                               [php安装的根目录]
--exec-prefix=/usr/local/php \                          [php执行文件所在目录]
--bindir=/usr/local/php/bin \                           [php/bin目录]
--sbindir=/usr/local/php/sbin \                         [php/sbin目录]
--includedir=/usr/local/php/include \                   [php包含文件所在目录]
--libdir=/usr/local/php/lib/php \                       [php/lib目录]
--mandir=/usr/local/php/php/man \                       [php/man目录]
--with-config-file-path=/usr/local/php/etc \            [php的配置目录]
--with-mysql-sock=/var/run/mysql/mysql.sock \           [php的Unix socket通信文件]
--with-mcrypt \                                         [是php里面重要的加密支持扩展库,linux环境下该库在默认情况下不开启]
--with-mhash \                                          [Mhash是基于离散数学原理的不可逆向的php加密方式扩展库,其在默认情况下不开启]
--with-openssl \                                        [OpenSSL 是一个安全套接字层密码库]
--with-mysqli=shared,mysqlnd \                          [php依赖mysql库]
--with-pdo-mysql=shared,mysqlnd \                       [php依赖mysql库]
--with-gd \                                             [gd库]                                               
--with-iconv \                                          [关闭iconv函数,种字符集间的转换]                        
--with-zlib \                                           [zlib是提供数据压缩用的函式库]
--enable-zip \                                          [打开对zip的支持]
--enable-inline-optimization \                          [优化线程]
--disable-debug \                                       [关闭调试模式]
--disable-rpath \                                       [关闭额外的运行库文件]
--enable-shared \                                       [启用动态库]
--enable-xml \                                          [开启xml扩展]
--enable-bcmath \                                       [打开图片大小调整,用到zabbix监控的时候用到了这个模块]
--enable-shmop \                                        [共享内存]
--enable-sysvsem \                                      [内存共享方案]
--enable-mbregex \                                      [开启多字节正则表达式的字符编码。]
--enable-mbstring \                                     [开启多字节字符串函数]
--enable-ftp \                                          [开启ftp]
--enable-gd-native-ttf \                                [开启gd库原有字体]
--enable-pcntl \                                        [PHP的进程控制支持实现了Unix方式的多进程创建]     
--enable-sockets \                                      [开启套节字]
--with-xmlrpc \                                         [打开xml-rpc的c语言]
--enable-soap \                                         [开启简单对象访问协议简单对象访问协议]
--without-pear \                                        [开启php扩展与应用库]
--with-gettext \                                        [开户php在当前域中查找消息]
--enable-session \                                      [允许php会话session]
--with-curl \                                           [允许curl扩展]
--with-jpeg-dir \                                       [指定jpeg安装目录yum安装过后不用再次指定会自动找到]
--with-freetype-dir \                                   [指定freetype安装目录yum安装过后不用再次指定会自动找到]
--enable-opcache \                                      [开启使用opcache缓存]
--enable-fpm \                                          [开启fpm]
--with-fpm-user=nginx \                                 [php-fpm的用户]
--with-fpm-group=nginx \                                [php-fpm的用户组]
--without-gdbm \                                        [数据库函数使用可扩展散列和类似于标准UNIX dbm的工作]
--enable-fast-install \                                 [为快速安装优化]
--disable-fileinfo
[root@rpi3 php-src-php-7.1.12]# make && make install

配置php
[root@rpi3 php-src-php-7.1.12]# cp php.ini-production /usr/local/php/etc/php.ini 
[root@rpi3 php-src-php-7.1.12]# vi /usr/local/php/etc/php.ini
这里需要配置时间区
date.timezone = PRC
配置扩展目录
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/"
增加扩展
extension=mysqli.so
extension=pdo_mysql.so

添加php安装目录到系统环境变量
[root@rpi3 php-src-php-7.1.12]# vi /etc/profile.d/php.sh
内容:
export PATH=$PATH:/usr/local/php/bin/:/usr/local/php/sbin/
使用source立即生效刚刚添加的php环境变量
[root@rpi3 php-src-php-7.1.12]# source /etc/profile.d/php.sh

配置php-fpm
[root@rpi3 php-src-php-7.1.12]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@rpi3 php-src-php-7.1.12]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@rpi3 php-src-php-7.1.12]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

创建php-fpm服务启动脚本
[root@rpi3 php-src-php-7.1.12]# /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
[Install]
WantedBy=multi-user.target
添加开机启动并启动服务
[root@rpi3 ~]# systemctl enable php-fpm
[root@rpi3 ~]# systemctl start php-fpm

安装redis扩展(根据自己情况可选安装)
[root@rpi3 ~]# wget -c https://github.com/phpredis/phpredis/archive/3.1.5.tar.gz
[root@rpi3 ~]# tar -zxvf 3.1.5.tar.gz
[root@rpi3 ~]# cd phpredis-3.1.5/
[root@rpi3 phpredis-3.1.5]# phpize
[root@rpi3 phpredis-3.1.5]# ./configure 
[root@rpi3 phpredis-3.1.5]# make && make install
在php.ini里增加 extension=redis.so 重启服务就ok了

安装swoole扩展(根据自己情况可选安装)
[root@rpi3 ~]# cd ~/
[root@rpi3 ~]# wget -c https://github.com/swoole/swoole-src/archive/v1.9.22.tar.gz
[root@rpi3 ~]# tar -zxvf v1.9.22.tar.gz
[root@rpi3 ~]# cd swoole-src-1.9.22/
[root@rpi3 swoole-src-1.9.22]# phpize
[root@rpi3 swoole-src-1.9.22]# ./configure
[root@rpi3 swoole-src-1.9.22]# make && make install
在php.ini里增加 extension=swoole.so 重启服务就ok了

3. 安装nginx
创建用户与群组
[root@rpi3 ~]# groupadd nginx
[root@rpi3 ~]# useradd -s /sbin/nologin -g nginx -M nginx

下载并解压
[root@rpi3 ~]# wget -c http://nginx.org/download/nginx-1.13.8.tar.gz
[root@rpi3 ~]# tar -zxvf nginx-1.13.8.tar.gz
[root@rpi3 ~]# cd nginx-1.13.8
[root@rpi3 nginx-1.13.8]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module 
#指定运行权限的用户
--user=nginx
#指定运行的权限用户组
--group=nginx
#指定安装路径
--prefix=/usr/local/nginx
#支持nginx状态查询
--with-http_stub_status_module
#开启ssl支持
--with-http_ssl_module
#开启GZIP功能
--with-http_gzip_static_module
[root@rpi3 nginx-1.13.8]# make && make install

创建nginx服务启动脚本
[root@rpi3 nginx-1.13.8]# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

添加开机启动并启动服务
[root@rpi3 nginx-1.13.8]# systemctl enable nginx
[root@rpi3 nginx-1.13.8]# systemctl start nginx

nginx的配置文件在:/usr/local/nginx/conf/nginx.conf 可以做相关的nginx配置

返回顶部 ^