這個週末上線一個幫朋友做的網站,遇到挺多坑,記錄一下

Vick發表於2018-05-07

購買伺服器,目前選擇的是阿里雲伺服器,選擇的是入門型1核1G例項

  • 目前沒什麼活動,三年1400軟妹幣

配置SSH連線

  • 增加本機ssh連線配置,一般啟用例項後,ssh的22埠是預設開放的,可以直接透過root使用者進行登入配置部署環境
  • 登入到伺服器後,將自己的公鑰加入到 ~/.ssh/authorized_keys 配置檔案中就可直接透過秘鑰進行登入

伺服器配置

  • 伺服器系統版本:CentOS Linux release 7.4.1708 (Core)
  • 記憶體:1G
  • CPU:1核
  • 硬碟:40G

安裝Mysql5.5

因為伺服器配置有點低,所以這邊選擇安裝比較低的mysql版本。從CentOS 7.0釋出以來,yum源中開始使用mariadb來代替MySQL的安裝。即使你輸入的是yum install mysql , 顯示的也是mariadb的安裝內容,因此,如果使用yum安裝MySQL的話,就需要去下載官方指定的yum源。
網址: https://dev.mysql.com/downloads/repo/yum/

先解除安裝mariadb,檢視mariadb是否已經安裝

  [root@iZbp17dq2xryqoixibq5u1Z ~]#  yum list installed | grep mariadb
  mariadb-libs.x86_64                     1:5.5.56-2.el7                 @anaconda

進行解除安裝

  [root@iZbp17dq2xryqoixibq5u1Z ~]# yum -y remove mariadb*
  Loaded plugins: fastestmirror
  Resolving Dependencies
  ......    **省略過程**

  Removed:
    mariadb-libs.x86_64 1:5.5.56-2.el7

  Dependency Removed:
    postfix.x86_64 2:2.10.1-6.el7                                                                redhat-lsb-core.x86_64 0:4.1-27.el7.centos.1

  Complete!

獲取yum源和資料庫安裝(官方指南

  wget http://repo.mysql.com/yum/mysql-5.5-community/el/6/x86_64/mysql-community-release-el6-5.noarch.rpm   #MySQL5.5yum源

安裝yum源

  rpm -ivh mysql-5.5-community/el/6/x86_64/mysql-community-release-el6-5.noarch.rpm 

安裝好yum源後,需要修改一下配置檔案,檔案路徑在 /etc/yum.repos.d/mysql-community.repo,要將5.5的enabled改為1,而5.6的enabled改為0

  # Enable to use MySQL 5.5
  [mysql55-community]
  name=MySQL 5.5 Community Server
  baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/6/$basearch/
  enabled=1
  gpgcheck=1
  gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

  # Enable to use MySQL 5.6
  [mysql56-community]
  name=MySQL 5.6 Community Server
  baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
  enabled=0
  gpgcheck=1
  gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

  # Note: MySQL 5.7 is currently in development. For use at your own risk.
  # Please read with sub pages: https://dev.mysql.com/doc/relnotes/mysql/5.7/en/
  [mysql57-community-dmr]
  name=MySQL 5.7 Community Server Development Milestone Release
  baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
  enabled=0
  gpgcheck=1
  gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

修改後儲存退出,開始安裝MySQL。在安裝之前,可以檢視下是否已有MySQL可安裝檔案

  [root@iZbp17dq2xryqoixibq5u1Z ~]# yum repolist enabled | grep "mysql.*-community.*"
  mysql-connectors-community/x86_64 MySQL Connectors Community                  49
  mysql-tools-community/x86_64      MySQL Tools Community                       61
  mysql55-community/x86_64          MySQL 5.5 Community Server                 449

安裝mysql

  # 安裝client,devel,server
  yum install mysql-community-client mysql-community-devel mysql-community-server

安裝完畢後,可以檢視下當前mysql版本

  [root@iZbp17dq2xryqoixibq5u1Z ~]# rpm -qi mysql-community-server
  Name        : mysql-community-server
  Version     : 5.5.60
  Release     : 2.el6
  Architecture: x86_64
  Install Date: 2018年05月05日 星期六 16:26:00
  ......

啟動mysql

執行 service mysqld start 啟動mysql

  [root@iZbp17dq2xryqoixibq5u1Z ~]# service mysqld start
  Starting mysqld (via systemctl):                           [  OK  ]

ok,mysql安裝完畢

常用命令:

systemctl start mysqld #啟動mysqld

systemctl stop mysqld #停止mysqld

systemctl restart mysqld #重啟mysqld

systemctl enable mysqld #設定開機啟動

systemctl status mysqld #檢視 MySQL Server 狀態

資料庫安全設定
設定mysql root賬戶密碼

  # mysqladmin -u root password 'new password'

重新登入mysql報如下錯誤

  ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

執行如下命令進行解決

  [root@iZbp17dq2xryqoixibq5u1Z ~]# systemctl stop mysqld       **關閉mysql服務**
  [root@iZbp17dq2xryqoixibq5u1Z ~]# mysqld --user=root --skip-grant-tables &        **關閉skip-grant-tables**
  [1] 32163
  [root@iZbp17dq2xryqoixibq5u1Z ~]# 180505 16:57:08 [Note] mysqld (mysqld 5.5.60) starting as process 32163 ...
  180505 16:57:08 [Note] Plugin 'FEDERATED' is disabled.
  180505 16:57:08 InnoDB: The InnoDB memory heap is disabled
  180505 16:57:08 InnoDB: Mutexes and rw_locks use GCC atomic builtins
  180505 16:57:08 InnoDB: Compressed tables use zlib 1.2.3
  180505 16:57:08 InnoDB: Using Linux native AIO
  180505 16:57:08 InnoDB: Initializing buffer pool, size = 128.0M
  180505 16:57:08 InnoDB: Completed initialization of buffer pool
  180505 16:57:08 InnoDB: highest supported file format is Barracuda.
  180505 16:57:08  InnoDB: Waiting for the background threads to start
  180505 16:57:09 InnoDB: 5.5.60 started; log sequence number 1595675
  180505 16:57:09 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
  180505 16:57:09 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
  180505 16:57:09 [Note] Server socket created on IP: '0.0.0.0'.
  180505 16:57:09 [Note] mysqld: ready for connections.
  Version: '5.5.60'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL)
  mysql -u root mysql       **空密碼登入**
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A

  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 1
  Server version: 5.5.60 MySQL Community Server (GPL)

  Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.

  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  mysql> UPDATE user SET authentication_string=PASSWORD('new password') where USER='root';      **重新設定root密碼**
  Query OK, 4 rows affected (0.00 sec)
  Rows matched: 4  Changed: 4  Warnings: 0

  mysql> FLUSH PRIVILEGES;      **重新整理設定**
  Query OK, 0 rows affected (0.00 sec)

  mysql> quit
  Bye

安裝PHP環境

開始安裝PHP和PHP-FPM
首先安裝EPEL。EPEL即Extra Packages for Enterprise Linux的簡稱,是為企業級Linux提供的一組高質量的額外軟體包

  yum -y install epel-release

安裝PHP和PHP-FPM

  yum -y install php php-fpm

檢視PHP版本

  [root@iZbp17dq2xryqoixibq5u1Z ~]# php -v
  PHP 5.4.16 (cli) (built: Mar  7 2018 13:34:47)
  Copyright (c) 1997-2013 The PHP Group
  Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

這時發現PHP版本是5.4的,原因是yum預設的epel-release源太低了,而我需要部署的網站是基於Laravel5.5開發的,環境要求:

  • PHP >= 7.0.0 需要重新安裝PHP7
  • PHP OpenSSL 擴充套件
  • PHP PDO 擴充套件
  • PHP Mbstring 擴充套件
  • PHP Tokenizer 擴充套件
  • PHP XML 擴充套件

刪除之前安裝的PHP版本

  yum remove php* php-common

