操作系统:linux Mint 18.2

编译安装php7.2


1.先安装编译工具
    sudo apt install gcc  automake autoconf libtool g++ 
    #在centos下,是yum install gcc automake autoconf libtool gcc-c++
2.安装基础库
    sudo apt install libxml2-dev openssl libssl-dev curl libcurl4-gnutls-dev libjpeg-dev libpng12-dev libfreetype6 libfreetype6-dev libmcrypt4 libmcrypt-dev
    #在centos下:yum install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel
2.下载php7.2.tar.gz源码
3.解压 tar -xzvf php7.2.tar.gz

4.编译安装

   ./configure --prefix=/usr/local/php7/ \
--with-pcre-regex=/opt/pcre-8.39/ \
--with-pcre-dir=/opt/pcre-8.39/ \
--with-openssl-dir=/opt/openssl-1.0.2h/ \
--with-openssl=/opt/openssl-1.0.2h/ \
--with-zlib-dir=/opt/zlib-1.2.8/ \
--with-zlib=/opt/zlib-1.2.8/ \
--with-curl=/opt/curl-7.50.1/ \
--with-mcrypt=/opt/libmcrypt-2.5.7/ \
--with-bz2 \
--with-ldap=/opt/openldap-2.4.30/ \
--with-gd \
--with-pdo-mysql=shared,mysqlnd \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libxml-dir \
--with-mhash \
--with-zlib-dir \
--without-pdo-sqlite \
--with-pear \
--with-xmlrpc \
--with-xsl \
--enable-opcache \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-zip \
--enable-ftp \
--enable-fpm \
--enable-mbstring \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-calendar \
--enable-fpm \
--enable-pcntl \
--enable-shmop \
--enable-exif \
--enable-pcntl \
--enable-wddx \
--enable-intl \
--enable-libgcc
./buildconf
./configure --prefix=/usr/local/php7 --exec-prefix=/usr/local/php7 --bindir=/usr/local/php7/bin --sbindir=/usr/local/php7/sbin --includedir=/usr/local/php7/include --libdir=/usr/local/php7/lib/php --mandir=/usr/local/php7/php/man --with-config-file-path=/usr/local/php7/etc --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mcrypt=/usr/include --with-mhash --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=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-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-openssl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www --without-gdbm --enable-fileinfo --enable-sysvsem  --enable-sysvshm  --enable-sysvmsg --enable-pcntl --enable-maintainer-zts --enable-pthreads



configure: WARNING: unrecognized options: --with-mcrypt, --with-mhash--with-openssl, --enable-gd-native-ttf, --enable-pthreads

Please check your libxml2 installation

然后修改fpm配置文件
cd /usr/local/php7/etc
sudo cp php-fpm.conf.default php-fpm.conf
但是执行php-fpm时报错:
../sbin/php-fpm
[23-Jul-2017 15:24:07] WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf' from /usr/local/php7/etc/php-fpm.conf at line 125.
[23-Jul-2017 15:24:07] ERROR: failed to open error_log (/usr/local/php7/var/log/php-fpm.log): Permission denied (13)
[23-Jul-2017 15:24:07] ERROR: failed to post process the configuration
[23-Jul-2017 15:24:07] ERROR: FPM initialization failed

添加用户和用户组(ubuntu,mint)
sudo addgroup www
sudo adduser www
对deepin 来说 sudo adduser www 就可以创建www用户和www用户组了。
然后改变php目录的用户和用户组
sudo chown -R www:www /usr/local/php7/
拷贝源码中的php.ini-develop文件到/usr/local/php7/etc/php.ini
把/usr/local/php7/etc/php-fpm.conf.default 复制为php-fpm.conf
把/usr/local/php7/etc/php-fpm.d/www.conf.default 复制为www.conf

然后再执行php-fpm
sudo ./php-fpm
执行成功,ps axu | grep php如下:

wm@wm-Lenovo /usr/local/php7/sbin $ ps axu | grep php
root     11492  0.0  0.1 177576 11420 ?        Ss   15:40   0:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
www      11493  0.0  0.1 177576  8748 ?        S    15:40   0:00 php-fpm: pool www
www      11494  0.0  0.1 177576  8744 ?        S    15:40   0:00 php-fpm: pool www
wm       11496  0.0  0.0  15964  1016 pts/1    S+   15:41   0:00 grep --color=auto php

nginx编译安装
1.下载nginx源码,并解压
2.下载zlib和pcre源码,并解压。
3.configure的参数,参考nginx官方

./configure
    --sbin-path=/usr/local/nginx/nginx
    --conf-path=/usr/local/nginx/nginx.conf
    --pid-path=/usr/local/nginx/nginx.pid
    --with-http_ssl_module
    --with-pcre=../pcre-8.40
    --with-zlib=../zlib-1.2.11
    --user=www
    --group=www

make && make install
与php安装一样,也修改/usr/local/nginx的用户和用户组为www:www

mysql编译安装

