在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的官方文档或在相关社区寻求帮助。