跳到主內容

ThinkPHP Nginx 安裝及設定 - CentOS 7

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

  2023.04.10 修改安裝方式並更新服務版本

運行環境


環境都是在 「Proxmox VE 」 虛擬系統上架設,都是以 「 LXC 」模式為主,除非有特殊狀況會告知使用 「 VM 」 模式

  • 系統環境: CentOS 7
  • Web 服務: Nginx 1.23.4
  • PHP 服務: PHP 7.4
  • 資料庫服務: MariaDB 10.11

Web 服務


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

資料庫服務


# 新增 MariaDB 來源庫
vim /etc/yum.repos.d/MariaDB.repo
  • 將以下內容貼上
# MariaDB 10.11 CentOS repository list - created 2023-04-10 05:48 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
# rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# baseurl = https://rpm.mariadb.org/10.11/centos/$releasever/$basearch
baseurl = https://ftp.ubuntu-tw.org/mirror/mariadb/yum/10.11/centos/$releasever/$basearch
module_hotfixes = 1
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://ftp.ubuntu-tw.org/mirror/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
  • 安裝 MariaDB
sudo yum install MariaDB-server MariaDB-client -y
  • 啟動資料庫
sudo systemctl enable mariadb && sudo systemctl start mariadb
  • 資料庫安裝及設定
# 設定資料庫的root密碼
mariadb-secure-installation
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]    <----- 直接按下 enter 鍵
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]               <----- 直接按下 enter 鍵
New password:                                 <----- 輸入 root 密碼
Re-enter new password:                        <----- 再次輸入 root 密碼
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 服務


  • 先安裝額外的套件及載點
sudo yum install -y epel-release yum-utils
  • 因內鍵預設版本是 PHP 5.4 因此本版官方已不在維護,建議直接安裝當時官方所提供主要版本,現在安裝版本是 PHP 7.4
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum-config-manager --disable remi-php54
yum-config-manager --enable remi-php74
  • 安裝 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
  • 設定「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
  • 重啟及開機自動啟動 php-fpm 服務
sudo systemctl restart php-fpm &&  sudo 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 安裝

  • 設定 Composer
# 將dirs更改為我們希望將composer安裝到的位置
cd /usr/local/bin
# 安裝 composer
curl -sS https://getcomposer.org/installer | php
# 重新命名 composer
mv composer.phar composer
  • 下載好之後到 Web 服務目錄下載 ThinkPHP 開發包 cd /var/www/html/
composer create-project topthink/think=5.1.* tp5
  • 本次下載的版本是 5.1版本,那在 tp5 這是所要建立的目錄名稱,這可以自行修改成自己想要名稱

  • 設定該目錄有 WEB 權限可讀取

chown -R nginx:nginx /var/www/html/tp5/

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

chmod -R 0777 /var/www/html/tp5/runtime
  • 開啟瀏灠器輸入web服務主機,如果連線成功的話會出現下面的畫面

thinkphp-01.png

補充說明


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



參考相關網頁