天天看點

CentOS7 下配置 Nginx + PHP7 + MariaDB + ThinkPHP5.1

最近突然想學習一下使用一些比較成熟的背景架構,考慮到之前幫大佬打下手的時候用過 ThinkPHP 是以就暫定了以 ThinkPHP 為主要學習目标。

下面是我在伺服器端配置 Thinkphp 所需環境而踩的一些坑(且有很多已經是老坑了):

  • 本次配置選擇的伺服器系統為 Centos7 環境是 Nginx + PHP7 + MariaDB + ThinkPHP5.1
  • 我會按照我個人認為合适的順序分别介紹他們的安裝與配置過程

1. MariaDB 的安裝與初始配置

# yum 安裝
yum -y install mariadb mariadb-server

# 安裝完成MariaDB,首先啟動MariaDB
systemctl start mariadb
# 設定開機啟動
systemctl enable mariadb

# 接下來進行MariaDB的相關簡單配置
mysql_secure_installation

#首先是設定密碼,會提示先輸入密碼
Enter current password for root (enter for none):# 初次運作直接回車

#設定密碼
Set root password? [Y/n] # 是否設定root使用者密碼,輸入y并回車或直接回車
New password: # 設定root使用者的密碼
Re-enter new password: # 再輸入一次你設定的密碼

#其他配置
Remove anonymous users? [Y/n] # 是否删除匿名使用者
Disallow root login remotely? [Y/n] #是否禁止root遠端登入
Remove test database and access to it? [Y/n] # 是否删除test資料庫
Reload privilege tables now? [Y/n] # 是否重新加載權限表

#初始化MariaDB完成,接下來測試登入
mysql -uroot -ppassword           

2. PHP7 + PHP-FPM 安裝

# 在centos7通過yum安裝PHP7,首先在終端運作:
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# 提示錯誤:
error: Failed dependencies:
epel-release >= 7 is needed by webtatic-release-7-3.noarch

# 需要先安裝epel-release。
yum -y install epel-release 

# 再安裝PHP7
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm # 擷取PHP7的yum源
yum install php70w php70w-fpm

# 驗證安裝
PHP -v,

# 顯示目前PHP版本,資訊如下:
[root@Ryoma /]# php -v
PHP 7.0.18 (cli) (built: Apr 15 2017 07:09:11) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

# 這樣就在CentOS 7下通過yum安裝成功PHP7.           

3. 編譯安裝 Nginx 并配置 PHP

# [必須]安裝 nginx 需要先将官網下載下傳的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
yum install gcc-c++

# [必須]PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 相容的正規表達式庫。nginx 的 http 子產品使用 pcre 來解析正規表達式,是以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。指令:
yum install -y pcre pcre-devel

# [自選]zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的内容進行 gzip ,是以需要在 Centos 上安裝 zlib 庫。
yum install -y zlib zlib-devel

# [自選]nginx 不僅支援 http 協定,還支援 https(即在ssl協定上傳輸http),是以需要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel

# 官網下載下傳 Ngxin
# 直接下載下傳.tar.gz安裝包,位址:https://nginx.org/en/download.html
# 推薦選擇穩定版下載下傳 : 目前版本 1.12.2
# 下載下傳連結: https://nginx.org/download/nginx-1.12.2.tar.gz
wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

# 解壓 用到了之前下載下傳的zlib庫 若沒下載下傳可選擇其他方式解壓
tar -zxvf nginx-1.12.2.tar.gz

# 編譯前初始化配置
cd nginx-1.12.2 # 進入nginx目錄
./configure # 使用預設配置

# 編譯安裝
make
make install

# 查找安裝路徑
[root@Ryoma nginx-1.12.2]#whereis nginx
nginx: /usr/local/nginx

# 啟動、停止 nginx
cd /usr/local/nginx/sbin/ # 進入 nginx sbin 目錄
./nginx # 啟動 nginx
./nginx -s quit # 此方式停止步驟是待 nginx 程序處理任務完畢進行停止
./nginx -s stop # 此方式相當于先查出 nginx 程序id再使用kill指令強制殺掉程序
./nginx -s reload # 重新加載

#查詢 nginx 程序:
ps aux|grep nginx

# 配置 nginx
vim /usr/local/nginx/conf/nginx.conf
# 個人配置圖如下           

4. 安裝 ThinkPHP5

# 全局安裝 composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

# tp5 5.1 版本安裝
composer create-project topthink/think  tp5  --prefer-dist
# PHP版本低于5.6的話 将仍然安裝5.0版本