我們需要更換下rpm源,搜尋epel-release源並刪除後進行更新

  [root@iZbp17dq2xryqoixibq5u1Z ~]# rpm -qa | grep epel
  epel-release-7-11.noarch
  [root@iZbp17dq2xryqoixibq5u1Z ~]# rpm -e epel-release-7-11.noarch
  warning: /etc/yum.repos.d/epel.repo saved as /etc/yum.repos.d/epel.repo.rpmsave
  [root@iZbp17dq2xryqoixibq5u1Z ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
  Retrieving https://mirror.webtatic.com/yum/el7/epel-release.rpm
  warning: /var/tmp/rpm-tmp.PHnPwl: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
  Preparing...                          ################################# [100%]
  Updating / installing...
     1:epel-release-7-5                 ################################# [100%]
  [root@iZbp17dq2xryqoixibq5u1Z ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  warning: /var/tmp/rpm-tmp.ohTozh: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
  Preparing...                          ################################# [100%]
  Updating / installing...
     1:webtatic-release-7-3             ################################# [100%]

重新安裝PHP和一些相關擴充套件

  yum install php72w-cli.x86_64 php72w-common.x86_64 php72w-gd.x86_64 php72w-ldap.x86_64 php72w-mbstring.x86_64 php72w-pdo.x86_64

安裝PHP-FPM

  yum install php72w-fpm

再次檢視PHP版本

  [root@iZbp17dq2xryqoixibq5u1Z ~]# php -v
  PHP 7.2.4 (cli) (built: Mar 30 2018 08:49:13) ( NTS )
  Copyright (c) 1997-2018 The PHP Group
  Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

啟動PHP-FPM

  systemctl start php-fpm
  systemctl enable php-fpm.service      **開機自啟動**

常用命令:

systemctl start php-fpm # 啟動

systemctl stop php-fpm # 停止

systemctl restart php-fpm # 重啟

systemctl enable php-fpm #開機自啟動

安裝Nginx

開始安裝Nginx

  yum install nginx

安裝完畢後,啟動Nginx

  systemctl start nginx
  systemctl enable nginx        **系統啟動時自動啟動Nginx**

常用命令:

fuser -k 80/tcp # 殺死80埠

nginx -s stop # 停止

nginx -s reopen # 重啟

nginx -s reload # 重新載入配置檔案

生成秘鑰

  ssh-keygen -t rsa

提示一直回車就行,將生成的秘鑰新增到專案託管的git庫網站上,因為我的網站是放在Coding上,我直接新增在了專案部署秘鑰

克隆專案

透過 git clone 命令將專案拉取到伺服器上,我這邊因為是Nginx,所以我拉取到 /var/www/ 目錄下,

安裝composer

  [root@iZbp17dq2xryqoixibq5u1Z project_fjylhjjsyxgs]# curl -sS https://getcomposer.org/installer | php
  All settings correct for using Composer
  Downloading...

  Composer (version 1.6.5) successfully installed to: /var/www/project_fjylhjjsyxgs/composer.phar
  Use it: php composer.phar

移動composer.phar檔案到/usr/local/bin目錄下 ,使命令全域性可用,並更換Packagist中國全量映象

  mv composer.phar /usr/local/bin/composer
  composer config -g repo.packagist composer https://packagist.phpcomposer.com

透過composer安裝專案依賴,但是出現了報錯

  [root@iZbp17dq2xryqoixibq5u1Z project_fjylhjjsyxgs]# composer install
  Loading composer repositories with package information
  Installing dependencies (including require-dev) from lock file
  Your requirements could not be resolved to an installable set of packages.

    Problem 1
      - Installation request for phar-io/manifest 1.0.1 -> satisfiable by phar-io/manifest[1.0.1].
      - phar-io/manifest 1.0.1 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    Problem 2
      - Installation request for phpunit/php-code-coverage 5.3.0 -> satisfiable by phpunit/php-code-coverage[5.3.0].
      - phpunit/php-code-coverage 5.3.0 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    Problem 3
      - Installation request for phpunit/phpunit 6.5.7 -> satisfiable by phpunit/phpunit[6.5.7].
      - phpunit/phpunit 6.5.7 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    Problem 4
      - Installation request for theseer/tokenizer 1.1.0 -> satisfiable by theseer/tokenizer[1.1.0].
      - theseer/tokenizer 1.1.0 requires ext-dom * -> the requested PHP extension dom is missing from your system.

    To enable extensions, verify that they are enabled in your .ini files:
      - /etc/php.ini
      - /etc/php.d/bz2.ini
      - /etc/php.d/calendar.ini
      - /etc/php.d/ctype.ini
      - /etc/php.d/curl.ini
      - /etc/php.d/exif.ini
      - /etc/php.d/fileinfo.ini
      - /etc/php.d/ftp.ini
      - /etc/php.d/gd.ini
      - /etc/php.d/gettext.ini
      - /etc/php.d/gmp.ini
      - /etc/php.d/iconv.ini
      - /etc/php.d/json.ini
      - /etc/php.d/ldap.ini
      - /etc/php.d/mbstring.ini
      - /etc/php.d/pdo.ini
      - /etc/php.d/pdo_sqlite.ini
      - /etc/php.d/phar.ini
      - /etc/php.d/shmop.ini
      - /etc/php.d/simplexml.ini
      - /etc/php.d/sockets.ini
      - /etc/php.d/sqlite3.ini
      - /etc/php.d/tokenizer.ini
      - /etc/php.d/xml.ini
      - /etc/php.d/zip.ini
    You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Google後發現,php-xml擴充套件有以上需要的相關軟體包

  yum install php72w-xml.x86_64

重新執行composer install命令,依賴下載成功

設定Nginx配置檔案

我本機homestead中的Nginx是透過 /etc/nginx/sites-available 目錄設定對應多域名的nginx配置檔案,但是不知道伺服器上安裝的Nginx,卻沒有這個目錄,所以檢視了下Nginx.conf檔案,發現http模組中有如下一條配置

    本機homestead中Nginx.conf
    ##
      # Virtual Host Configs
      ##
      include /etc/nginx/conf.d/*.conf;
      include /etc/nginx/sites-enabled/*;

      伺服器上安裝的Nginx.conf
    # Load modular configuration files from the /etc/nginx/conf.d directory.
      # See http://nginx.org/en/docs/ngx_core_module.html#include
      # for more information.
      include /etc/nginx/conf.d/*.conf;

那好吧,我估計sites-available也是軟連線的conf.d或者sites-enabled目錄吧,所以我就直接將官方手冊中Nginx配置複製了一份到/etc/nginx/conf.d/域名.conf,修改了一些配置

  server {
      listen 80;
      server_name 域名或公網IP;
      root 專案地址指向到public目錄;

      add_header X-Frame-Options "SAMEORIGIN";
      add_header X-XSS-Protection "1; mode=block";
      add_header X-Content-Type-Options "nosniff";

      index index.html index.htm index.php;

      charset utf-8;

      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }

      location = /favicon.ico { access_log off; log_not_found off; }
      location = /robots.txt  { access_log off; log_not_found off; }

      error_page 404 /index.php;

      location ~ \.php$ {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;       **這裡有個坑下面講**
          fastcgi_index index.php;
          include fastcgi_params;
      }

      location ~ /\.(?!well-known).* {
          deny all;
      }
  }

配置完畢,儲存退出,透過域名訪問,發現報502錯誤,檢視下Nginx的錯誤日誌,發現報如下錯誤

  2018/05/06 20:17:37 [crit] 5898#0: *66 connect() to unix:/var/run/php/php7.2-fpm.sock failed (2: No such file or directory) while connecting to upstream,

簡單的講下,php-fpm.sock檔案就是讓Nginx和PHP-FPM的程式間進行通訊的檔案,具體的含義,這邊就不做詳細介紹了。進這個目錄檢視下是否存在這個目錄或檔案,發現兩個問題:

  • 目錄錯誤,不是/var/run/php,而是/var/run/php-fpm
  • php7.2-fpm.sock這個檔案未生成

    未生成原因:php5.3之後的版本,php-fpm.conf裡的listen的預設配置是127.0.0.1:9000,也就是tcp的方式,不會生成php-fpm.sock。

    因為這次上線的網站沒有什麼併發量,unix socket方式要比tcp的方式快而且消耗資源少,所以我還是採用unix socket方式。定位到問題後,修改下 /etc/php-fpm.d/www.conf,也就是php-fpm的配置檔案,關閉原來的listen方式,然後重啟下php-fpm

    ;listen = 127.0.0.1:9000
    listen = /var/run/php-fpm/php7.2-fpm.sock

    這個時候,由於職業習慣,想把Nginx也重啟下,然後就又碰到了一個問題

    [root@iZbp17dq2xryqoixibq5u1Z project_fjylhjjsyxgs]# nginx -s stop
    [root@iZbp17dq2xryqoixibq5u1Z project_fjylhjjsyxgs]# nginx -s reload
    nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)

    這應該是因為把Nginx程式殺死後pid丟失了,下一次再開啟nginx -s reload時無法啟動。再次面向Google程式設計,發現還有挺多人碰到這個問題,所以解決方法馬上就能搜尋到了,執行如下命令

    nginx -c /etc/nginx/nginx.conf
    nginx -s reload

    ok,接著Navicat遠端連線到伺服器資料庫,source下資料庫完畢。現看起來應該是可以跑起網站來了。再次訪問域名,發現還是502,再次檢視Nginx日誌,報如下錯誤:

    2018/05/06 20:37:00 [crit] 6078#0: *1 connect() to unix:/var/run/php-fpm/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream,

和上面報錯看起來差不多,但是這次主要問題出在跑Nginx的使用者是Nginx,而php-fpm.sock這個檔案,監聽的Nginx使用者沒有該許可權,導致Nginx無法訪問php-fpm.sock這個檔案,自然監聽就失去了效果,再次修改php-fpm配置檔案和重啟下php-fpm

  ; Set permissions for unix socket, if one is used. In Linux, read/write
  ; permissions must be set in order to allow connections from a web server. Many
  ; BSD-derived systems allow connections regardless of permissions.
  ; Default Values: user and group are set as the running user
  ;                 mode is set to 0660
  listen.owner = nginx
  listen.group = nginx
  listen.mode = 0660

重新訪問下域名,發現訪問任何路由都是白屏,這次狀態碼都是200了,但是沒有任何輸出,再次面向Google程式設計……發現問題:

由於nginx與php-fpm之間的一個小bug,會導致這樣的現象: 網站中的靜態頁面 .html 都能正常訪問,而 .php 檔案雖然會返回200狀態碼, 但實際輸出給瀏覽器的頁面內容卻是空白。 簡而言之,原因是nginx無法正確的將 *.php 檔案的地址傳遞給php-fpm去解析, 相當於php-fpm接受到了請求,但這請求卻指向一個不存在的檔案,於是返回空結果。 為了解決這個問題,需要改動nginx預設的fastcgiparams配置檔案

/etc/nginx/fastcgi_params 檔案的最後增加兩行:

  fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
  fastcgi_param PATH_INFO     $fastcgi_script_name;  

再再再次訪問域名,終於正常顯示了,但是頁面上又出現了新的錯誤:

*could not find driver(select from users where deleted = 0)**

任何有sql查詢的頁面都報如上錯誤,這個錯誤多半是因為pdo_mysql未開啟或者未安裝此模組造成,執行 php -m 發現還真沒裝,安裝php72w-mysql又遇到一個坑

  yum install php72w-mysql

報如下錯誤,說缺少libmysqlclient.so.18依賴

  ......
  error: package: php72w-mysql 
  requires: libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Available: 1:mariadb-libs-5.5.52-1.el7.x86_64 (base)
             libmysqlclient.so.18(libmysqlclient_18)(64bit)
  ......

這就很奇怪了,檢視使用者庫檔案目錄內libmysqlclient.so.18已經存在

  [root@iZbp17dq2xryqoixibq5u1Z ~]# ll /usr/lib64/mysql/libmysqlclient.so.18
  libmysqlclient.so.18      libmysqlclient.so.18.1.0

這個坑花了一個小時,Google多次嘗試無果後,認真思考下可能的原因,想了想可能是mysql版本的問題,修改下 /etc/yum.repos.d/mysql-community.repo 配置檔案,將5.6的enabled設為1,5.5設為0,更新Mysql版本後,再次執行 yum install php72w-mysql 安裝pdo_mysql模組成功…...

後續的部署最佳化,另外開篇記錄。

原文部落格地址

本作品採用《CC 協議》,轉載必須註明作者和本文連結
Kill Time, Or Kiss Time.

相關文章