在Ubuntu上部署PHP文件通常涉及以下幾個步驟:
安裝Web服務器:
sudo apt update
sudo apt install apache2
sudo apt update
sudo apt install nginx
安裝PHP:
sudo apt update
sudo apt install php libapache2-mod-php php-cli php-fpm
sudo apt install php-fpm
配置Web服務器:
sudo systemctl start apache2
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo systemctl start nginx
/etc/nginx/sites-available/default
),添加以下內容:server {
listen 80;
server_name example.com; # 替換為你的域名或IP地址
root /var/www/html; # 替換為你的PHP文件所在的目錄
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; # 根據你的PHP版本調整路徑
}
location ~ /\.ht {
deny all;
}
}
sudo systemctl restart nginx
上傳PHP文件:
/var/www/html
;對于Nginx,默認目錄也是/var/www/html
。設置文件權限:
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html -type f -exec chmod 644 {} \;
sudo find /var/www/html -type d -exec chmod 755 {} \;
測試部署:
通過以上步驟,你應該能夠在Ubuntu上成功部署PHP文件。如果你遇到任何問題,請檢查Web服務器的錯誤日志以獲取更多信息。