为了账号安全,请及时绑定邮箱和手机立即绑定

ubuntu下mysql5.7+php7+nginx环境搭建

标签:
PHP

1 安装1.8.0版nginx

最新的稳定版本是1.8.0,官方网站推荐如下方法安装。(如果用ubuntu的源,安装得到的版本是1.4.6)

1.1 下载并添加nginx_signing.key

wget http://nginx.org/keys/nginx_signing.key 
sudo apt-key add nginx_signing.key

1.2 添加官方源

sudo vim /etc/apt/sources.list

在末尾加上:

deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx

然后更新源

sudo apt-get update

1.3 更新nginx

因为之前安装过ubuntu的源的nginx1.4.6,所以用upgrade。

sudo apt-get upgrade nginx

2 编译PHP7.0.0

参考文章How to install PHP 7…

2.1 下载

wget http://cn2.php.net/distributions/php-7.0.0.tar.bz2

2.2 安装编译依赖的库

sudo apt-get install libfcgi-dev libfcgi0ldbl libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-dev libxml2-dev libbz2-dev libcurl4-openssl-dev libjpeg-dev libpng12-dev libfreetype6-dev libkrb5-dev libpq-dev libxml2-dev libxslt1-dev

2.3 解决configure错误

解决configure错误:Cannot find imap library (libc-client.a). Please check your c-client installation.

sudo ln -s /usr/lib/libc-client.a /usr/lib/x86_64-linux-gnu/libc-client.a

2.4 configure & make

 ./configure --prefix=/opt/php-7.0.0 --with-pdo-pgsql --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl --with-mcrypt --with-zlib --with-gd --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-mysqli --with-pdo-mysql --with-mysqli --with-jpeg-dir=/usr --with-png-dir=/usr --enable-gd-native-ttf --with-openssl --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=/lib/x86_64-linux-gnu --enable-ftp --with-imap --with-imap-ssl --with-kerberos --with-gettext --with-xmlrpc --with-xsl --enable-opcache --enable-fpm
  • make

  • sudo make install

fengyh@vfyh-lab:~/tools/php-7.0.0$ sudo make install

[sudo] password for fengyh: 

exit 0; -t /home/fengyh/tools/php-7.0.0/ext/json/php_json_scanner_defs.h --no-generation-date -bci -o /home/fengyh/tools/php-7.0.0/ext/json/json_scanner.c /home/fengyh/tools/php-7.0.0/ext/json/json_scanner.re

Installing shared extensions:     /opt/php-7.0.0/lib/php/extensions/no-debug-non-zts-20151012/

Installing PHP CLI binary:        /opt/php-7.0.0/bin/

Installing PHP CLI man page:      /opt/php-7.0.0/php/man/man1/

Installing PHP FPM binary:        /opt/php-7.0.0/sbin/

Installing PHP FPM config:        /opt/php-7.0.0/etc/

Installing PHP FPM man page:      /opt/php-7.0.0/php/man/man8/

Installing PHP FPM status page:      /opt/php-7.0.0/php/php/fpm/

Installing phpdbg binary:         /opt/php-7.0.0/bin/

Installing phpdbg man page:       /opt/php-7.0.0/php/man/man1/

Installing PHP CGI binary:        /opt/php-7.0.0/bin/

Installing PHP CGI man page:      /opt/php-7.0.0/php/man/man1/

Installing build environment:     /opt/php-7.0.0/lib/php/build/

Installing header files:          /opt/php-7.0.0/include/php/

Installing helper programs:       /opt/php-7.0.0/bin/

  program: phpize

  program: php-config

Installing man pages:             /opt/php-7.0.0/php/man/man1/

  page: phpize.1

  page: php-config.1

Installing PEAR environment:      /opt/php-7.0.0/lib/php/

--2015-11-22 17:27:17--  https://pear.php.net/install-pear-nozlib.phar

Resolving pear.php.net (pear.php.net)... 5.77.39.20

Connecting to pear.php.net (pear.php.net)|5.77.39.20|:443... connected.

HTTP request sent, awaiting response... 200 OK

Length: 3579275 (3.4M) [text/plain]

Saving to: ‘pear/install-pear-nozlib.phar’

100%[=================================================>] 3,579,275   10.4KB/s   in 8m 58s 

2015-11-22 17:36:20 (6.50 KB/s) - ‘pear/install-pear-nozlib.phar’ saved [3579275/3579275]

[PEAR] Archive_Tar    - installed: 1.4.0

[PEAR] Console_Getopt - installed: 1.4.1

[PEAR] Structures_Graph- installed: 1.1.1

[PEAR] XML_Util       - installed: 1.3.0

[PEAR] PEAR           - installed: 1.10.1

Wrote PEAR system config file at: /opt/php-7.0.0/etc/pear.conf

You may want to add: /opt/php-7.0.0/lib/php to your php.ini include_path

/home/fengyh/tools/php-7.0.0/build/shtool install -c ext/phar/phar.phar /opt/php-7.0.0/bin

ln -s -f phar.phar /opt/php-7.0.0/bin/phar

Installing PDO headers:          /opt/php-7.0.0/include/php/ext/pdo/

2.5 拷贝php.ini和php-fpm.conf

