在Linux上配置PHP通常涉及以下幾個步驟:
安裝PHP: 你可以使用包管理器來安裝PHP。以下是一些常見Linux發行版的安裝命令:
Debian/Ubuntu:
sudo apt update
sudo apt install php php-cli php-fpm php-mysql php-json php-xml php-mbstring php-zip
CentOS/RHEL:
sudo yum install php php-cli php-fpm php-mysqlnd php-json php-xml php-mbstring php-zip
Fedora:
sudo dnf install php php-cli php-fpm php-mysqlnd php-json php-xml php-mbstring php-zip
配置Web服務器: 安裝PHP后,你需要配置Web服務器(如Apache或Nginx)來處理PHP文件。
Apache:
編輯Apache的配置文件(通常位于/etc/apache2/sites-available/
或/etc/httpd/conf.d/
),添加以下內容:
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
# 使用FastCGI處理PHP文件
SetHandler application/x-httpd-php
# 或者使用PHP-FPM
# SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
然后啟用PHP模塊并重啟Apache:
sudo a2enmod php7.4
sudo systemctl restart apache2
Nginx:
編輯Nginx的配置文件(通常位于/etc/nginx/sites-available/
),添加以下內容:
server {
listen 80;
server_name yourdomain.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
然后啟用PHP-FPM并重啟Nginx:
sudo systemctl start php7.4-fpm
sudo systemctl restart nginx
配置PHP:
編輯PHP的配置文件(通常位于/etc/php/7.4/cli/php.ini
或/etc/php/7.4/fpm/php.ini
),根據需要進行調整。例如:
; 啟用擴展
extension=mysqlnd.so
extension=pdo_mysql.so
extension=json.so
extension=xml.so
extension=mbstring.so
extension=zip.so
; 設置時區
date.timezone = "America/New_York"
; 設置上傳臨時目錄
upload_tmp_dir = "/tmp"
; 設置錯誤日志
error_log = /var/log/php7.4-fpm.log
重啟服務: 修改配置文件后,記得重啟相應的服務以使更改生效:
sudo systemctl restart php7.4-fpm
sudo systemctl restart apache2 # 或 sudo systemctl restart nginx
通過以上步驟,你應該能夠在Linux上成功配置PHP。如果你遇到任何問題,請檢查服務日志以獲取更多信息。