什麼是 NGINX?
NGINX 是一個開源 Linux Web 服務器,可在使用更少資源的同時加速內容。 NGINX 提供:
- 表現。
- 穩定。
- 負荷分配。
- 反向代理。
- 郵件代理。
- HTTP 緩存。
NGINX 默認不運行 PHP 腳本,必須進行配置。本教程將幫助您配置 NGINX 和 PHP,以在您的服務器上啟用和測試 PHP 功能。
筆記在 7.2.24 之前的 PHP 版本中發現了 PHP-FPM 和 NGINX 錯誤。這個 常用表達 裡面 fastcgi_split_path_info 濫用指令可能導致遠程代碼執行。 1 修理 在 PHP 7.2.24 和 7.3.11 中發布以修復此問題。
開始前
- 以 root 身份登錄或添加到您的 Ubuntu 16.04 LTS 服務器 須藤 在所有你還沒有的命令之前。
- 安裝 NGINX 在 Ubuntu 16.04 服務器上。
- 本教程使用 NGINX 1.10.3。舊版本可能會工作,但建議在配置 PHP 之前更新到最新的可用版本。
- 本教程正在運行 php7.0-fpm。 PHP 5.6 是的 生命的盡頭 不再支持。
第 1 步:更新所有軟件包
通過運行以下命令更新所有 Linux 軟件包:
sudo apt-get update && apt-get upgrade
使用以下命令檢查 NGINX 的當前版本:
nginx -v
結果是以下輸出。
筆記最佳實踐是使用最新版本的 NGINX。 在進行任何更改之前備份您的 NGINX 和 PHP 配置。
使用以下命令測試 NGINX 配置文件的語法錯誤:
nginx -t
如果成功,您將獲得如下輸出:
這個 nginx -t 如果失敗,該命令將為您提供查找錯誤的起點。
安裝 NGINX 後,它會自動啟動。但是,有一些用於控制 NGINX 的命令。
sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
sudo systemctl restart nginx.service
第 2 步:安裝 PHP 並檢查版本
如果你需要安裝 PHP,你可以運行這一行:
sudo apt-get -y install php7.0 php7.0-fpm
將 7.0 替換為最新的 PHP 版本。您可以在此處查看更新。
或者,如果您需要將 PHP 更新到最新版本,請在進行任何更改之前進行備份並運行:
sudo apt-get upgrade
現在檢查 PHP 是否正在運行以及它是什麼版本。使用以下命令執行此操作:
sudo systemctl status php7.0-fpm
您應該會看到類似於以下內容的輸出:
第三步:NGINX PHP 配置
一旦 NGINX 和 PHP 在您的系統設置中運行,請配置您的 PHP 設置。
從家中,使用以下命令將目錄更改為 NGINX 文件夾: 光盤 命令。
cd ~
cd /etc/nginx
要配置 NGINX PHP 設置設置, 光盤 進來 等/php 文件夾。
cd etc/php/
它正在尋找一個名為 php.ini 的文件。使用以下命令從文本編輯器訪問該文件:
vim 7.0/fpm/php.ini
還
vim 7.1/fpm/php.ini
筆記該文件夾取決於您使用的 PHP 版本。 將 7.0 或 7.1 替換為您的 PHP 版本。
這個 php.ini 這是一個允許自定義環境的大文件。最佳做法是在進行任何更改之前製作此文件的副本。使用以下命令製作文件的副本:
cp php.ini php.ini_copy
筆記在 Vim 文本編輯器中, 一代 命令插入, 結束 命令結束, :wq 使用命令保存文件如果您需要不保存文件,請使用 :q隨意使用您最喜歡的文本編輯器。
下面是php.ini文件的推薦值。
max_execution_time = 300
最長輸入時間 = 60
內存限制 = 256M
upload_max_filesize = 100M
在文件中找到這些變量並更新它們的值。
編輯前:
編輯後:
第 4 步:默認站點配置
是時候設置默認站點環境了。打開站點配置文件。默認情況下,它位於以下路徑中:
/etc/nginx/sites-available/default
你可以使用它 光盤 命令到達或打開它 維姆.
刪除 PHP7.0 和 PHP 7.1 的以下註釋行。
PHP7.0#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
PHP 7.1server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
第 5 步:使用 NGINX 測試 PHP
進行必要的編輯後,使用以下行重新啟動 NGINX 和 PHP:
sudo systemctl restart nginx.service
使用此語法。 phpinfo.php 在文件中 /var/www/html 文件路徑。
sudo vim /var/www/html/phpinfo.php
將以下內容添加到文件中並保存。
<?php phpinfo( ); ?>
要測試您的設置,請輸入您的服務器 IP,然後 /phpinfo.php 在您的網絡瀏覽器中。
https://yourserverip/phpinfo.php
當您看到類似以下內容時,NGINX PHP 配置已成功設置:
包起來
配置 NGINX 來讀取 PHP 有幾個好處。 在設置 NGINX PHP 配置之前,需要準備一些額外的部分,但結果是值得的。
Liquid Web 的專用服務器具有 root 訪問權限,並且可以選擇安裝和配置 NGINX。請聯繫我們的銷售團隊以獲取更多信息。