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

1 安裝1.8.0版nginx

最新的穩定版本是1.8.0,官方網站推薦以下方法安裝。(若是用ubuntu的源,安裝獲得的版本是1.4.6)php

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

在末尾加上:html

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

而後更新源mysql

sudo apt-get update

1.3 更新nginx

由於以前安裝過ubuntu的源的nginx1.4.6,因此用upgrade。linux

sudo apt-get upgrade nginx

2 編譯PHP7.0.0

參考文章How to install PHP 7…nginx

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.web

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
  • makesql

  • 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到正確的目錄。json

  • 編譯後,cd到源碼跟目錄,而後sudo cp php.ini-production /opt/php7.0.0/lib/php.iniubuntu

  • 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.deb
sudo 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 update
sudo 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: none
pid = 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.六、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

如下解決方法下策,添加紅框內的語句。有替代方案仍是要儘可能改進:pic

相關文章
相關標籤/搜索