在Ubuntu上配置PHP環境是一個相對簡單的過程,以下是一個詳細的步驟指南,包括安裝、配置、性能優化和安全設置。
sudo apt update
sudo apt install php php-cli php-fpm php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath
php -v
sudo apt install apache2
sudo a2enmod php7.4-fpm # 替換為你的PHP版本
sudo systemctl restart apache2
/etc/apache2/sites-available/
目錄下有一個配置文件,例如000-default.conf
,并包含以下內容:<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo apt install nginx
/etc/nginx/sites-available/
目錄下創建或編輯一個配置文件,例如default
,并包含以下內容:server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 替換為你的PHP版本
}
location ~ /\.ht {
deny all;
}
}
sudo systemctl restart nginx
/etc/php/7.4/fpm/php.ini
(替換為你的PHP版本),取消以下行的注釋:zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=64
opcache.max_accelerated_files=4000
opcache.revalidate_freq=2
/etc/php/7.4/fpm/pool.d/www.conf
,調整以下設置:pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx
display_errors Off
log_errors On
error_reporting E_ALL & E_NOTICE & E_DEPRECATED & E_STRICT & E_USER_NOTICE & E_USER_WARNING
safe_mode On
safe_mode_allowed_classes "mysqli,pdo_mysql"
disable_functions exec,system,passthru,shell_exec,popen,curl,fsockopen,file_get_contents,mkdir,rmdir,rename,unlink,chmod,chown,chgrp,date,time,localtime,gmtime,isset,empty,is_array,is_object,is_resource,is_string,is_int,is_float,is_bool,is_callable,assert
open_basedir
限制PHP可以訪問的系統目錄:open_basedir /var/www/html:/tmp
allow_url_fopen
和allow_url_include
:allow_url_fopen Off
allow_url_include Off
sudo ufw allow 22/tcp # 允許SSH
sudo ufw allow 80/tcp # 允許HTTP
sudo ufw allow 443/tcp # 允許HTTPS
sudo ufw enable
sudo apt update
sudo apt upgrade
通過以上步驟,你可以在Ubuntu上成功配置PHP環境,并進行性能優化和安全設置。請根據你的具體需求調整配置。