- 目前沒什麼活動,三年1400軟妹幣
- 增長本機ssh鏈接配置,通常激活實例後,ssh的22端口是默認開放的,能夠直接經過root用戶進行登陸配置部署環境
- 登陸到服務器後,將本身的公鑰加入到 ~/.ssh/authorized_keys 配置文件中就可直接經過祕鑰進行登陸
- 服務器系統版本:CentOS Linux release 7.4.1708 (Core)
- 內存:1G
- CPU:1核
- 硬盤:40G
由於服務器配置有點低,因此這邊選擇安裝比較低的mysql版本。從CentOS 7.0發佈以來,yum源中開始使用mariadb來代替MySQL的安裝。即便你輸入的是yum install mysql , 顯示的也是mariadb的安裝內容,所以,若是使用yum安裝MySQL的話,就須要去下載官方指定的yum源。 網址: https://dev.mysql.com/downloads/repo/yum/。php
先卸載mariadb,查看mariadb是否已經安裝html
[root@iZbp17dq2xryqoixibq5u1Z ~]# yum list installed | grep mariadb
mariadb-libs.x86_64 1:5.5.56-2.el7 @anaconda
複製代碼
進行卸載mysql
[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源和數據庫安裝(官方指南)nginx
wget http://repo.mysql.com/yum/mysql-5.5-community/el/6/x86_64/mysql-community-release-el6-5.noarch.rpm #MySQL5.5yum源
複製代碼
安裝yum源git
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改成0web
# 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可安裝文件sql
[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
複製代碼
安裝mysqlshell
# 安裝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-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
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/ 目錄下,
[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命令,依賴下載成功
我本機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的進程間進行通訊的文件,具體的含義,這邊就不作詳細介紹了。進這個目錄查看下是否存在這個目錄或文件,發現兩個問題:
未生成緣由: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模塊成功…...
後續的部署優化,另外開篇記錄。