在Linux系統中部署ThinkPHP應用,可以按照以下步驟進行:
sudo apt update
sudo apt install apache2
sudo apt update
sudo apt install nginx
sudo apt update
sudo apt install php php-cli php-fpm php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
編輯/etc/apache2/sites-available/000-default.conf
文件,添加以下內容:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
啟用站點:
sudo a2ensite 000-default.conf
sudo systemctl restart apache2
編輯/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;
}
}
重啟Nginx:
sudo systemctl restart nginx
下載ThinkPHP應用:
cd /var/www/html
git clone https://github.com/top-think/thinkphp.git myapp
cd myapp
配置數據庫:
編輯application/database.php
文件,配置數據庫連接信息。
安裝Composer依賴:
composer install
運行應用:
php run start
確保防火墻允許HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
如果你需要HTTPS,可以使用Let’s Encrypt免費獲取SSL證書:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
通過以上步驟,你應該能夠在Linux系統中成功部署ThinkPHP應用。