在Ubuntu上部署ThinkPHP項目時可能會遇到各種問題,以下是一些常見的部署步驟和解決方案:
sudo apt update
sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
sudo apt install nginx
sudo apt install apache2 libapache2-mod-php php php-mysql
Nginx配置:
編輯 /etc/nginx/sites-available/default
文件,添加以下內容:
server {
listen 80;
server_name your_domain_or_ip;
root /path/to/your/thinkphp/project;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
創建符號鏈接以啟用該配置:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
重啟Nginx服務:
sudo systemctl restart nginx
Apache配置:
確保已啟用 mod_rewrite
模塊:
sudo a2enmod rewrite
在 /etc/apache2/sites-available
目錄下創建一個新的配置文件,例如 your_project.conf
,并添加以下內容:
<VirtualHost *:80>
ServerName your_domain_or_ip
DocumentRoot /path/to/your/thinkphp/project
<Directory /path/to/your/thinkphp/project>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/your_project_error.log
CustomLog ${APACHE_LOG_DIR}/your_project_access.log combined
</VirtualHost>
啟用該配置:
sudo a2ensite your_project.conf
重啟Apache服務:
sudo systemctl restart apache2
sudo chown -R www-data:www-data /path/to/your/thinkphp/project
sudo chmod -R 755 /path/to/your/thinkphp/project
/etc/php/7.4/fpm/pool.d/www.conf
文件,將 cgi.fix_pathinfo
設置為 0
:cgi.fix_pathinfo = 0
sudo systemctl restart php7.4-fpm
fastcgi_pass
路徑正確,并且PHP-FPM正在運行。php.ini
來實現。如果在部署過程中遇到其他問題,可以參考ThinkPHP的官方文檔或在相關社區尋求幫助。