拷贝php.ini和php-fpm.conf到正确的目录。

  • 编译后,cd到源码跟目录,然后sudo cp php.ini-production /opt/php7.0.0/lib/php.ini

  • cd /opt/php-7.0.0/etc,然后sudo cp php-fpm.conf.default php-fpm.conf

  • cd /opt/php-7.0.0/etc/php-fpm.d,然后sudo cp www.conf.default www.conf

3 拷贝PHP7.0.0到云端使用

本地同样的系统,安装的目录是/opt/php7.0.0,打包拷贝到云端同样的目录

4 mysql更新

按照mysql官网的指南A Quick Guide to Using the MySQL APT Repository ,卸载原来的旧版本的mysql,安装最新的5.7。- 安装的时候配置mysql版本并apply,或者事后使用sudo dpkg-reconfigure mysql-apt-config配置

发现5.7.9还有问题

mysql.user缺字段,导致grant命令报错。  mysqldump也报错。  text字段长度不够,同样的长度在mysql5.5没有问题

由于重新安装5.6会有问题,启动不了Mysql5.6。所以重新恢复服务器快照到干净实例。

4.1 下载和安装MySQL APT Repository

wget http://dev.mysql.com/get/mysql-apt-config_0.5.3-1_all.debsudo dpkg -i mysql-apt-config_0.5.3-1_all.deb

4.2 确认版本,或者更改版本:

sudo dpkg-reconfigure mysql-apt-config

4.3 更新源并安装mysql-server

sudo apt-get updatesudo apt-get install mysql-server

4.4 重启系统

发现刚安装完毕mysql服务器启动失败, 需要重新启动。

5 配置nginx+php7+mysql

  • php-fpm.conf的pid

sudo vim /opt/php-7.0.0/etc/php-fpm.conf

去掉语句pid = run/php-fpm.pid前的分号注释。

[global]; Pid file; Note: the default prefix is /opt/php-7.0.0/var; Default Value: nonepid = run/php-fpm.pid
  • www.conf中的listen端口,默认9000,可以修改

listen = 127.0.0.1:9000
  • 写service启动php-fpm的脚本/etc/init.d/php-7.0.0-fpm

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-7.0.0-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-7.0.0-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php-7.0.0/sbin/php-fpm
php_fpm_CONF=/opt/php-7.0.0/etc/php-fpm.conf
php_fpm_PID=/opt/php-7.0.0/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"
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 $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-exit"
                        exit 1
                else
                        echo " done"
                       echo " done"
                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}"
                exit 1
        ;;
esac

5.1 update-rc.d使脚本生效

$ sudo chmod 755 /etc/init.d/php-7.0.0-fpm
$ sudo update-rc.d php-7.0.0-fpm defaults 
 Adding system startup for /etc/init.d/php-7.0.0-fpm ...
   /etc/rc0.d/K20php-7.0.0-fpm -> ../init.d/php-7.0.0-fpm
   /etc/rc1.d/K20php-7.0.0-fpm -> ../init.d/php-7.0.0-fpm
   /etc/rc6.d/K20php-7.0.0-fpm -> ../init.d/php-7.0.0-fpm
   /etc/rc2.d/S20php-7.0.0-fpm -> ../init.d/php-7.0.0-fpm
   /etc/rc3.d/S20php-7.0.0-fpm -> ../init.d/php-7.0.0-fpm
   /etc/rc4.d/S20php-7.0.0-fpm -> ../init.d/php-7.0.0-fpm
   /etc/rc5.d/S20php-7.0.0-fpm -> ../init.d/php-7.0.0-fpm
$ sudo service php-7.0.0-fpm start

5.2 配置虚拟站点

配置虚拟站点www.geiliedu.com:* cd /etc/nginx/conf.d* cp default.conf www.geiliedu.com.conf* 然后将www.geiliedu.com.conf配置成如下的样子

server {
    listen       80;
    server_name  www.geiliedu.com;
    charset utf-8;
    access_log  /var/log/nginx/log/www.geiliedu.com.access.log  main;
    location / {
        root   /home/fengyh/web/geiliedu/frontend/web;
        index  index.php index.html index.htm;
        if (!-e $request_filename){ 
                rewrite ^/(.*) /index.php last;
        }  
    }
    error_page  404              /home/fengyh/web/geiliedu/404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /home/fengyh/web/geiliedu/50x.html;
    location = /50x.html {
        root   /home/fengyh/web/geiliedu;
    }
    # 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   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/fengyh/web/geiliedu/frontend/web$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;
    #}
}
  • 配置php.ini,指出pdo.mysql的socket位置

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

6 部署www.geiliedu.com

  • init,选production

  • mysql -u root -p,然后创建数据库geiliedu_docs

  • grant all privileges on geiliedu_docs.*to ‘geiliedu_docs’@’localhost’ identified by ‘…密码…’;

  • 发现上述语句在5.7中有问题,需要在mysql.user表中添加这个字段:

alter table user add `password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N';
  • 修改common/config/main-local.php的数据库配置

  • migrations

  • ./yii migrate - yii2模板自动的migrations

  • ./yii migrate --migrationPath=@fengyh/mdmgr/migrations - yii2-mdmgr的migrations

7 在php5.6以上版本swiftmailer的问题

由于php5.6、php7对完全有所加强,所以导致一些库出‘Bug’

stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

以下解决方法下策,添加红框内的语句。有替代方案还是要尽量改进:

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消