什麼是 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。 PHP5.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
第二步: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 保存文件的命令。如果您需要保留文件而不保存它 : 問..隨意使用最熟悉的文本編輯器。
下面是php.ini文件的推薦值。
max_execution_time = 300
最長輸入時間 = 60
memory_limit = 256M
upload_max_filesize = 100M
在文件中找到這些變量並更新它們的值。
編輯前:
編輯後:
第 4 步:默認站點配置
是時候設置默認站點環境了。打開站點配置文件。默認情況下,它位於以下路徑中:
/etc/nginx/sites-available/默認
你可以使用它 光盤 命令到達那里或打開它 vim..
刪除 PHP 7.0 和 PHP 7.1 的以下註釋行:
PHP 7.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。聯繫我們的銷售團隊了解更多信息。