ThinkPHP Nginx 安裝及設定

BUBU 因為之前有學習 Laravel  ,但是對我入門新手來說有一點高,因此老大有推一套大陸的版的 MVC 系統來做開發,老大本身也有在用這一套開發,這套在大陸那裡資源來滿多的文件都是中文比較,Laravel 大多都是原文比較多找起資料會有一點麻煩,那我來記錄一下怎麼將此開發程式怎麼安裝及應用

環境:

CentOS 7、PHP 7.3、MariaDB 10.4、ThinkPHP 5.1、Nginx 1.17.4

Web 服務


採用 Nginx 服務來運行網頁,安裝方式請參考本知識庫的 啟用Nginx 支援到 OpenSSL TLS1.3 安全協定 安裝方式

資料庫服務

 


採用 MariaDB 10.4

 

  • 首先新增加 MariaDB.repo 官方載點
新增加MariaDB安裝檔
vim /etc/yum.repos.d/MariaDB.repo

再填入內容為以下

# MariaDB 10.4 CentOS repository list - created 2019-08-21 13:29 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

增加完畢後在下命令安裝

yum install MariaDB-server MariaDB-client -y
  • 資料庫安裝及設定
systemctl enable mariadb ; systemctl start mariadb   # 啟動資料庫服
mysql_secure_installation   # 設定資料庫的root密碼

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n]
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

PHP 服務


  • 先安裝額外的套件及載點
yum install -y epel-release yum-utils
  • 因內鍵預設版本是 PHP 5.4 因此本版官方已不在維護,建議直接安裝當時官方所提供主要版本,現在安裝版本是 PHP 7.3
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum-config-manager --disable remi-php54
yum-config-manager --enable remi-php73
  • 安裝 PHP 套件
yum install -y php php-mysqlnd php-pdo php-xml php-pear php-devel php-mbstring re2c gcc-c++ gcc \
php-fpm php-mysql php-gd php-cli php-json php-opcache php-curl php-ldap php-odbc php-xmlrpc php-soap \
curl curl-devel php-intl  php-zip
  • 安裝好 PHP 要去修改 「vim /etc/php.ini」 設定檔
修改時區
[Date]
修改前
;date.timezone = 
修改後
date.timezone = Asia/Taipei

存檔
:wq
  • 設定「php-fpm」「vim /etc/php-fpm.d/www.conf
由誰使用此服務
; RPM: apache user chosen to provide access to the same directories as httpd
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group =nginx

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

修改好存檔
:wq
  • 重啟及開機自動啟動 php-fpm 服務
systemctl restart php-fpm ; systemctl enable php-fpm

 

設定 Nginx 服務設定檔


  • 設定「vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
#
events {
    worker_connections 1024;
}
#
http {
    include /etc/nginx/mime.types;
#gzip  on;
    include /etc/nginx/conf.d/*.conf;
    default_type application/octet-stream;
    log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log  main;
    sendfile on;
#tcp_nopush     on;
    keepalive_timeout 65;
# 以下是反代理取得真實 IP    
# Restoring the original visitor IP for your web application
    set_real_ip_from 內網 IP ;
    set_real_ip_from 公網 IP 指的是自己目前當下的 IP;
    set_real_ip_from 127.0.0.1;
# use any of the following two
# real_ip_header CF-Connecting-IP;
    real_ip_header X-Forwarded-For;
}
  • 設定「vim /etc/nginx/conf.d/XXX.conf
server {
    listen 80;
    server_name localhost;
    root /var/www/html/tp5/public;
#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;
    location / {
        index index.html index.htm index.php;
#autoindex on;
        if (!-e $request_filename) {
            rewrite ^(.*)$  /index.php?s=/$1  last;
            break;
        }
        location = /50x.html {
            root /usr/share/nginx/html;
        }
# 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$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param PATH_TRANSLATED  $document_root$fastcgi_path_info;
        }
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
        location ~ /\.ht {
            deny all;
        }
#error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
        error_page 500 502 503 504  /50x.html;
    }
}

程式碼說明:

在Nginx低版本中,是不支援PATHINFO的,但是可以通過在Nginx.conf(在/usr/local/nginx/conf/nginx.conf或者通過find / | grep nginx.conf來查詢位置)中配置轉發規則實現:在nginx配置檔案中新增:

 location / { // …..省略部分代碼
   if (!-e $request_filename) {
   rewrite  ^(.*)$  /index.php?s=/$1  last;
   break;
    }
 }

手動增加三個行程式

location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;  #新增加
            fastcgi_param PATH_INFO $fastcgi_path_info;     #新增加
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param PATH_TRANSLATED  $document_root$fastcgi_path_info;      #新增加
        }

 

 

ThinkPHP 開發程式

有提供三種方式下載程式包

  1. 官方下載,但是版本不一定會是最新的
  2. 官方得 GitHub 下載,版本都會是最新的包含正在發開中的程式。
  3. 使用 Composer 作曲家 服務來下載程式來做開發

那以上三種前面兩種我都試過了服務沒有辦法啟來,使用 Composer 作曲家,方式來下載開發程式

Composer 安裝

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

下載好之後到 Web 服務目錄下載 ThinkPHP 開發包

composer create-project topthink/think=5.1.* tp5

本次下載的版本是 5.1版本,那在 tp5 這是所要建立的目錄名稱,這可以自行修改成自己想要名稱

執完之後要給於該目前一個權限

chown -R nginx:nginx tp5/

mac或者linux環境下面,注意需要設置runtime目錄權限為777。

開啟瀏灠器輸入web服務主機,如果連線成功的話會出現下面的畫面

thinkphp-01.png

 

補充說明


指令列方式來運行 ThinkPHP 服務,要在 ThinkPHP 所下載目錄下執行此指令

 

php think run

 

 

參考相關網頁: