網站與網頁技術 - PHP 7.3.0正式發表與Nginx / PHP-FPM實作 - 電腦

前往內容


PHP 7.3.0正式發表與Nginx / PHP-FPM實作

PHP 7.3.0終於在12月6日正式發表, 首先嘗試原始碼編譯安裝的方式與Nginx Web Server建構網站應成程式伺服器, 這裡採用PHP-FPM方式, 便於Nginx與PHP的個別程序管理, 測試環境為Debian Linux 9.6的VMware虛擬機器, Nginx採用預設package安裝, 畢竟本次主軸在PHP 7.3.0身上。

Debian Linux安裝編譯PHP與需要的工具
apt-get install build-essential autoconf libxml2-dev openssl curl libzip-dev libcurl4-gnutls-dev libmcrypt-dev re2c


安裝Nginx Web Server
apt-get install nginx

稍後修改Nginx與PHP-FPM的設定

一些函數目錄的連結建立(OpenSSL與curl會使用)
ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib
ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/curl


下載PHP 7.3.0原始碼
wget http://tw2.php.net/get/php-7.3.0.tar.gz/from/this/mirror -O php-7.3.0.tar.gz


解壓縮php-7.3.0.tar.gz
tar zxvf php-7.3.0.tar.gz
cd php-7.3.0


PHP組態設定
./configure --with-config-file-path=/etc/php7/fpm --enable-pcntl --with-zlib --enable-mbstring --with-iconv --with-openssl --with-curl --enable-fpm --enable-mysqlnd --with-libxml-dir=/usr/include/libxml2 --with-mysqli


編譯並安裝
make
make install


建立PHP-FPM設定目錄, 存放php.ini與php-fpm.conf
mkdir -p /etc/php7/fpm
cp php.ini-production /etc/php7/fpm/php.ini

這裡複製了原始碼檔案裡的php.ini範本來用, 請依照需求修改

PHP 7.2就移除了mcrypt函數, 這裡手動補回
wget https://pecl.php.net/get/mcrypt-1.0.1.tgz
tar zxvf mcrypt-1.0.1.tgz
cd mcrypt-1.0.1
phpize
./confgiure
make
make install


檢視模組目錄, 要有mcrypt.so存在
ls -l /usr/local/lib/php/extensions/no-debug-non-zts-20180731/


將php.ini與php-fpm.conf相關檔案與目錄移到我們要的組態目錄(/etc/php7/fpm)
cd /usr/local/etc
mv php-fpm.conf.default /etc/php7/fpm/php-fpm.conf
mv php-fpm.d /etc/php7/fpm/
mv /etc/php7/fpm/php-fpm.d/www.conf.default /etc/php7/fpm/php-fpm.d/www.conf

php-fpm.conf為PHP-FPM設定, 也會讀取/etc/php7/fpm/php-fpm.d/www.conf, 後續再修改它們

首先修改/etc/php7/fpm/php-fpm.conf
nano /etc/php7/fpm/php-fpm.conf

修改以下設定
pid = /run/php-fpm.pid


接著修改www.conf
nano /etc/php7/fpm/php-fpm.d/www.conf

修改以下設定
user = www-data
group = www-data
listen = /var/run/php7.3-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

其他的設定如
pm = dynamic
pm.max_children = 5
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.process_idle_timeout = 10s;
pm.max_requests = 200

請評估自己的機器規格設定, 您也能找找網路上PHP-FPM的設定建議

建立PHP-FPM系統服務檔
nano /lib/systemd/system/php-fpm.service

將以下內容貼上
[Unit]
Description=The PHP 7.3 FastCGI Process Manager
After=local-fs.target network.target nginx.service

[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/sbin/php-fpm --fpm-config /etc/php7/fpm/php-fpm.conf --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target


預設開機啟動PHP-FPM服務, 並把它跑起來
systemctl enable php-fpm
systemctl start php-fpm


修改Nginx的網站設定,
nano /etc/nginx/sites-available/default

修改網站目錄, 使用Unix Socket連接PHP, 並補上index.php為首頁

server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;

index index.php index.html index.htm;

server_name _;

location / {
try_files $uri $uri/ =404;
}

# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php7.3-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}


這裡還不需要SSL
nano /etc/nginx/nginx.conf


將SSL相關設定關掉
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

#ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
#ssl_prefer_server_ciphers on;


##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}


重新啟動Nginx
service nginx restart


在網站目錄放個index.php來當首頁測試, 就用phpinfo()函數好了
nano /var/www/html/index.php

貼入內容
<?php phpinfo(); ?>


接著在瀏覽器試試看網站有沒有跑起來, 有的話像是這樣的結果


後續就可以裝資料庫軟體, 與您需要的網站應用程式!
如果覺得太麻煩, 那就稍等Debian開發者Ondřej Surý的PHP 7 repository: https://packages.sury.org/php/
今天仍停留在PHP 7.3 RC6, 也許不久正式版就更新了

1頁 (共1頁)

前往