https://dev.mysql.com/downloads/mysql/
https://dev.mysql.com/downloads/file/?id=471659
-- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19.tar.gz
编译安装需要参考如下:
https://dev.mysql.com/doc/refman/5.7/en/source-installation.html
https://dev.mysql.com/doc/refman/5.7/en/installing-source-distribution.html

http://www.cnblogs.com/goozgk/p/5645041.html
# mkdir -p /usr/local/mysql /mydata/data
# groupadd -r mysql
# useradd -g mysql -r -d /data/mydata mysql
# chown -R mysql.mysql /usr/local/mysql /mydata/data

# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
# tar xf mysql-boost-5.7.13.tar.gz
# cd mysql-5.7.13
# yum install -y openssl openssl-devel

# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/mydata/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_BOOST=/home/alex/Downloads/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
如果编译出现各种奇葩失败问题,干脆 sudo apt-get install mysql-server 得了。

localhost 和127.0.0.1的问题
连接MySQL数据库有两种方式:TCP/IP(一般理解的端口的那种)和Unix套接字(一般叫socket或者sock)。大部分情况下,可以用localhost代表本机127.0.0.1,但是在MySQL连接时,二者不可混用,而且MySQL中权限设置中localhost与127.0.0.1也是分开设置的。当设置为127.0.0.1时,系统通过TCP/IP方式连接数据库;当设置为localhost时,系统通过socket方式连接数据库。
解决:首先要看本机MySQL的socket套接字文件在哪里,查看命令是:
mysqld --verbose --help | grep socket
输出结果显示套接字文件的位置,比如:这台服务器显示的是
socket /var/run/mysqld/mysqld.sock
然后修改php的配置文件php.ini与之对应起来就好了。
找到这一项:
mysql.default_socket =
一般来说这一项都是空的,改成:
mysql.default_socket = /var/run/mysqld/mysqld.sock
这里应写上一步查询到的文件,根据你的情况设置。至此php配置就修改好了,如果是CLI(命令行)方式或者CGI方式的话,立即就生效,如果是FASTCGI方式,需要重启一下fastcgi进程。

php-fpm服务启动脚本
先创建自启动文件:/etc/init.d/php-fpm
内容如下:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/usr/local/php
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


wait_for_pid () {
    try=0

    while test $try -lt 35 ; do

        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;

            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac

        echo -n .
        try=`expr $try + 1`
        sleep 1

    done

}

case "$1" in
    start)
        echo -n "Starting php-fpm "

        $php_fpm_BIN --daemonize $php_opts

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi

        wait_for_pid created $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Gracefully shutting down php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -QUIT `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    status)
        if [ ! -r $php_fpm_PID ] ; then
            echo "php-fpm is stopped"
            exit 0
        fi

        PID=`cat $php_fpm_PID`
        if ps -p $PID | grep -q $PID; then
            echo "php-fpm (pid $PID) is running..."
        else
            echo "php-fpm dead but pid file exists"
        fi
    ;;

    force-quit)
        echo -n "Terminating php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -TERM `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    reload)

        echo -n "Reload service php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -USR2 `cat $php_fpm_PID`

        echo " done"
    ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
        exit 1
    ;;

esac

修改php-fpm的权限,设置启动

# 设置权限
chmod 755 /etc/init.d/php-fpm
# php-fpm加入服务
chkconfig --add php-fpm 
# php-fpm 234级别下设置为启动
chkconfig php-fpm on
# 查看php-fpm服务当前配置
chkconfig --list php-fpm
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

如果是ubuntu下( chkconfig 已经不能在ubuntu下使用,应该:)
# 设置权限
chmod 755 /etc/init.d/php-fpm
sudo update-rc.d php-fpm defaults

// 添加
update-rc.d ServiceName defaults
// 删除
update-rc.d ServiceName remove

最后应该是sudo service php-fpm force-stop一下,不然直接service php-fpm restart 可能会报错:

service 控制nginx

#!/bin/sh  
  
### BEGIN INIT INFO  
# Provides:    nginx  
# Required-Start:  
# Required-Stop:  
# Default-Start:        2 3 4 5  
# Default-Stop:        0 1 6  
# Short-Description: nginx  
# Description: nginx server  
### END INIT INFO  
  
. /lib/lsb/init-functions  # ubuntu
# . /etc/lib/functions # centos
  
PROGRAM=/usr/local/nginx/nginx  
  
  
test -x $PROGRAM || exit 0  
  
case "$1" in  
  start)  
    log_begin_msg "Starting Nginx server"  
    /usr/local/nginx/nginx  
    log_end_msg 0  
    ;;  
  stop)  
    PID=`cat /usr/local/nginx/nginx.pid`  
    log_begin_msg "Stopping Nginx server"  
    if [ ! -z "$PID" ]; then  
        kill -15 $PID  
    fi  
    log_end_msg 0  
    ;;  
  restart)  
    $0 stop  
    $0 start  
    ;;  
  *)  
    log_success_msg "Usage: service nginx {start|stop|restart}"  
    exit 1  
esac  
  
exit 0 

helloworldcoding
172 声望15 粉丝

no easy day.